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

cjs auto-detection on by default #1163

Merged
merged 1 commit into from
Oct 1, 2020
Merged

cjs auto-detection on by default #1163

merged 1 commit into from
Oct 1, 2020

Conversation

FredKSchott
Copy link
Owner

Changes

  • Follow up to https://twitter.com/pikapkg/status/1311048815844102144
  • Match Node.js v14 ESM support: auto-detect CJS packages by default.
  • Keeping current "namedExports" support, since auto-detection still feels a bit rough (also, left one larger todo which would be to deeply scan re-exports (ex: exports.foo = require('./foo.js')).
  • Updated docs.

Testing

  • New test added

@FredKSchott FredKSchott requested a review from a team as a code owner September 30, 2020 06:04
@vercel
Copy link

vercel bot commented Sep 30, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/r3kzf0yiw
✅ Preview: https://snowpack-git-wip-cjs-autodetect.pikapkg.vercel.app

*/
function cjsAutoDetectExportsRuntime(normalizedFileLoc: string): string[] | undefined {
try {
return Object.keys(require(normalizedFileLoc)).filter((imp) => imp !== 'default');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very clever

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to detect if require() isn’t an object with keys first (e.g. module.exports = myFunc() or module.exports = 'some string')? I guess that would fall into the catch() block but it may be a bit noisy

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yea that's a good call. Yea that would be a "default export" anyway so the failure gives us the result we want, but yea I'd rather code defensively.

@@ -0,0 +1,2 @@
exports.export1 = 'foo';
exports.export2 = 'bar';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking this PR, but it’d be great to test module.exports = [various things that may conflict / compete with the other exports] either in the same package or another package

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're falling back to https://github.com/guybedford/cjs-module-lexer for this analysis, we can assume the edge cases are already tested on their end. But, I agree we could do a bit more here than just this simple example, will add a few more lines/exports.

export2: export2
};
export default entrypoint;
export { entrypoint as __moduleExports, export1, export2 };"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! It’s fun to see things progress to interop better between CJS and ESM

@drwpow
Copy link
Collaborator

drwpow commented Sep 30, 2020

This is such a huge addition! I feel like this has been historically one of the biggest mental barriers to using Snowpack.

@FredKSchott FredKSchott merged commit 00feade into master Oct 1, 2020
@FredKSchott FredKSchott deleted the wip-cjs-autodetect branch October 1, 2020 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants