You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
properties are not mangled if it's in a deferred top level object #397
In a real case, if a Bar is defined in some later files, I want to pre-define Bar to make terser uglifiy this name, and I also want to uglify all its properties. But I don't like to pass all input files to terser during one calling to .minify
I'm using Gulp, and the file which defines Bar is in another task
currently, I add an extra step to call terser.minify("var Bar = {};") by myself, and abort its output
so that the gulp task's output keeps smallest.
This usage works well under terser v3.10.*, and something becames wrong since terser v3.14.1 .
I've read #219 , but I think my usage is still worth a new if-branch.
changed the title [-][bug] some properteis are not mangled in some cases[/-][+][bug] some properteis are not mangled if it's in a top level object which is also mangled[/+]on Jul 9, 2019
changed the title [-][bug] some properteis are not mangled if it's in a top level object which is also mangled[/-][+]some properteis are not mangled if it's in a deferred top level object[/+]on Jul 9, 2019
The old issue message is here: it's too long and somehow incorrect, so I move it here just to archive it.
description
When I compiled Vimium C at commit gdh1995/vimium-c@c7cb108 using different versions of terser, the terser v4.1.2 cause null pointer exception, and after debugging, I found some properties are not mangled unexpectedly. After further tests, I've confirmed:
all versions since v3.14.1, including v3.16.0, v3.16.1, v3.17.0, v4.1.2, have such a bug
while v3.14.0, v3.13.1, v3.11.0, v3.10.12 and v3.10.11 works well (at least only caring about the function for mangling properties)
I tested removing it in v3.14.1 but this bug still exists.
error details
Vimium C has quite a few steps to compile it:
firstly, compile all TypeScript scripts to JavaScript and save them in a folder of dist/.build/
the gulp task is gulp build/ts
the next step is to uglify files in the dist/.build/lib and dist/.build/content folder
the gulp task which uses terser is named "min/content"
in this step, terser generates good results (at least all wanted properties are mangled)
the third step is to uglify files in dist/.build/background, and some other steps are for dist/.build/pages and dist/.build/front
the gulp tasks are min/bg and min/others
here, all config data passed to terser is just the same as the task min/content in the step 2,
except that I load a "nameCache" dict and setup config.nameCache to it,
the cache looks like { vars: {...}, props: {...}, timestamp: 0 } - vars and props are from terser's output in step 2, and they will be re-wrapped and passed to terser.
And:
I removed the extra nameCache by running ENABLE_NAME_CACHE=0 DEBUG=1 gulp min/bg, this bug also occurred.
I tried modifing Gulpfile.js and runs min/bg ignoring min/content, but it was still there.
changed the title [-]some properteis are not mangled if it's in a deferred top level object[/-][+]properties are not mangled if it's in a deferred top level object[/+]on Jul 9, 2019
What you described is not exactly a bug. Terser has no way of knowing whether lowercase bar is a property that exists.
What I found though, is that if I create another artificial file and use Bar.bar in any way, Terser does not "find out" that bar is a property, so it doesn't minify .bar in the third code example. Like this:
#!/usr/bin/env node
"use strict";
var opts = {
"mangle": {
"properties": true,
"toplevel": true
},
nameCache: { vars: {}, props: {} }
};
var terser = require('./dist/bundle.js');
terser.minify('var Bar = {};', opts);
terser.minify('Bar.bar = 123', opts); // <- this is what I added
console.log(opts.nameCache)
var result = terser.minify('var Foo = { foo: function() { return Bar.bar() } };', opts);
var expected = "var r={o:function(){return a.v()}};";
console.log("[task] test uglifying external properties",
"\n[Current ]", result.code, "\n[Expected]", expected);
I suppose this is the bug you're trying to report. In that case, sounds like an interesting bug. And indeed I want this to be a Terser feature. When this is fixed I'll ensure there's a test that protects your use case.
varconfig={"compress": {"properties": true,"pure_getters": true,"toplevel": false,},"mangle": {"properties": {"regex": /^_|_$/},"toplevel": true,"reserved": [// # expected names in web-extension content"WeakSet","Set",// # expected names in 3rd-party extensions' contents"requestIdleCallback",// # content global names:"browser","VimiumInjector","VDom","VKey","VHints","VScroller","VOmni","VFind","VVisual","VMarks","VHud","VPort","VEvent",// ...],// I've ensured all other global names of my code MUST start / end with "_",// and they are mangled correctly by terser v3.10.*.}};
And as said in the comment, only those global variables name which starts/ends with "_" will be mangled, so I wonder if terser may think "all matched global names and their properties visits should be uglified" when it knows:
"not all" properties are expected to be mangled
or, some global variables have been mangled, and pure_getters is true.
Activity
build: revert terser to v3.10.13
[-][bug] some properteis are not mangled in some cases[/-][+][bug] some properteis are not mangled if it's in a top level object which is also mangled[/+][-][bug] some properteis are not mangled if it's in a top level object which is also mangled[/-][+]some properteis are not mangled if it's in a deferred top level object[/+]gdh1995 commentedon Jul 9, 2019
The old issue message is here: it's too long and somehow incorrect, so I move it here just to archive it.
description
When I compiled Vimium C at commit gdh1995/vimium-c@c7cb108 using different versions of terser, the terser v4.1.2 cause null pointer exception, and after debugging, I found some properties are not mangled unexpectedly. After further tests, I've confirmed:
I tested removing it in v3.14.1 but this bug still exists.
error details
Vimium C has quite a few steps to compile it:
dist/.build/gulp build/tsdist/.build/libanddist/.build/contentfolderterseris named "min/content"tersergenerates good results (at least all wanted properties are mangled)dist/.build/background, and some other steps are fordist/.build/pagesanddist/.build/frontmin/bgandmin/othersmin/contentin the step 2,except that I load a "
nameCache" dict and setupconfig.nameCacheto it,{ vars: {...}, props: {...}, timestamp: 0 }-varsandpropsare from terser's output in step 2, and they will be re-wrapped and passed to terser.And:
ENABLE_NAME_CACHE=0 DEBUG=1 gulp min/bg, this bug also occurred.Gulpfile.jsand runsmin/bgignoringmin/content, but it was still there.[-]some properteis are not mangled if it's in a deferred top level object[/-][+]properties are not mangled if it's in a deferred top level object[/+]fabiosantoscode commentedon Jul 16, 2019
Hey there!
Nice extension :)
What you described is not exactly a bug. Terser has no way of knowing whether lowercase
baris a property that exists.What I found though, is that if I create another artificial file and use
Bar.barin any way, Terser does not "find out" thatbaris a property, so it doesn't minify.barin the third code example. Like this:I suppose this is the bug you're trying to report. In that case, sounds like an interesting bug. And indeed I want this to be a Terser feature. When this is fixed I'll ensure there's a test that protects your use case.
Thanks for your report!
gdh1995 commentedon Jul 17, 2019
Thanks.
idea 1
If it's hard to tell whether a variable is from outsides, I think a new option may be suitable to leave this problem to final users.
idea 2
For the project I mentioned above, it's in TypeScript and I've applied many "strict" rules, so I have a terser config looking like this (from https://github.com/gdh1995/vimium-c/blob/master/scripts/uglifyjs.dist.json):
And as said in the comment, only those global variables name which starts/ends with "_" will be mangled, so I wonder if terser may think "all matched global names and their properties visits should be uglified" when it knows:
pure_gettersis true.