Skip to content

Commit fa4cbf1

Browse files
committed
add more module type constants, use them across codebase
1 parent 4bcc0f0 commit fa4cbf1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+903
-729
lines changed

lib/APIPlugin.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
"use strict";
77

8+
const {
9+
JAVASCRIPT_MODULE_TYPE_AUTO,
10+
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
11+
JAVASCRIPT_MODULE_TYPE_ESM
12+
} = require("./ModuleTypeConstants");
813
const RuntimeGlobals = require("./RuntimeGlobals");
914
const WebpackError = require("./WebpackError");
1015
const ConstDependency = require("./dependencies/ConstDependency");
@@ -113,6 +118,8 @@ const REPLACEMENTS = {
113118
};
114119
/* eslint-enable camelcase */
115120

121+
const PLUGIN_NAME = "APIPlugin";
122+
116123
class APIPlugin {
117124
/**
118125
* Apply the plugin
@@ -121,7 +128,7 @@ class APIPlugin {
121128
*/
122129
apply(compiler) {
123130
compiler.hooks.compilation.tap(
124-
"APIPlugin",
131+
PLUGIN_NAME,
125132
(compilation, { normalModuleFactory }) => {
126133
compilation.dependencyTemplates.set(
127134
ConstDependency,
@@ -130,7 +137,7 @@ class APIPlugin {
130137

131138
compilation.hooks.runtimeRequirementInTree
132139
.for(RuntimeGlobals.chunkName)
133-
.tap("APIPlugin", chunk => {
140+
.tap(PLUGIN_NAME, chunk => {
134141
compilation.addRuntimeModule(
135142
chunk,
136143
new ChunkNameRuntimeModule(chunk.name)
@@ -140,7 +147,7 @@ class APIPlugin {
140147

141148
compilation.hooks.runtimeRequirementInTree
142149
.for(RuntimeGlobals.getFullHash)
143-
.tap("APIPlugin", (chunk, set) => {
150+
.tap(PLUGIN_NAME, (chunk, set) => {
144151
compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule());
145152
return true;
146153
});
@@ -154,11 +161,11 @@ class APIPlugin {
154161
parser.hooks.expression
155162
.for(key)
156163
.tap(
157-
"APIPlugin",
164+
PLUGIN_NAME,
158165
toConstantDependency(parser, info.expr, info.req)
159166
);
160167
if (info.assign === false) {
161-
parser.hooks.assign.for(key).tap("APIPlugin", expr => {
168+
parser.hooks.assign.for(key).tap(PLUGIN_NAME, expr => {
162169
const err = new WebpackError(`${key} must not be assigned`);
163170
err.loc = expr.loc;
164171
throw err;
@@ -167,13 +174,13 @@ class APIPlugin {
167174
if (info.type) {
168175
parser.hooks.evaluateTypeof
169176
.for(key)
170-
.tap("APIPlugin", evaluateToString(info.type));
177+
.tap(PLUGIN_NAME, evaluateToString(info.type));
171178
}
172179
});
173180

174181
parser.hooks.expression
175182
.for("__webpack_layer__")
176-
.tap("APIPlugin", expr => {
183+
.tap(PLUGIN_NAME, expr => {
177184
const dep = new ConstDependency(
178185
JSON.stringify(parser.state.module.layer),
179186
expr.range
@@ -184,7 +191,7 @@ class APIPlugin {
184191
});
185192
parser.hooks.evaluateIdentifier
186193
.for("__webpack_layer__")
187-
.tap("APIPlugin", expr =>
194+
.tap(PLUGIN_NAME, expr =>
188195
(parser.state.module.layer === null
189196
? new BasicEvaluatedExpression().setNull()
190197
: new BasicEvaluatedExpression().setString(
@@ -194,7 +201,7 @@ class APIPlugin {
194201
);
195202
parser.hooks.evaluateTypeof
196203
.for("__webpack_layer__")
197-
.tap("APIPlugin", expr =>
204+
.tap(PLUGIN_NAME, expr =>
198205
new BasicEvaluatedExpression()
199206
.setString(
200207
parser.state.module.layer === null ? "object" : "string"
@@ -204,7 +211,7 @@ class APIPlugin {
204211

205212
parser.hooks.expression
206213
.for("__webpack_module__.id")
207-
.tap("APIPlugin", expr => {
214+
.tap(PLUGIN_NAME, expr => {
208215
parser.state.module.buildInfo.moduleConcatenationBailout =
209216
"__webpack_module__.id";
210217
const dep = new ConstDependency(
@@ -219,7 +226,7 @@ class APIPlugin {
219226

220227
parser.hooks.expression
221228
.for("__webpack_module__")
222-
.tap("APIPlugin", expr => {
229+
.tap(PLUGIN_NAME, expr => {
223230
parser.state.module.buildInfo.moduleConcatenationBailout =
224231
"__webpack_module__";
225232
const dep = new ConstDependency(
@@ -233,18 +240,18 @@ class APIPlugin {
233240
});
234241
parser.hooks.evaluateTypeof
235242
.for("__webpack_module__")
236-
.tap("APIPlugin", evaluateToString("object"));
243+
.tap(PLUGIN_NAME, evaluateToString("object"));
237244
};
238245

239246
normalModuleFactory.hooks.parser
240-
.for("javascript/auto")
241-
.tap("APIPlugin", handler);
247+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
248+
.tap(PLUGIN_NAME, handler);
242249
normalModuleFactory.hooks.parser
243-
.for("javascript/dynamic")
244-
.tap("APIPlugin", handler);
250+
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
251+
.tap(PLUGIN_NAME, handler);
245252
normalModuleFactory.hooks.parser
246-
.for("javascript/esm")
247-
.tap("APIPlugin", handler);
253+
.for(JAVASCRIPT_MODULE_TYPE_ESM)
254+
.tap(PLUGIN_NAME, handler);
248255
}
249256
);
250257
}

lib/CompatibilityPlugin.js

+53-52
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55

66
"use strict";
77

8+
const {
9+
JAVASCRIPT_MODULE_TYPE_AUTO,
10+
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
11+
JAVASCRIPT_MODULE_TYPE_ESM
12+
} = require("./ModuleTypeConstants");
813
const ConstDependency = require("./dependencies/ConstDependency");
914

1015
/** @typedef {import("./Compiler")} Compiler */
1116
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
1217

1318
const nestedWebpackRequireTag = Symbol("nested __webpack_require__");
19+
const PLUGIN_NAME = "CompatibilityPlugin";
1420

1521
class CompatibilityPlugin {
1622
/**
@@ -20,49 +26,47 @@ class CompatibilityPlugin {
2026
*/
2127
apply(compiler) {
2228
compiler.hooks.compilation.tap(
23-
"CompatibilityPlugin",
29+
PLUGIN_NAME,
2430
(compilation, { normalModuleFactory }) => {
2531
compilation.dependencyTemplates.set(
2632
ConstDependency,
2733
new ConstDependency.Template()
2834
);
2935

3036
normalModuleFactory.hooks.parser
31-
.for("javascript/auto")
32-
.tap("CompatibilityPlugin", (parser, parserOptions) => {
37+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
38+
.tap(PLUGIN_NAME, (parser, parserOptions) => {
3339
if (
3440
parserOptions.browserify !== undefined &&
3541
!parserOptions.browserify
3642
)
3743
return;
3844

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+
});
6670
});
6771

6872
/**
@@ -71,7 +75,7 @@ class CompatibilityPlugin {
7175
*/
7276
const handler = parser => {
7377
// Handle nested requires
74-
parser.hooks.preStatement.tap("CompatibilityPlugin", statement => {
78+
parser.hooks.preStatement.tap(PLUGIN_NAME, statement => {
7579
if (
7680
statement.type === "FunctionDeclaration" &&
7781
statement.id &&
@@ -91,7 +95,7 @@ class CompatibilityPlugin {
9195
});
9296
parser.hooks.pattern
9397
.for("__webpack_require__")
94-
.tap("CompatibilityPlugin", pattern => {
98+
.tap(PLUGIN_NAME, pattern => {
9599
const newName = `__nested_webpack_require_${pattern.range[0]}__`;
96100
parser.tagVariable(pattern.name, nestedWebpackRequireTag, {
97101
name: newName,
@@ -105,7 +109,7 @@ class CompatibilityPlugin {
105109
});
106110
parser.hooks.expression
107111
.for(nestedWebpackRequireTag)
108-
.tap("CompatibilityPlugin", expr => {
112+
.tap(PLUGIN_NAME, expr => {
109113
const { name, declaration } = parser.currentTagData;
110114
if (!declaration.updated) {
111115
const dep = new ConstDependency(name, declaration.range);
@@ -120,31 +124,28 @@ class CompatibilityPlugin {
120124
});
121125

122126
// 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);
135136
}
136-
);
137+
});
137138
};
138139

139140
normalModuleFactory.hooks.parser
140-
.for("javascript/auto")
141-
.tap("CompatibilityPlugin", handler);
141+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
142+
.tap(PLUGIN_NAME, handler);
142143
normalModuleFactory.hooks.parser
143-
.for("javascript/dynamic")
144-
.tap("CompatibilityPlugin", handler);
144+
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
145+
.tap(PLUGIN_NAME, handler);
145146
normalModuleFactory.hooks.parser
146-
.for("javascript/esm")
147-
.tap("CompatibilityPlugin", handler);
147+
.for(JAVASCRIPT_MODULE_TYPE_ESM)
148+
.tap(PLUGIN_NAME, handler);
148149
}
149150
);
150151
}

0 commit comments

Comments
 (0)