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

Allow external CommonJS modules to be imported properly in Node #3473

Merged
merged 3 commits into from Jun 22, 2021

Conversation

matthewp
Copy link
Contributor

Changes

In Node anything that is external goes through requireOrImport. For CommonJS this resolves to the module.exports value. In order to do import def from 'default' that object needs a default value. This adds it.

Testing

Test added.

Docs

N/A, bug fix.

@matthewp matthewp requested a review from a team as a code owner June 22, 2021 16:20
@vercel
Copy link

vercel bot commented Jun 22, 2021

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/DwFjVSbxVJTSQGXH4TrMnqjvkk7t
✅ Preview: https://snowpack-git-import-node-default2-pikapkg.vercel.app

@matthewp
Copy link
Contributor Author

I might have been too eager here by making the ESM modules return the whole namespace. I'll change that part back.

Copy link
Contributor

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

LGTM!

resolve(mdl);
} catch (e) {
if (e instanceof SyntaxError && /export|import/.test(e.message)) {
console.error(`Failed to load "${filepath}"!\nESM format is not natively supported in "node@${process.version}".\nPlease use CommonJS or upgrade to an LTS version of node above "node@12.17.0".`)
} else if (e.code === 'ERR_REQUIRE_ESM') {
const url = pathToFileURL(resolvedPath);
return NATIVE_IMPORT(url).then(mdl => resolve(mdl.default ? mdl.default : mdl));
};
return NATIVE_IMPORT(url).then(mdl => resolve(mdl.default ? mdl.default : mdl), err => reject(err));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any difference between onRejected vs catch()? I have a slight preference for the latter because it’s easier to read but don’t feel too strongly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.catch matters where you put it, if you put it after the then it would reject if the caller had an error, when is not what we want. Could have put the .catch before the then though.

Copy link
Collaborator

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

LGTM!

@matthewp matthewp merged commit fb7eaaa into main Jun 22, 2021
@matthewp matthewp deleted the import-node-default2 branch June 22, 2021 17:37
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.

None yet

3 participants