-
Notifications
You must be signed in to change notification settings - Fork 1.4k
support @std/esm #1618
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
support @std/esm #1618
Conversation
| (opts.require || []).forEach(x => require(x)); | ||
| (opts.require || []).forEach(x => { | ||
| if (/.*std[/\\]esm[/\\]index\.js$/.test(x)) { | ||
| require = require(x)(module); // eslint-disable-line no-global-assign |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, we are making sure that we use @std/esm's require function instead of the standard one when we load the test file later in the file.
I am a bit surprised that this is necessary, since I figured @std/esm would install a require extension hook (via pirates or similar). @jdalton, am I mistaken here? Do you not implement a require hook via require.extensions['.js']?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since @std/esm is meant to be a production dependency, as well as a dev dependency, we can't just always overload the global require.extensions (the Node folks would flip-out, esp. since Lodash v5 will be using @std/esm).
If @std/esm is invoked through a CLI then it will hook into require.extensions, otherwise it produces its own loader instance for folks to use as you've done 👆. Whenever AVA requires @std/esm through the --require option the @std/esm loader is in CLI mode so will hook require.extensions. In this case, the thing preventing its use is the precompile check which dead-ends the load chain and prevents it from getting to @std/esm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
precompile would only be short circuiting imports for tests, but those will have already used Babel to convert the import statements to require statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I poked around in the @std/esm codebase, and didn't see where you were actually hooking require.extensions (even in "CLI mode").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
precompilewould only be short circuiting imports for tests, but those will have already used Babel to convert theimportstatements torequirestatements.
That's the only issue affecting @std/esm (the test files).
I poked around in the
@std/esmcodebase, and didn't see where you were actually hookingrequire.extensions(even in "CLI mode").
I think @std/esm might be OK with just having ababel: false or precompile: false option. That way it avoids any package specific sniffs and is something that can be applied for other uses/reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so this is actually only a problem when Babel is configured not to transpile ESM modules. Setting babel:false does not actually disable Babel - we still apply a few transforms to the tests for power-assert support, and providing better errors for common misuses of the API.
Since we're always going to load Babel, precompile: false becomes a huge performance hit, as Babel just takes too long to load.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so this is actually only a problem when Babel is configured not to transpile ESM modules.
Correct. The user wanted to just use @std/esm which is where I pointed to #709.
we still apply a few transforms to the tests for power-assert support
power-assert ships with a UMD build (no Babel required). The babel-plugin-throws-helper could always be optional or could be written for acorn+magic-string or other lighter solutions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
power-assert ships with a UMD build (no Babel required). The babel-plugin-throws-helper could always be optional or could be written for acorn+magic-string or other lighter solutions.
Interesting ideas. Power assert already has an acorn based transform, so the whole stack could share that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@std/esm uses acorn+magic-string 😋
| { | ||
| "cjs": true, | ||
| "esm": "js" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👆 You can shorthand to just "cjs" in the .esmrc file. In the next release you can use { "esm":"cjs" } as a shorthand for the above too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like "cjs"\n is the full contents of the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, kinda gross but it works. You may want to wait for the other shorthand form.
novemberborn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamestalmage just so I understand, this detects the @std/esm require and replaces the require() method we use for other "require" values?
Is there a remaining issue with how we load the transpiled test files, or how we transpile them to begin with?
lib/test-worker.js
Outdated
|
|
||
| (opts.require || []).forEach(x => require(x)); | ||
| (opts.require || []).forEach(x => { | ||
| if (/.*std[/\\]esm[/\\]index\.js$/.test(x)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should start with [/\\]@std I think, to better match just @std/esm.
|
@novemberborn - The only other issue would be providing a way to bypass the main thread precompilation. In that case, the package sniffing I am doing wouldn't be needed. |
|
It also replaces "require" for when we load the tests. |
|
(Apologies for the notification spam.) As the original reporter of standard-things/esm#197, I just want to express appreciation for everyone working on this. Always awesome to see OSS projects come together to support interoperability for users. Thanks y'all ❤️ |
novemberborn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@novemberborn - The only other issue would be providing a way to bypass the main thread precompilation. In that case, the package sniffing I am doing wouldn't be needed.
That's tracked elsewhere, but in any case it's entirely conceivable that users may want to use @std/esm while still using AVA's transpilations.
I think we should still tighten up the pattern match, and per @jdalton's comments could clean up the .esmrc file.
I know you're generally quite busy @jamestalmage (so thanks for chiming in with this PR!). Let me know if you'd like me to land it instead.
I was pretty floored by everyone jumping in to help out. It's such a pleasant surprise to have to rein in adjustments. Double thanks to y'all ❤️ |
|
Okay, |
f3cd328 to
ab8296a
Compare
This fixes support for
@std/esm.//cc: @jdalton