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

define and boolean elimination #2063

Closed
Conaclos opened this issue Feb 27, 2022 · 3 comments
Closed

define and boolean elimination #2063

Conaclos opened this issue Feb 27, 2022 · 3 comments

Comments

@Conaclos
Copy link

Conaclos commented Feb 27, 2022

Hi!

I would like to thank you again for esbuild and its excellent documentation (including the CHANGELOG).

I use esbuild as the main tool to build my libraries. Usually I have two builds:

  • a debug/test build
  • a production build (the published one)

In my code I generally extensively use assertions (pre- and post- conditions). I would like to remove these assertions in the production build. The only way I know to achieve it with esbuild is to use a constant such as DEBUG:

// @filename index.js
import { assert } from "assert-mod"
export function sqrt(x) {
  DEBUG && assert(x >= 0);
  return Math.sqrt(x);
}

and set DEBUG to false (or 0) for the production build:

esbuild --bundle index.js --define:DEBUG=false --format=esm --external:assert-mod

However, esbuild emits the following code:

import { assert } from "assert-mod";
function sqrt(x) {
  false;
  return Math.sqrt(x);
}
export {
  sqrt
};

Note the apparition of an expression-statement false;.

Do you think it could be possible to improve a bit the dead-code elimination in order to remove also the boolean constant? The emitted code could be:

function sqrt(x) {
  return Math.sqrt(x);
}
export {
  sqrt
};

Note that I would like to avoid minification to keep a readable code.

Alternative solution

Another solution could be to add a --drop:assert.

@evanw
Copy link
Owner

evanw commented Mar 1, 2022

The emitted code could be:

function sqrt(x) {
  return Math.sqrt(x);
}
export {
  sqrt
};

No. The external import of assert-mod must be kept because it may have side effects. Dropping that import would be incorrect.

@Conaclos
Copy link
Author

Conaclos commented Mar 2, 2022

@evanw

Ah yes I forgot to add the import. However, it is not the proposal of the issue: the proposal is to remove the useless boolean constant.

In other words, instead of emitting:

import { assert } from "assert-mod";
function sqrt(x) {
  false;
  return Math.sqrt(x);
}
export {
  sqrt
};

esbuild could emit:

import { assert } from "assert-mod";
function sqrt(x) {
  return Math.sqrt(x);
}
export {
  sqrt
};

Diff between the two versions:

import { assert } from "assert-mod";
function sqrt(x) {
-  false;
  return Math.sqrt(x);
}
export {
  sqrt
};

@evanw evanw closed this as completed in 0ceefac Mar 2, 2022
@Conaclos
Copy link
Author

Conaclos commented Mar 2, 2022

Thanks!

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

No branches or pull requests

2 participants