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

Package exports flawed resolution #388

Closed
aduh95 opened this issue Mar 1, 2021 · 1 comment
Closed

Package exports flawed resolution #388

aduh95 opened this issue Mar 1, 2021 · 1 comment

Comments

@aduh95
Copy link
Contributor

aduh95 commented Mar 1, 2021

WMR prefers to load "node" over "default" in the package.json#exports.

Here is a minimal repro:

yarn create wmr your-project-name
cd your-project-name
mkdir node_modules/test
echo '{"name":"test", "exports":{".":{"node":"./node.mjs", "default":"./browser.mjs"}}}' > node_modules/test/package.json
echo 'export default "Browser implementation"' > node_modules/test/browser.mjs
echo 'import "node:url";export default "Node.js implementation"' > node_modules/test/node.mjs
echo 'import test from "test";export default function Home(){return <div>{test}</div>}' > public/pages/home/index.js
yarn build

This outputs a warning ('node:url' is imported bynpm/test/node.mjs, but could not be resolved – treating it as an external dependency) and builds the home page using <div>Node.js implementation</div> instead of the expected <div>Browser implementation</div>.

Expected

$ grep 'Browser implementation' dist/index.html
        <body><div class="app"><header><nav><a href="/">Home</a><a href="/about">About</a><a href="/error">Error</a></nav><label>URL:<input readonly value="/" /></label></header><div>Browser implementation</div></div><script type="isodata"></script>
$ grep 'Node.js implementation' dist/index.html

Actual

$ grep 'Browser implementation' dist/index.html
$ grep 'Node.js implementation' dist/index.html
        <body><div class="app"><header><nav><a href="/">Home</a><a href="/about">About</a><a href="/error">Error</a></nav><label>URL:<input readonly value="/" /></label></header><div>Node.js implementation</div></div><script type="isodata"></script>

Additional info

BTW, if the test module is not using the node: prefix to load the Node.js module, the builds will just fails:

$ echo 'import "url";export default "Node.js implementation"' > node_modules/test/node.mjs
$ yarn build
yarn run v1.22.10
warning package.json: No license field
wmr build --prerender
Could not load url (imported bynpm/test/node.mjs): Error: url is a Node built-in - WMR does not polyfill these
error Command failed with exit code 1.

For reference, @rollup/plugin-node-resolve and Webpack both prefers "default" over "node" in the same situation.

@rschristian
Copy link
Member

Closing as this appears to be working as expected now. Let me know if that is incorrect.

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

No branches or pull requests

2 participants