@@ -15,15 +15,15 @@ class DefinePlugin {
1515 }
1616
1717 apply ( compiler ) {
18- let definitions = this . definitions ;
18+ const definitions = this . definitions ;
1919 compiler . plugin ( "compilation" , ( compilation , params ) => {
2020 compilation . dependencyFactories . set ( ConstDependency , new NullFactory ( ) ) ;
2121 compilation . dependencyTemplates . set ( ConstDependency , new ConstDependency . Template ( ) ) ;
2222
2323 params . normalModuleFactory . plugin ( "parser" , ( parser ) => {
2424 ( function walkDefinitions ( definitions , prefix ) {
2525 Object . keys ( definitions ) . forEach ( ( key ) => {
26- let code = definitions [ key ] ;
26+ const code = definitions [ key ] ;
2727 if ( code && typeof code === "object" && ! ( code instanceof RegExp ) ) {
2828 walkDefinitions ( code , prefix + key + "." ) ;
2929 applyObjectDefine ( prefix + key , code ) ;
@@ -36,7 +36,7 @@ class DefinePlugin {
3636
3737 function stringifyObj ( obj ) {
3838 return "__webpack_require__.i({" + Object . keys ( obj ) . map ( ( key ) => {
39- let code = obj [ key ] ;
39+ const code = obj [ key ] ;
4040 return JSON . stringify ( key ) + ":" + toCode ( code ) ;
4141 } ) . join ( "," ) + "})" ;
4242 }
@@ -59,39 +59,57 @@ class DefinePlugin {
5959 }
6060
6161 function applyDefine ( key , code ) {
62- let isTypeof = / ^ t y p e o f \s + / . test ( key ) ;
62+ const isTypeof = / ^ t y p e o f \s + / . test ( key ) ;
6363 if ( isTypeof ) key = key . replace ( / ^ t y p e o f \s + / , "" ) ;
6464 let recurse = false ;
6565 let recurseTypeof = false ;
6666 code = toCode ( code ) ;
6767 if ( ! isTypeof ) {
6868 parser . plugin ( "can-rename " + key , ParserHelpers . approve ) ;
6969 parser . plugin ( "evaluate Identifier " + key , ( expr ) => {
70+ /**
71+ * this is needed in case there is a recursion in the DefinePlugin
72+ * to prevent an endless recursion
73+ * e.g.: new DefinePlugin({
74+ * "a": "b",
75+ * "b": "a"
76+ * });
77+ */
7078 if ( recurse ) return ;
71- let res = parser . evaluate ( code ) ;
79+ recurse = true ;
80+ const res = parser . evaluate ( code ) ;
7281 recurse = false ;
7382 res . setRange ( expr . range ) ;
7483 return res ;
7584 } ) ;
7685 parser . plugin ( "expression " + key , ParserHelpers . toConstantDependency ( code ) ) ;
7786 }
78- let typeofCode = isTypeof ? code : "typeof (" + code + ")" ;
87+ const typeofCode = isTypeof ? code : "typeof (" + code + ")" ;
7988 parser . plugin ( "evaluate typeof " + key , ( expr ) => {
89+ /**
90+ * this is needed in case there is a recursion in the DefinePlugin
91+ * to prevent an endless recursion
92+ * e.g.: new DefinePlugin({
93+ * "typeof a": "tyepof b",
94+ * "typeof b": "typeof a"
95+ * });
96+ */
8097 if ( recurseTypeof ) return ;
81- let res = parser . evaluate ( typeofCode ) ;
98+ recurseTypeof = true ;
99+ const res = parser . evaluate ( typeofCode ) ;
82100 recurseTypeof = false ;
83101 res . setRange ( expr . range ) ;
84102 return res ;
85103 } ) ;
86104 parser . plugin ( "typeof " + key , ( expr ) => {
87- let res = parser . evaluate ( typeofCode ) ;
105+ const res = parser . evaluate ( typeofCode ) ;
88106 if ( ! res . isString ( ) ) return ;
89107 return ParserHelpers . toConstantDependency ( JSON . stringify ( res . string ) ) . bind ( parser ) ( expr ) ;
90108 } ) ;
91109 }
92110
93111 function applyObjectDefine ( key , obj ) {
94- let code = stringifyObj ( obj ) ;
112+ const code = stringifyObj ( obj ) ;
95113 parser . plugin ( "can-rename " + key , ParserHelpers . approve ) ;
96114 parser . plugin ( "evaluate Identifier " + key , ( expr ) => new BasicEvaluatedExpression ( ) . setRange ( expr . range ) ) ;
97115 parser . plugin ( "evaluate typeof " + key , ParserHelpers . evaluateToString ( "object" ) ) ;
0 commit comments