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

Math is a side-effect #3988

Open
thetarnav opened this issue Nov 26, 2024 · 5 comments
Open

Math is a side-effect #3988

thetarnav opened this issue Nov 26, 2024 · 5 comments

Comments

@thetarnav
Copy link

thetarnav commented Nov 26, 2024

constants with math operations are not being tree-shaken

INPUT

const FOO = 1 + 2

OUTPUT

// entry.js
var FOO = 1 + 2;

https://esbuild.github.io/try/#YgAwLjI0LjAALS1idW5kbGUgLS1mb3JtYXQ9ZXNtAGUAZW50cnkuanMAY29uc3QgRk9PID0gMSArIDI

Which in my case leaves a bunch of unused constants from imported but unused libraries.

// lib/memory.mjs
var REG_SIZE = 4;
var STRING_SIZE = 2 * REG_SIZE;

// vendor/pako/pako.js
var MIN_MATCH$1 = 3;
var MAX_MATCH$1 = 258;
var LENGTH_CODES$1 = 29;
var LITERALS$1 = 256;
var L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
var D_CODES$1 = 30;
var HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
var static_ltree = /* @__PURE__ */ new Uint32Array((L_CODES$1 + 2) * 2);
var static_dtree = /* @__PURE__ */ new Uint32Array(D_CODES$1 * 2);
var _length_code = /* @__PURE__ */ new Uint32Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
var LENGTH_CODES = 29;
var LITERALS = 256;
var L_CODES = LITERALS + 1 + LENGTH_CODES;
var HEAP_SIZE = 2 * L_CODES + 1;
var MIN_MATCH = 3;
var MAX_MATCH = 258;
var MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;

// site/git.ts
var UTF_0 = 0 + 48;
var UTF_1 = 1 + 48;
CODE

And there is no way to prevent this without making them to an iife afaik.

@ukslim
Copy link

ukslim commented Dec 10, 2024

I that in fact it constant-folds and tree-shakes the first constant (in a file?) but does not continue doing that for subsequent constants.

In this playground

  • esbuild tree-shakes an unused constant with a literal number value
  • esbuild tree-shakes the first unused constant with arithmetic value
  • esbuild neither constant-folds nor tree-shakes the next two unused constants with arithmetic value

@ukslim
Copy link

ukslim commented Dec 10, 2024

Bonus discovery - it works when the expression is +, - or all the bitwise operators I've tried.

It just fails for * and / !

Playground

@ukslim
Copy link

ukslim commented Dec 10, 2024

@thetarnav Your playground works OK if you add --minify.

When minifying, esbuild will perform constant-folding, reducing your const FOO = 1 + 2 to const FOO = 3, and then that gets tree-shaken. Constant-folding doesn't happen unless minifying.

However, per my comments, this doesn't happen for * and / -- I wonder whether it should be reported as a separate bug?

@thetarnav
Copy link
Author

My issue is mainly about --tree-shaking but thanks for letting me know that it disappears with --minify on. So yeah that would be a separate bug.

@boarush
Copy link

boarush commented Dec 15, 2024

I think constant-folding even when --minify is passed does not happen for * and / because there might be no guarantees for loss of precision?

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

3 participants