Skip to content
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

Passing objects or arrays for node globals to define produces invalid code #1080

Closed
remorses opened this issue Mar 29, 2021 · 1 comment
Closed

Comments

@remorses
Copy link
Contributor

remorses commented Mar 29, 2021

This is a regression happened in 0.11.0

Compiling the following code

console.log(module.x)

With

const { build } = require('esbuild')

build({
   entryPoints: ['entry.js'],
    define: {
        module: '{}',
    },
    format: 'esm',
    outdir: 'out',
})

Produces

var __commonJS = (cb, mod) => () => (mod || cb((mod = {exports: {}}).exports, mod), mod.exports);
var require_entry = __commonJS((exports, module) => {
  init_define_module();
  console.log("ciao");
  console.log(module.x);
});
export default require_entry();

Which will give init_define_module is not defined when run

If you instead pass a primitive value to define (for example a string or number), module.x will remain module.x (no replacement)

@evanw evanw closed this as completed in 6cbfaca Mar 29, 2021
@evanw
Copy link
Owner

evanw commented Mar 29, 2021

Thanks very much for reporting this. The problem here is that the definition file is being removed from the bundle because it's unused, but the call to the absent wrapper function is still being generated. This wasn't a problem with CommonJS wrapper closures because they are always considered to have side effects and tree shaking is disabled for them, but the new ESM wrapper closures still support tree shaking and can actually be completely removed. The fix will be in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants