5
5
6
6
"use strict" ;
7
7
8
+ const {
9
+ JAVASCRIPT_MODULE_TYPE_AUTO ,
10
+ JAVASCRIPT_MODULE_TYPE_DYNAMIC ,
11
+ JAVASCRIPT_MODULE_TYPE_ESM
12
+ } = require ( "./ModuleTypeConstants" ) ;
8
13
const ConstDependency = require ( "./dependencies/ConstDependency" ) ;
9
14
10
15
/** @typedef {import("./Compiler") } Compiler */
11
16
/** @typedef {import("./javascript/JavascriptParser") } JavascriptParser */
12
17
13
18
const nestedWebpackRequireTag = Symbol ( "nested __webpack_require__" ) ;
19
+ const PLUGIN_NAME = "CompatibilityPlugin" ;
14
20
15
21
class CompatibilityPlugin {
16
22
/**
@@ -20,49 +26,47 @@ class CompatibilityPlugin {
20
26
*/
21
27
apply ( compiler ) {
22
28
compiler . hooks . compilation . tap (
23
- "CompatibilityPlugin" ,
29
+ PLUGIN_NAME ,
24
30
( compilation , { normalModuleFactory } ) => {
25
31
compilation . dependencyTemplates . set (
26
32
ConstDependency ,
27
33
new ConstDependency . Template ( )
28
34
) ;
29
35
30
36
normalModuleFactory . hooks . parser
31
- . for ( "javascript/auto" )
32
- . tap ( "CompatibilityPlugin" , ( parser , parserOptions ) => {
37
+ . for ( JAVASCRIPT_MODULE_TYPE_AUTO )
38
+ . tap ( PLUGIN_NAME , ( parser , parserOptions ) => {
33
39
if (
34
40
parserOptions . browserify !== undefined &&
35
41
! parserOptions . browserify
36
42
)
37
43
return ;
38
44
39
- parser . hooks . call
40
- . for ( "require" )
41
- . tap ( "CompatibilityPlugin" , expr => {
42
- // support for browserify style require delegator: "require(o, !0)"
43
- if ( expr . arguments . length !== 2 ) return ;
44
- const second = parser . evaluateExpression ( expr . arguments [ 1 ] ) ;
45
- if ( ! second . isBoolean ( ) ) return ;
46
- if ( second . asBool ( ) !== true ) return ;
47
- const dep = new ConstDependency ( "require" , expr . callee . range ) ;
48
- dep . loc = expr . loc ;
49
- if ( parser . state . current . dependencies . length > 0 ) {
50
- const last =
51
- parser . state . current . dependencies [
52
- parser . state . current . dependencies . length - 1
53
- ] ;
54
- if (
55
- last . critical &&
56
- last . options &&
57
- last . options . request === "." &&
58
- last . userRequest === "." &&
59
- last . options . recursive
60
- )
61
- parser . state . current . dependencies . pop ( ) ;
62
- }
63
- parser . state . module . addPresentationalDependency ( dep ) ;
64
- return true ;
65
- } ) ;
45
+ parser . hooks . call . for ( "require" ) . tap ( PLUGIN_NAME , expr => {
46
+ // support for browserify style require delegator: "require(o, !0)"
47
+ if ( expr . arguments . length !== 2 ) return ;
48
+ const second = parser . evaluateExpression ( expr . arguments [ 1 ] ) ;
49
+ if ( ! second . isBoolean ( ) ) return ;
50
+ if ( second . asBool ( ) !== true ) return ;
51
+ const dep = new ConstDependency ( "require" , expr . callee . range ) ;
52
+ dep . loc = expr . loc ;
53
+ if ( parser . state . current . dependencies . length > 0 ) {
54
+ const last =
55
+ parser . state . current . dependencies [
56
+ parser . state . current . dependencies . length - 1
57
+ ] ;
58
+ if (
59
+ last . critical &&
60
+ last . options &&
61
+ last . options . request === "." &&
62
+ last . userRequest === "." &&
63
+ last . options . recursive
64
+ )
65
+ parser . state . current . dependencies . pop ( ) ;
66
+ }
67
+ parser . state . module . addPresentationalDependency ( dep ) ;
68
+ return true ;
69
+ } ) ;
66
70
} ) ;
67
71
68
72
/**
@@ -71,7 +75,7 @@ class CompatibilityPlugin {
71
75
*/
72
76
const handler = parser => {
73
77
// Handle nested requires
74
- parser . hooks . preStatement . tap ( "CompatibilityPlugin" , statement => {
78
+ parser . hooks . preStatement . tap ( PLUGIN_NAME , statement => {
75
79
if (
76
80
statement . type === "FunctionDeclaration" &&
77
81
statement . id &&
@@ -91,7 +95,7 @@ class CompatibilityPlugin {
91
95
} ) ;
92
96
parser . hooks . pattern
93
97
. for ( "__webpack_require__" )
94
- . tap ( "CompatibilityPlugin" , pattern => {
98
+ . tap ( PLUGIN_NAME , pattern => {
95
99
const newName = `__nested_webpack_require_${ pattern . range [ 0 ] } __` ;
96
100
parser . tagVariable ( pattern . name , nestedWebpackRequireTag , {
97
101
name : newName ,
@@ -105,7 +109,7 @@ class CompatibilityPlugin {
105
109
} ) ;
106
110
parser . hooks . expression
107
111
. for ( nestedWebpackRequireTag )
108
- . tap ( "CompatibilityPlugin" , expr => {
112
+ . tap ( PLUGIN_NAME , expr => {
109
113
const { name, declaration } = parser . currentTagData ;
110
114
if ( ! declaration . updated ) {
111
115
const dep = new ConstDependency ( name , declaration . range ) ;
@@ -120,31 +124,28 @@ class CompatibilityPlugin {
120
124
} ) ;
121
125
122
126
// Handle hashbang
123
- parser . hooks . program . tap (
124
- "CompatibilityPlugin" ,
125
- ( program , comments ) => {
126
- if ( comments . length === 0 ) return ;
127
- const c = comments [ 0 ] ;
128
- if ( c . type === "Line" && c . range [ 0 ] === 0 ) {
129
- if ( parser . state . source . slice ( 0 , 2 ) . toString ( ) !== "#!" ) return ;
130
- // this is a hashbang comment
131
- const dep = new ConstDependency ( "//" , 0 ) ;
132
- dep . loc = c . loc ;
133
- parser . state . module . addPresentationalDependency ( dep ) ;
134
- }
127
+ parser . hooks . program . tap ( PLUGIN_NAME , ( program , comments ) => {
128
+ if ( comments . length === 0 ) return ;
129
+ const c = comments [ 0 ] ;
130
+ if ( c . type === "Line" && c . range [ 0 ] === 0 ) {
131
+ if ( parser . state . source . slice ( 0 , 2 ) . toString ( ) !== "#!" ) return ;
132
+ // this is a hashbang comment
133
+ const dep = new ConstDependency ( "//" , 0 ) ;
134
+ dep . loc = c . loc ;
135
+ parser . state . module . addPresentationalDependency ( dep ) ;
135
136
}
136
- ) ;
137
+ } ) ;
137
138
} ;
138
139
139
140
normalModuleFactory . hooks . parser
140
- . for ( "javascript/auto" )
141
- . tap ( "CompatibilityPlugin" , handler ) ;
141
+ . for ( JAVASCRIPT_MODULE_TYPE_AUTO )
142
+ . tap ( PLUGIN_NAME , handler ) ;
142
143
normalModuleFactory . hooks . parser
143
- . for ( "javascript/dynamic" )
144
- . tap ( "CompatibilityPlugin" , handler ) ;
144
+ . for ( JAVASCRIPT_MODULE_TYPE_DYNAMIC )
145
+ . tap ( PLUGIN_NAME , handler ) ;
145
146
normalModuleFactory . hooks . parser
146
- . for ( "javascript/esm" )
147
- . tap ( "CompatibilityPlugin" , handler ) ;
147
+ . for ( JAVASCRIPT_MODULE_TYPE_ESM )
148
+ . tap ( PLUGIN_NAME , handler ) ;
148
149
}
149
150
) ;
150
151
}
0 commit comments