Open Paul-Browne opened 6 months ago
To achieve something similar to what you're asking for, I'm using esbuild to generate the ESM bundle, then feeding it to rollup to produce other formats like CJS and IIFE (which will reduce some code of preserving ESM bindings semantics).
Yeah, i would really want to avoid having to involve another bundler...
another option is to proxy the import and export it as a module.exports
// proxy.cjs
import awesomeFunction from "foo.js";
module.exports = awesomeFunction;
// bundle.js
await esbuild.build({
entryPoints: ['proxy.cjs'],
bundle: true,
minify: true,
format: "iife",
target: "esnext",
globalName: "awesomeFunction",
outfile: 'foo.min.js'
})
...or, this is even possible without the proxy.cjs.
By using stdin
await esbuild.build({
stdin: {
contents: 'import a from "./foo.js";module.exports = a;',
resolveDir: '.'
},
bundle: true,
minify: true,
sourcemap: false,
format: "iife",
target: "esnext",
globalName: "awesomeFunction",
outfile: 'foo.min.js'
})
in the browser
window.awesomeFunction
is the object{__esModule: true}
, where aswindow.awesomeFunction.default
is the actual function. I understand why this is the case, but the solution from when the issue was mention earlier was to change the exported function type tomodule.exports
. But wouldn't it be nicer/more convienient if there was an option in the builder to assign the default export directly to the globalName?...Or, is there a way to do this already, without having to change the import
Extending on this... Maybe an option for using the name of the function (if it has been named) from the import as the
globalName