5
5
6
6
"use strict" ;
7
7
8
- const path = require ( "path" ) ;
8
+ const {
9
+ approve,
10
+ evaluateToIdentifier,
11
+ evaluateToString,
12
+ toConstantDependency
13
+ } = require ( "../JavascriptParserHelpers" ) ;
14
+ const NullFactory = require ( "../NullFactory" ) ;
15
+ const RuntimeGlobals = require ( "../RuntimeGlobals" ) ;
16
+
9
17
const AMDDefineDependency = require ( "./AMDDefineDependency" ) ;
18
+ const AMDDefineDependencyParserPlugin = require ( "./AMDDefineDependencyParserPlugin" ) ;
10
19
const AMDRequireArrayDependency = require ( "./AMDRequireArrayDependency" ) ;
11
20
const AMDRequireContextDependency = require ( "./AMDRequireContextDependency" ) ;
21
+ const AMDRequireDependenciesBlockParserPlugin = require ( "./AMDRequireDependenciesBlockParserPlugin" ) ;
12
22
const AMDRequireDependency = require ( "./AMDRequireDependency" ) ;
13
23
const AMDRequireItemDependency = require ( "./AMDRequireItemDependency" ) ;
24
+ const {
25
+ AMDDefineRuntimeModule,
26
+ AMDOptionsRuntimeModule
27
+ } = require ( "./AMDRuntimeModules" ) ;
28
+ const ConstDependency = require ( "./ConstDependency" ) ;
14
29
const LocalModuleDependency = require ( "./LocalModuleDependency" ) ;
15
30
const UnsupportedDependency = require ( "./UnsupportedDependency" ) ;
16
31
17
- const NullFactory = require ( "../NullFactory" ) ;
18
-
19
- const AMDDefineDependencyParserPlugin = require ( "./AMDDefineDependencyParserPlugin" ) ;
20
- const AMDRequireDependenciesBlockParserPlugin = require ( "./AMDRequireDependenciesBlockParserPlugin" ) ;
21
-
22
- const AliasPlugin = require ( "enhanced-resolve/lib/AliasPlugin" ) ;
23
-
24
- const {
25
- approve,
26
- evaluateToIdentifier,
27
- evaluateToString,
28
- toConstantDependency
29
- } = require ( "../JavascriptParserHelpers" ) ;
32
+ /** @typedef {import("../../declarations/WebpackOptions").ModuleOptions } ModuleOptions */
33
+ /** @typedef {import("../Compiler") } Compiler */
30
34
31
35
class AMDPlugin {
36
+ /**
37
+ * @param {ModuleOptions } options the plugin options
38
+ * @param {Record<string, any> } amdOptions the AMD options
39
+ */
32
40
constructor ( options , amdOptions ) {
33
- this . amdOptions = amdOptions ;
34
41
this . options = options ;
42
+ this . amdOptions = amdOptions ;
35
43
}
36
44
45
+ /**
46
+ * @param {Compiler } compiler the compiler instance
47
+ * @returns {void }
48
+ */
37
49
apply ( compiler ) {
38
50
const options = this . options ;
39
51
const amdOptions = this . amdOptions ;
@@ -103,44 +115,76 @@ class AMDPlugin {
103
115
new LocalModuleDependency . Template ( )
104
116
) ;
105
117
118
+ compilation . hooks . runtimeRequirementInModule
119
+ . for ( RuntimeGlobals . amdDefine )
120
+ . tap ( "AMDPlugin" , ( module , set ) => {
121
+ set . add ( RuntimeGlobals . require ) ;
122
+ } ) ;
123
+
124
+ compilation . hooks . runtimeRequirementInModule
125
+ . for ( RuntimeGlobals . amdOptions )
126
+ . tap ( "AMDPlugin" , ( module , set ) => {
127
+ set . add ( RuntimeGlobals . require ) ;
128
+ } ) ;
129
+
130
+ compilation . hooks . runtimeRequirementInTree
131
+ . for ( RuntimeGlobals . amdDefine )
132
+ . tap ( "AMDPlugin" , ( chunk , set ) => {
133
+ compilation . addRuntimeModule ( chunk , new AMDDefineRuntimeModule ( ) ) ;
134
+ } ) ;
135
+
136
+ compilation . hooks . runtimeRequirementInTree
137
+ . for ( RuntimeGlobals . amdOptions )
138
+ . tap ( "AMDPlugin" , ( chunk , set ) => {
139
+ compilation . addRuntimeModule (
140
+ chunk ,
141
+ new AMDOptionsRuntimeModule ( amdOptions )
142
+ ) ;
143
+ } ) ;
144
+
106
145
const handler = ( parser , parserOptions ) => {
107
146
if ( parserOptions . amd !== undefined && ! parserOptions . amd ) return ;
108
147
109
- const setExpressionToModule = ( outerExpr , module ) => {
110
- parser . hooks . expression . for ( outerExpr ) . tap ( "AMDPlugin" , expr => {
111
- const dep = new AMDRequireItemDependency ( module , expr . range ) ;
112
- dep . userRequest = outerExpr ;
113
- dep . loc = expr . loc ;
114
- parser . state . current . addDependency ( dep ) ;
115
- return true ;
116
- } ) ;
148
+ const tapOptionsHooks = optionExpr => {
149
+ parser . hooks . expression
150
+ . for ( optionExpr )
151
+ . tap (
152
+ "AMDPlugin" ,
153
+ toConstantDependency ( parser , RuntimeGlobals . amdOptions , [
154
+ RuntimeGlobals . amdOptions
155
+ ] )
156
+ ) ;
157
+ parser . hooks . evaluateIdentifier
158
+ . for ( optionExpr )
159
+ . tap ( "AMDPlugin" , evaluateToIdentifier ( optionExpr , true ) ) ;
160
+ parser . hooks . evaluateTypeof
161
+ . for ( optionExpr )
162
+ . tap ( "AMDPlugin" , evaluateToString ( "object" ) ) ;
163
+ parser . hooks . typeof
164
+ . for ( optionExpr )
165
+ . tap (
166
+ "AMDPlugin" ,
167
+ toConstantDependency ( parser , JSON . stringify ( "object" ) )
168
+ ) ;
117
169
} ;
118
170
119
171
new AMDRequireDependenciesBlockParserPlugin ( options ) . apply ( parser ) ;
120
172
new AMDDefineDependencyParserPlugin ( options ) . apply ( parser ) ;
121
173
122
- setExpressionToModule ( "require .amd" , "!!webpack amd options ") ;
123
- setExpressionToModule ( "define .amd" , "!!webpack amd options ") ;
124
- setExpressionToModule ( "define" , "!!webpack amd define ") ;
174
+ tapOptionsHooks ( "define .amd") ;
175
+ tapOptionsHooks ( "require .amd") ;
176
+ tapOptionsHooks ( "__webpack_amd_options__ ") ;
125
177
126
- parser . hooks . expression
127
- . for ( "__webpack_amd_options__" )
128
- . tap (
129
- "AMDPlugin" ,
130
- toConstantDependency ( parser , JSON . stringify ( amdOptions ) )
178
+ parser . hooks . expression . for ( "define" ) . tap ( "AMDPlugin" , expr => {
179
+ const dep = new ConstDependency (
180
+ RuntimeGlobals . amdDefine ,
181
+ expr . range ,
182
+ [ RuntimeGlobals . amdDefine ]
131
183
) ;
132
- parser . hooks . evaluateTypeof
133
- . for ( "define.amd" )
134
- . tap ( "AMDPlugin" , evaluateToString ( typeof amdOptions ) ) ;
135
- parser . hooks . evaluateTypeof
136
- . for ( "require.amd" )
137
- . tap ( "AMDPlugin" , evaluateToString ( typeof amdOptions ) ) ;
138
- parser . hooks . evaluateIdentifier
139
- . for ( "define.amd" )
140
- . tap ( "AMDPlugin" , evaluateToIdentifier ( "define.amd" , true ) ) ;
141
- parser . hooks . evaluateIdentifier
142
- . for ( "require.amd" )
143
- . tap ( "AMDPlugin" , evaluateToIdentifier ( "require.amd" , true ) ) ;
184
+ dep . loc = expr . loc ;
185
+ parser . state . current . addDependency ( dep ) ;
186
+ return true ;
187
+ } ) ;
144
188
parser . hooks . typeof
145
189
. for ( "define" )
146
190
. tap (
@@ -152,11 +196,11 @@ class AMDPlugin {
152
196
. tap ( "AMDPlugin" , evaluateToString ( "function" ) ) ;
153
197
parser . hooks . canRename . for ( "define" ) . tap ( "AMDPlugin" , approve ) ;
154
198
parser . hooks . rename . for ( "define" ) . tap ( "AMDPlugin" , expr => {
155
- const dep = new AMDRequireItemDependency (
156
- "!!webpack amd define" ,
157
- expr . range
199
+ const dep = new ConstDependency (
200
+ RuntimeGlobals . amdDefine ,
201
+ expr . range ,
202
+ [ RuntimeGlobals . amdDefine ]
158
203
) ;
159
- dep . userRequest = "define" ;
160
204
dep . loc = expr . loc ;
161
205
parser . state . current . addDependency ( dep ) ;
162
206
return false ;
@@ -180,54 +224,7 @@ class AMDPlugin {
180
224
. tap ( "AMDPlugin" , handler ) ;
181
225
}
182
226
) ;
183
- compiler . hooks . afterResolvers . tap ( "AMDPlugin" , ( ) => {
184
- compiler . resolverFactory . hooks . resolver
185
- . for ( "normal" )
186
- . tap ( "AMDPlugin" , resolver => {
187
- new AliasPlugin (
188
- "described-resolve" ,
189
- {
190
- name : "amdefine" ,
191
- alias : path . join (
192
- __dirname ,
193
- ".." ,
194
- ".." ,
195
- "buildin" ,
196
- "amd-define.js"
197
- )
198
- } ,
199
- "resolve"
200
- ) . apply ( resolver ) ;
201
- new AliasPlugin (
202
- "described-resolve" ,
203
- {
204
- name : "webpack amd options" ,
205
- alias : path . join (
206
- __dirname ,
207
- ".." ,
208
- ".." ,
209
- "buildin" ,
210
- "amd-options.js"
211
- )
212
- } ,
213
- "resolve"
214
- ) . apply ( resolver ) ;
215
- new AliasPlugin (
216
- "described-resolve" ,
217
- {
218
- name : "webpack amd define" ,
219
- alias : path . join (
220
- __dirname ,
221
- ".." ,
222
- ".." ,
223
- "buildin" ,
224
- "amd-define.js"
225
- )
226
- } ,
227
- "resolve"
228
- ) . apply ( resolver ) ;
229
- } ) ;
230
- } ) ;
231
227
}
232
228
}
229
+
233
230
module . exports = AMDPlugin ;
0 commit comments