@@ -9,6 +9,11 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
9
9
const ParserHelpers = require ( "./ParserHelpers" ) ;
10
10
const NullFactory = require ( "./NullFactory" ) ;
11
11
12
+ /** @typedef {import("./Compiler") } Compiler */
13
+ /** @typedef {import("./Parser") } Parser */
14
+ /** @typedef {null|undefined|RegExp|Function|string|number } CodeValuePrimitive */
15
+ /** @typedef {CodeValuePrimitive|Record<string, CodeValuePrimitive>|RuntimeValue } CodeValue */
16
+
12
17
class RuntimeValue {
13
18
constructor ( fn , fileDependencies ) {
14
19
this . fn = fn ;
@@ -37,6 +42,12 @@ const stringifyObj = (obj, parser) => {
37
42
) ;
38
43
} ;
39
44
45
+ /**
46
+ * Convert code to a string that evaluates
47
+ * @param {CodeValue } code Code to evaluate
48
+ * @param {Parser } parser Parser
49
+ * @returns {string } code converted to string that evaluates
50
+ */
40
51
const toCode = ( code , parser ) => {
41
52
if ( code === null ) {
42
53
return "null" ;
@@ -60,6 +71,10 @@ const toCode = (code, parser) => {
60
71
} ;
61
72
62
73
class DefinePlugin {
74
+ /**
75
+ * Create a new define plugin
76
+ * @param {Record<string, CodeValue> } definitions A map of global object definitions
77
+ */
63
78
constructor ( definitions ) {
64
79
this . definitions = definitions ;
65
80
}
@@ -68,6 +83,11 @@ class DefinePlugin {
68
83
return new RuntimeValue ( fn , fileDependencies ) ;
69
84
}
70
85
86
+ /**
87
+ * Apply the plugin
88
+ * @param {Compiler } compiler Webpack compiler
89
+ * @returns {void }
90
+ */
71
91
apply ( compiler ) {
72
92
const definitions = this . definitions ;
73
93
compiler . hooks . compilation . tap (
@@ -79,7 +99,18 @@ class DefinePlugin {
79
99
new ConstDependency . Template ( )
80
100
) ;
81
101
102
+ /**
103
+ * Handler
104
+ * @param {Parser } parser Parser
105
+ * @returns {void }
106
+ */
82
107
const handler = parser => {
108
+ /**
109
+ * Walk definitions
110
+ * @param {Object } definitions Definitions map
111
+ * @param {string } prefix Prefix string
112
+ * @returns {void }
113
+ */
83
114
const walkDefinitions = ( definitions , prefix ) => {
84
115
Object . keys ( definitions ) . forEach ( key => {
85
116
const code = definitions [ key ] ;
@@ -98,6 +129,12 @@ class DefinePlugin {
98
129
} ) ;
99
130
} ;
100
131
132
+ /**
133
+ * Apply define key
134
+ * @param {string } prefix Prefix
135
+ * @param {string } key Key
136
+ * @returns {void }
137
+ */
101
138
const applyDefineKey = ( prefix , key ) => {
102
139
const splittedKey = key . split ( "." ) ;
103
140
splittedKey . slice ( 1 ) . forEach ( ( _ , i ) => {
@@ -108,6 +145,12 @@ class DefinePlugin {
108
145
} ) ;
109
146
} ;
110
147
148
+ /**
149
+ * Apply Code
150
+ * @param {string } key Key
151
+ * @param {CodeValue } code Code
152
+ * @returns {void }
153
+ */
111
154
const applyDefine = ( key , code ) => {
112
155
const isTypeof = / ^ t y p e o f \s + / . test ( key ) ;
113
156
if ( isTypeof ) key = key . replace ( / ^ t y p e o f \s + / , "" ) ;
@@ -181,6 +224,12 @@ class DefinePlugin {
181
224
} ) ;
182
225
} ;
183
226
227
+ /**
228
+ * Apply Object
229
+ * @param {string } key Key
230
+ * @param {Object } obj Object
231
+ * @returns {void }
232
+ */
184
233
const applyObjectDefine = ( key , obj ) => {
185
234
parser . hooks . canRename
186
235
. for ( key )
0 commit comments