-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Simplify constant object access #1166
Comments
I believe that minifying this should be fine provided that the expression is guaranteed to have no side effects. Some edge cases to think about off the top of my head:
|
If it never minifies to undefined, the existance of My first version using maybeRewritePropertyAccess is quite simple and seems to work. It only handles property access and string indexer, though (number indexers get skipped: js_parser.go:10600). And direct access only. Tracking something that isn't an anonymous object would require quite a lot more work I bet...
ooh, terser does the wrong thing: export const computed = {
a: 'bad',
[String.fromCharCode(97)]: 'good' // equiv. ['a']: 'good'
}.a; // should be 'good' export const computed=["bad",String.fromCharCode(97)][0]; // 'bad' |
Good catch about terser! I see you have already filed this as terser/terser#980.
I believe you are correct. I was thinking of those rules in order and didn't realize the next rule eclipses that rule. |
I just thought of another case: console.log({
foo: function() { return this.bar },
bar: true,
}.foo()) It would be bad if that was changed to this: console.log(function() { return this.bar }()) |
Note: I opened this issue with a higher level question, which was how
{foo: 'bar'}.foo === 'bar'
should be minified to!0
, but this is due to the property accessor not simplifying. If this issue is fixed, then that statement should be minified for free (there is already constant string equality checking). The original issue is at the bottom.{ foo: 'bar' }.foo
should be minified to"bar"
.This should work with a fair amount of depth:
{ foo: { bar: 'baz' } }.foo.bar
should be minified to"baz"
.Would modifying js_parser maybeRewritePropertyAccess be the place for this to go?
Original issue is here. Click to expand.
console.log({foo: 'bar'}.foo === 'bar')
is not minified toconsole.log(!0)
esbuild does minify some other simple statements like this:
console.log('foo' === 'foo')
is minified toconsole.log(!0)
Terser minifies both statements.
My story: I was using Snowpack which uses
import.meta.env
to access the environment, e.g.import.meta.env.NODE_ENV
. (instead of process.env.) I added support for this in rollup using the resolveMetaImport plugin hook.My input:
Output from rollup which inlines the
import.meta.env
access:with esbuild's minification:
I was hoping that esbuild would recognise this pattern and remove the dead code. Terser's output in this case:
I understand that esbuild's minifier doesn't aim to support everything, so if that's the case here, all good!
The text was updated successfully, but these errors were encountered: