Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ESM modules with airtap #294

Open
bmz1 opened this issue Sep 9, 2020 · 15 comments · May be fixed by #328
Open

Using ESM modules with airtap #294

bmz1 opened this issue Sep 9, 2020 · 15 comments · May be fixed by #328
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects

Comments

@bmz1
Copy link

bmz1 commented Sep 9, 2020

Hello there!

I'd like to use airtap with es6 modules, however I couldn't find a way yet.

I tried the following script: "test": "airtap --local -r esm src/test/*.test.js"
It looks like airtap ignores the esm flag. Could you please recommend a way to get it working?
Thanks

@vweevers
Copy link
Member

vweevers commented Sep 9, 2020

A browserify transform or plugin, like babel or esmify, should do the trick. Unless you want to test real ESM in browsers, then I'm not sure.

@vweevers vweevers added this to Backlog in Airtap Apr 23, 2021
@jimmywarting
Copy link

We also want to test real ESM in the browser

@vweevers
Copy link
Member

I haven't used real ESM before, some guidance is welcome.

First thoughts: I suppose airtap would have to serve the user's input files as-is without bundling them, acting as a static file server. The client could then load those files either by appending <script type="module"> elements to the DOM, or through a dynamic import().

Currently we serve a bundle at http://localhost:1234/airtap/test.js. This URL will have to be dynamic and use a dedicated route, something like http://localhost:1234/user/*. We could have the airtap server send a list of entrypoint URLs to the client, over the websocket.

Which directory should we serve? For example given test/*.mjs. Do we serve test/ or the complete working directory (including node_modules)? It's likely that tests will import e.g. ../src/foo.mjs. So we'd serve files like so:

  • http://localhost:1234/user/test/foo-test.mjs => /cwd/test/foo-test.mjs
  • http://localhost:1234/user/src/foo.mjs => /cwd/src/foo.mjs

Sound good so far?

@jimmywarting
Copy link

jimmywarting commented Jul 11, 2021

sounds good so far. There is also the possibility of using import maps for node_modules

@vweevers
Copy link
Member

Are there any good (simple and unopinionated) npm packages to help us create import maps? Or (if there are other caveats besides import maps) a general solution for serving ESM files? I'm looking for small building blocks, rather than a full framework or bundler like snowpack.

@vweevers vweevers added enhancement New feature or request help wanted Extra attention is needed labels Jul 23, 2021
@vweevers vweevers changed the title Using es6 modules with airtap Using ESM modules with airtap Nov 27, 2021
@ThaUnknown
Copy link

ThaUnknown commented Nov 17, 2022

seems like specifying

browserify:
  - plugin: 'esmify'

inside .airtap.yml mostly fixes this except for type assertion, for browserify CLI doing -t [ babelify --presets [@babel/preset-env] --plugins [@babel/plugin-syntax-import-assertions]] does the job, but i have no clue how to specify that for yml as it doesn't seem to accept args

@vweevers
Copy link
Member

browserify:
  - transform: babelify
    presets: ['@babel/preset-env']
    plugins: ['@babel/plugin-syntax-import-assertions']

@ThaUnknown
Copy link

browserify:
  - transform: babelify
    presets: ['@babel/preset-env']
    plugins: ['@babel/plugin-syntax-import-assertions']

this doesn't seem to work for me, even tho it parses it

@ThaUnknown
Copy link

ThaUnknown commented Nov 17, 2022

@vweevers SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (2:0) while parsing
see: ThaUnknown/webtorrent@7adb87f

@vweevers
Copy link
Member

My example was just to show how to specify transform options. The complete configuration should also include esmify.

@ThaUnknown
Copy link

ThaUnknown commented Nov 17, 2022

My example was just to show how to specify transform options. The complete configuration should also include esmify.

for CLI browserify you don't need esmify when u specify babelify, anyways even if i add esmify as a plugin it just straight up ignores the transform:
image
code:

browserify:
  - plugin: 'esmify'
  - transform: babelify
    presets: ['@babel/preset-env']
    plugins: ['@babel/plugin-syntax-import-assertions']

the cli command for reference:

browserify -t [ babelify --presets [@babel/preset-env] --plugins [@babel/plugin-syntax-import-assertions]] --standalone WebTorrent . | minify --mangle=false > webtorrent.min.js

@vweevers
Copy link
Member

I see. OK, then I can't be of further assistance. You now know how to specify options in airtap.yml, so unless you have more questions about airtap specifically, you'll have to figure out the right combination of plugins and transforms yourself.

@ThaUnknown
Copy link

ThaUnknown commented Nov 17, 2022

@vweevers this seems to be a bug with airtap, as running the cli command I listed above works fine, with only babelify

@ThaUnknown
Copy link

ThaUnknown commented Nov 17, 2022

@vweevers welp, feel free to add this to the readme

To compile ESM tests and modules to your .airtap.yml add:

browserify:
  - transform: babelify
    global: true
    presets: ['@babel/preset-env']
    plugins: ['@babel/plugin-syntax-import-assertions']

make sure that your root package.json does not have any browserify or babel fields!

@ThaUnknown ThaUnknown linked a pull request Nov 21, 2022 that will close this issue
@robertsLando
Copy link

robertsLando commented Aug 1, 2023

When using this I get: 'ReferenceError: regeneratorRuntime is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
Airtap
  
Backlog
Development

Successfully merging a pull request may close this issue.

5 participants