You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently using to esbuild to hook into node's module loader via esbuild-register to compile files on demand when they are loaded in node. Internally, it relies on transformSync to transpile files sent by node's internal module loader.
The app is mainly written in commonjs, but loads some esm files via dynamic import statements. To allow node to load esm files from commonjs one must use the dynamic import statement. In esbuild this is supported via the --platform=node flag in cli or via a build property of the same name for build and buildSync APIs.
But no such option exists for transform or transformSync. Those APIs only support setting format=cjs which transpiles every import, including dynamic import statements. That's great for browsers but unwanted in node environments. So there needs to be some way to preserve dynamic import statements via transform and transformSync.
Input code:
// inputexportconstfoo=()=>import('./bar.mjs')
Expected output code:
constfoo=()=>import("./bar.mjs")));
Actual output code:
// ...snip bunch of esm interop codeconstfoo=()=>Promise.resolve().then(()=>__toModule(require("./bar.mjs")));
The text was updated successfully, but these errors were encountered:
I think at some point when I was writing this I didn't realize that import() was a regular function in script mode. Also the TypeScript compiler converts import() to require() when the output format is CommonJS, so I have done that as well. I agree that this would be something good to change. However, this seems like a breaking change to me, so it should wait until the next breaking change release.
I'm currently using to
esbuild
to hook into node's module loader viaesbuild-register
to compile files on demand when they are loaded in node. Internally, it relies ontransformSync
to transpile files sent by node's internal module loader.The app is mainly written in commonjs, but loads some esm files via dynamic import statements. To allow node to load esm files from commonjs one must use the dynamic import statement. In esbuild this is supported via the
--platform=node
flag in cli or via a build property of the same name forbuild
andbuildSync
APIs.But no such option exists for
transform
ortransformSync
. Those APIs only support settingformat=cjs
which transpiles every import, including dynamic import statements. That's great for browsers but unwanted in node environments. So there needs to be some way to preserve dynamic import statements viatransform
andtransformSync
.Input code:
Expected output code:
Actual output code:
The text was updated successfully, but these errors were encountered: