terserの圧縮大法--magle-props
4077 ワード
――magle-propsは本当に天に逆らう機能です.
CLI mangling property names(--magle-props)Note:THIS WILL BREAK YOUR CODE.A good rule of thumb is not to use this unless you know exactly what you're dong and how this works and read this section end.
Mangling property names is a separate step,different from variable name mangling.Pass--magle-props to enable it.The least dangerous way to use this to use the regex option like so:
terser example.js-c-m--mandle-props regex=/uThis will magle all properties that start with an undersscore.So you can use it to magle internal methods.
By default、it will magle all properties in the input code with the exception of built in DOM properties and properties in core JavaScript clases、which is what will brek your code if you don't:
Control all the code you're mangling Avoid using a module bundler,as they usually will Terser on each file individually,making it impossible to pass magmagmagmagmagmags bets modules.Avodcalling funce Profidencebecause they refer to object properties stings and will break your code if you don't know what you are dong.An example:
//example.jsvar x={
$terser example.js-c-m--magle-propsvar x={o:0,u:1,l:function(){return this.u+this.o}x.t=2,x.o=3,consolone(x.l();Mangle all properties except for reerved properties(still very unsafe):
terser example.js-c-m--magle-props reerved=[foo_,bar_]var x={o:0,foo_:1,_:function(){return this.foou+this.o}x.bar_=2,x.o=3,consolie.log(x.u();Mangle all properties matching a regex(not as unsafe but still unsafe):
$terser example.js-c-m--magle-props regex=/uvar x={o:0,_:1,canction(){return this._+this.o}x.l=2,x.o=3,consolone(x.calc();Commbining magle properties options:
$terser example.js-c-m--mandle-props regex=/u/rerrerved=[bar_]var x={o:0,_:1,canction(){return this._+this.o}x.bar_=2,x.o=3,consolie.log(x.calc);In order for this to be of any use,we avoid magling standard JS names by default(--magle-props builtions to override)
A default exclusion file is provided in tools/domprops.json which shoud cover most standard JS and DOM properties defined in various browsers.Pass--magle-props domprops to disable this feat.
A reglar expression can be used to define which property names shoruld be magled.For example,--magle-props regex=/^_/will only magle property names that start with an undersscore.
When you comppress multiiple files using this option、in order for them to work together in the inininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininstststststststststststststaaaaaaaaaaaaaaaaaaExample:
rm-f/tmp/cachche.jsonstartfresh$terser file 1.js file 2.js--magle-props--name-cache/tmpmp/cachche.json-mantpart 1.js.js.terser file3.js file4.js--maggle-props--magle-props--name-props--name-cachcheeeeeem/2.cacheeeeeem 2.cachchchem/2.cachchchchem.jjtttttttttttttttttttttttttcachem/cachchem/name.
Using the name cache is not necessary if you commpress all your files in a single call to Terser.
Mangling unquot;ted names(--magle-props keep Gated)Using quot;ted property name(o[foo])reerves the propertyname so that it is not magled throughot the entire script even when stune.eyle.eyle.
//stuff.jsvar o={
// stuff.js
var o = {
"foo": 1,
bar: 3
};
o.foo += o.bar;
console.log(o.foo);
圧縮後、【コマンドライン:terser.//zpageA.js-o.js--magle-props】はこうなりました.var o={o:1,l:3};o.o+=o.l;console.log(o.o);
ナイマの本来の属性は全部なくなりました.他のオプションがあります.例えば、引用符の属性があるかどうかは保護されます.一緒に正則を合わせます.CLI mangling property names(--magle-props)Note:THIS WILL BREAK YOUR CODE.A good rule of thumb is not to use this unless you know exactly what you're dong and how this works and read this section end.
Mangling property names is a separate step,different from variable name mangling.Pass--magle-props to enable it.The least dangerous way to use this to use the regex option like so:
terser example.js-c-m--mandle-props regex=/uThis will magle all properties that start with an undersscore.So you can use it to magle internal methods.
By default、it will magle all properties in the input code with the exception of built in DOM properties and properties in core JavaScript clases、which is what will brek your code if you don't:
Control all the code you're mangling Avoid using a module bundler,as they usually will Terser on each file individually,making it impossible to pass magmagmagmagmagmags bets modules.Avodcalling funce Profidencebecause they refer to object properties stings and will break your code if you don't know what you are dong.An example:
//example.jsvar x={
baz_: 0,
foo_: 1,
calc: function() {
return this.foo_ + this.baz_;
}
}x.bar_=2.x["baz_]=3;consolie.logs(x.calc);Mangle all properties(except for JavaScript builtins)(very unsafe):$terser example.js-c-m--magle-propsvar x={o:0,u:1,l:function(){return this.u+this.o}x.t=2,x.o=3,consolone(x.l();Mangle all properties except for reerved properties(still very unsafe):
terser example.js-c-m--magle-props reerved=[foo_,bar_]var x={o:0,foo_:1,_:function(){return this.foou+this.o}x.bar_=2,x.o=3,consolie.log(x.u();Mangle all properties matching a regex(not as unsafe but still unsafe):
$terser example.js-c-m--magle-props regex=/uvar x={o:0,_:1,canction(){return this._+this.o}x.l=2,x.o=3,consolone(x.calc();Commbining magle properties options:
$terser example.js-c-m--mandle-props regex=/u/rerrerved=[bar_]var x={o:0,_:1,canction(){return this._+this.o}x.bar_=2,x.o=3,consolie.log(x.calc);In order for this to be of any use,we avoid magling standard JS names by default(--magle-props builtions to override)
A default exclusion file is provided in tools/domprops.json which shoud cover most standard JS and DOM properties defined in various browsers.Pass--magle-props domprops to disable this feat.
A reglar expression can be used to define which property names shoruld be magled.For example,--magle-props regex=/^_/will only magle property names that start with an undersscore.
When you comppress multiiple files using this option、in order for them to work together in the inininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininstststststststststststststaaaaaaaaaaaaaaaaaaExample:
rm-f/tmp/cachche.jsonstartfresh$terser file 1.js file 2.js--magle-props--name-cache/tmpmp/cachche.json-mantpart 1.js.js.terser file3.js file4.js--maggle-props--magle-props--name-props--name-cachcheeeeeem/2.cacheeeeeem 2.cachchchem/2.cachchchchem.jjtttttttttttttttttttttttttcachem/cachchem/name.
Using the name cache is not necessary if you commpress all your files in a single call to Terser.
Mangling unquot;ted names(--magle-props keep Gated)Using quot;ted property name(o[foo])reerves the propertyname so that it is not magled throughot the entire script even when stune.eyle.eyle.
//stuff.jsvar o={
"foo": 1,
bar: 3
}o.foo+=o.barconsolie.log(o.foo);terser stuff.js--magle-props keep_quot ted-c-mvar={foo:1,o:3}o.foo+=o.o,consone.log(o.foo);