rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 567 forks source link

[@rollup/plugin-node-resolve] resolving package.json exports/imports arrays #1714

Open ChristianM18 opened 2 months ago

ChristianM18 commented 2 months ago

⚠️ Please note that the packaged utils library in this example is available here, if needed: https://github.com/ChristianM18/build-utils

Expected Behavior

Arrays defined in package.json exports to be taken into account, with the resolution algorithm trying each item until one is successfully resolved.

  "exports": {
    "./*": ["./dist/*.js", "./dist/*/index.js"]
  },

Actual Behavior

The following error is thrown:

[!] Error: Could not load /home/projects/rollup-repro-udym2a/node_modules/utils/dist/color.js (imported by src/main.js): ENOENT: no such file or directory, open '/home/projects/rollup-repro-udym2a/node_modules/utils/dist/color.js'
ENOENT: no such file or directory, open '/home/projects/rollup-repro-udym2a/node_modules/utils/dist/color.js'

Additional Information

I expected the following statement:

import {sayFavoriteColor} from "utils/color";
console.log(sayFavoriteColor("yellow"));

to resolve to ./dist/color/index.js, as the util's library package.json contains:

  "exports": {
    "./*": ["./dist/*.js", "./dist/*/index.js"]
  },

and ./dist/color.js does not exist.
However, it seems that the fact that ./dist/color.js is missing fires an exception, making the algorithm stop.

Apparently a similar (if not same) issue has already been addressed in #670, so maybe it's just a problem on my end.

fregante commented 6 days ago

Edit: In my case the issue was with the input flag, not imports in the input. Still a bug, but not a common usage. I resolved by running require.resolve first.

I think plugin-node-resolve is unable to resolve anything other than exports: './file.js' because I'm having similar issues with this exports array:

Could not resolve entry module "../../tmp/219e0b6c-a89a-45ab-9365-67aa8252356b/node_modules/webext-dynamic-content-scripts".

"exports": {
    ".": {
        "types": "./distribution/index.d.ts",
        "default": "./distribution/index.js"
    },
    "./including-active-tab.js": {
        "types": "./distribution/including-active-tab.d.ts",
        "default": "./distribution/including-active-tab.js"
    },
    "./utils.js": {
        "types": "./distribution/utils.d.ts",
        "default": "./distribution/utils.js"
    }
},

My config is just:

const bundle = await rollup({
    input,
    plugins: [
        rollupResolve(), // Also no luck with {exportConditions: ["node"]}
        commonjs(),
        cleanup(),
    ],
});
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"rollup": "^3.29.4",

Links