What problem does this feature solve?
When a CJS file requires an external module commonly builtin modules like node:path, Error: Dynamic require of "*" is not supported is thrown for the output bundle.
Reproduction steps
- Open https://stackblitz.com/edit/github-xv4yss-2sb9fe?file=rolldown.config.mjs,src%2Fentry.js,src%2Fa.cjs
npm i runs automatically
npm run build runs automatically
- run
node dist/entry.js. It errors with Error: Dynamic require of "node:path" is not supported.
- run
npm run build:up to use rollup
- run
node dist/entry.js. It runs successfully.
Workarounds
I'm setting
banner: "import { createRequire as ___createRequire } from 'module'; const require = ___createRequire(import.meta.url);",
like when I encounter evanw/esbuild#1921.
What does the proposed API look like?
For platform: 'node', it should be safe to use import module from 'node:module'; const require = module.createRequire(import.meta.url) for it.
For other platforms, I guess converting require into import is what users want in most cases. This may lead to crash in some cases due to incompatibility of module.exports / export *, so probably we need an option to control the interop code like requireReturnsDefault option in @rollup/plugin-commonjs.
For any platforms, I guess the node builtins can be treated as it's safe to convert require into import even if it's externalized. probably this isn't safe due to hoisting?
What problem does this feature solve?
When a CJS file
requires an external module commonly builtin modules likenode:path,Error: Dynamic require of "*" is not supportedis thrown for the output bundle.Reproduction steps
npm iruns automaticallynpm run buildruns automaticallynode dist/entry.js. It errors withError: Dynamic require of "node:path" is not supported.npm run build:upto use rollupnode dist/entry.js. It runs successfully.Workarounds
I'm setting
like when I encounter evanw/esbuild#1921.
What does the proposed API look like?
For
platform: 'node', it should be safe to useimport module from 'node:module'; const require = module.createRequire(import.meta.url)for it.For other
platforms, I guess convertingrequireintoimportis what users want in most cases. This may lead to crash in some cases due to incompatibility ofmodule.exports/export *, so probably we need an option to control the interop code likerequireReturnsDefaultoption in @rollup/plugin-commonjs.For anyprobably this isn't safe due to hoisting?platforms, I guess the node builtins can be treated as it's safe to convertrequireintoimporteven if it's externalized.