-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Using --format=esm and --external:package produces code containing require() calls for package #566
Comments
This transformation isn't done automatically because it's impossible in the general case to preserve the semantics of the original code when you do this. It's a lossy transformation. Evaluation order would be changed and conditional imports would be changed into unconditional imports. One way to fix this if you're running the code in node is to bring back the import { createRequire } from 'module'
global.require = createRequire(import.meta.url)
import('./your-code.mjs') If you are ok with changing the semantics of your code, another way to work around this is to use a plugin to convert const externalCjsToEsmPlugin = external => ({
name: 'external',
setup(build) {
let escape = text => `^${text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}$`
let filter = new RegExp(external.map(escape).join('|'))
build.onResolve({ filter: /.*/, namespace: 'external' }, args => ({
path: args.path, external: true
}))
build.onResolve({ filter }, args => ({
path: args.path, namespace: 'external'
}))
build.onLoad({ filter: /.*/, namespace: 'external' }, args => ({
contents: `export * from ${JSON.stringify(args.path)}`
}))
},
}) You would use it like this: require('esbuild').build({
bundle: true,
outdir: 'bundle',
format: 'esm',
target: 'es2017',
entryPoints: ['./src/index.ts'],
plugins: [externalCjsToEsmPlugin(['react', 'react-dom'])],
}) |
Interesting, although the package in the repro ( |
I tried the plugin solution here and it works well My use case was creating a bundle without any dependency apart from peer dependencies |
Hi @evanw , could you elaborate how this plugin can "convert require calls to import statements"? I thought it would change
to
but the plugin seems to only change the content of the dependency lib to export statement. I don't quite understand how this would help to solve the problem in this thread. Thanks! |
@buronnie, basically esbuild replaces the lib contents with an esm export from the external package, this means that there won't be any require calls in the bundle If you want to try this yourself i published a plugin to do this https://github.com/remorses/esbuild-plugins/tree/master/esm-externals |
Thank you @remorses. This works well for both "require cjs module" and "import es module". But it doesn't work for "import cjs module" (e.g. |
I also notice that this plugin doesn't seem to work for
will be transpiled to
instead |
Just a note that this also affects requires of Node standard libraries in builds where |
I have the same problem. Absolutely, it's " instead lodash.default.upperCase("abc") will work" ,but I want " "lodash.upperCase("abc") " . . how to achieve it . |
Using
--format=esm
and--external:package
produces code containing require() calls forpackage
I would like to have require calls on the external package to be converted to ES imports instead of require calls, to make the bundle valid ESM code
Reproduction: https://github.com/remorses/reproduction-esbuild-require-in-esm-with-externals
The text was updated successfully, but these errors were encountered: