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
Option to remove "use strict"; #128
Comments
The problem is that "use strict" introduces certain semantics, which actually makes it harder in some cases to remove it. |
Could you give me an example ? (I'm still quite new to Javascript ^^) Anyway for the moment I'm removing the "use strict"; statement as a step of my build process, before using uglifyjs. I just thought that it might be an option not too hard to add and that a few people might like to have. |
SafetyIt's not trivial to check for cases like the following, where it's not safe to remove "use strict";
function Foo() {
if (typeof this === 'undefined') return new Foo();
this.val = 1;
}
Foo.prototype.getVal = function () {
return this.val;
};
// the following then works in strict mode
// but leaks a global variable `val` in non-strict
// mode
Foo().getVal(); PerformanceThe other reason not to do this is performance. As such, removing |
@ForbesLindesay thanks for taking the time to explain. We could have this as an option but I've no interest to do it; will take a patch if someone bothers to write it. |
Thanks a lot for the explanation, I'll need to grab a good javascript book one day to learn those things :) |
Would it make sense to remove multiple |
After the initial |
It's safe to remove duplicate directives, but not different ones like @ForbesLindesay suggests. |
There are no other valid directives: function foo() {
"foo bar";
return 5;
} is exactly equivalent to: function foo() {
return 5;
} The only string literal (that I'm aware of) which has special meaning is |
@ForbesLindesay There is also "use asm" (see http://asmjs.org/spec/latest/) |
@ForbesLindesay: any string in directive position may be meaningful and must be preserved. |
@Intyuh thanks, I wasn't aware of that directive. We could add exceptions for any that are potentially meaningful. http://ecma-international.org/ecma-262/5.1/#sec-14.1 States:
Which means only string literals at the beginning of a function body suffer the problem of potentially being directives. It also does indeed goes on to state:
so implementations can define implementation specific meanings as @michaelficarra suggests. It's worth noting that only implementations can define such meaning, not users. Essentially, providing you consider the list of known directives (and keep that up to date), it's no less safe than mangling variable names, since someone is always able to call As a final point, pretty much no code actually relies on these directives. For example, code may actually run faster if the |
|
So is there any option to prevent UglifyJS from adding "use strict"; in the beginning of the file? I just don't feel safe knowing that my script will enable strict mode globally for anyone who will use it. i have placed "use strict"; manually to the place where it belongs in my code, but it gets ripped of and placed in the beginning, which is not good |
No, there is no option to prevent directives from printing See https://github.com/mishoo/UglifyJS2/blob/master/lib/output.js#L690 or lookup |
(a bit of creative code, automation or manual removal could do the trick anyway) |
Hi,
As the title suggest, it would be really nice to have an option switch to enable removing all the "use strict"; of the output.
For instance in my project, I go from 36k to 35k. It's not much, but it's still a few bytes :) (and shouldn't be too complicated to add I guess. I haven't looked at the Uglify code yet, but I find the time, I'll try to add that)
The text was updated successfully, but these errors were encountered: