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

Constant folding does not work on polymorphic equality test #2895

Closed
pyrocat101 opened this issue Feb 6, 2023 · 0 comments
Closed

Constant folding does not work on polymorphic equality test #2895

pyrocat101 opened this issue Feb 6, 2023 · 0 comments

Comments

@pyrocat101
Copy link

Repro

// index.js
let value;
if (process.env.USE === 'foo') {
  value = require('./foo');
} else {
  value = require('./bar');
}
console.log(value);

// foo.js
module.exports = 'foo';

// bar.js
module.exports = 'bar';

Command:

esbuild --bundle index.js '--define:process.env.USE=null' --format=esm

Expected output

foo.js not to be bundled, since null === 'foo' should always evaluate to false.

Actual output

// ...[omitted]
// index.js
var value;
if (null === "foo") {
  value = require_foo();
} else {
  value = require_bar();
}
console.log(value);

However, if the following command is used instead:

esbuild --bundle index.js '--define:process.env.USE="null"' --format=esm

then the constant folding is correctly applied to the if conditions as well as the branch block.

// ...[omitted]
// index.js
var value;
if (false) {
  value = null;
} else {
  value = require_bar();
}
console.log(value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants