Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more module type constants, use them across codebase #16898

Merged
merged 1 commit into from Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 25 additions & 18 deletions lib/APIPlugin.js
Expand Up @@ -5,6 +5,11 @@

"use strict";

const {
JAVASCRIPT_MODULE_TYPE_AUTO,
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
JAVASCRIPT_MODULE_TYPE_ESM
} = require("./ModuleTypeConstants");
const RuntimeGlobals = require("./RuntimeGlobals");
const WebpackError = require("./WebpackError");
const ConstDependency = require("./dependencies/ConstDependency");
Expand Down Expand Up @@ -113,6 +118,8 @@ const REPLACEMENTS = {
};
/* eslint-enable camelcase */

const PLUGIN_NAME = "APIPlugin";

class APIPlugin {
/**
* Apply the plugin
Expand All @@ -121,7 +128,7 @@ class APIPlugin {
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"APIPlugin",
PLUGIN_NAME,
(compilation, { normalModuleFactory }) => {
compilation.dependencyTemplates.set(
ConstDependency,
Expand All @@ -130,7 +137,7 @@ class APIPlugin {

compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.chunkName)
.tap("APIPlugin", chunk => {
.tap(PLUGIN_NAME, chunk => {
compilation.addRuntimeModule(
chunk,
new ChunkNameRuntimeModule(chunk.name)
Expand All @@ -140,7 +147,7 @@ class APIPlugin {

compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.getFullHash)
.tap("APIPlugin", (chunk, set) => {
.tap(PLUGIN_NAME, (chunk, set) => {
compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule());
return true;
});
Expand All @@ -154,11 +161,11 @@ class APIPlugin {
parser.hooks.expression
.for(key)
.tap(
"APIPlugin",
PLUGIN_NAME,
toConstantDependency(parser, info.expr, info.req)
);
if (info.assign === false) {
parser.hooks.assign.for(key).tap("APIPlugin", expr => {
parser.hooks.assign.for(key).tap(PLUGIN_NAME, expr => {
const err = new WebpackError(`${key} must not be assigned`);
err.loc = expr.loc;
throw err;
Expand All @@ -167,13 +174,13 @@ class APIPlugin {
if (info.type) {
parser.hooks.evaluateTypeof
.for(key)
.tap("APIPlugin", evaluateToString(info.type));
.tap(PLUGIN_NAME, evaluateToString(info.type));
}
});

parser.hooks.expression
.for("__webpack_layer__")
.tap("APIPlugin", expr => {
.tap(PLUGIN_NAME, expr => {
const dep = new ConstDependency(
JSON.stringify(parser.state.module.layer),
expr.range
Expand All @@ -184,7 +191,7 @@ class APIPlugin {
});
parser.hooks.evaluateIdentifier
.for("__webpack_layer__")
.tap("APIPlugin", expr =>
.tap(PLUGIN_NAME, expr =>
(parser.state.module.layer === null
? new BasicEvaluatedExpression().setNull()
: new BasicEvaluatedExpression().setString(
Expand All @@ -194,7 +201,7 @@ class APIPlugin {
);
parser.hooks.evaluateTypeof
.for("__webpack_layer__")
.tap("APIPlugin", expr =>
.tap(PLUGIN_NAME, expr =>
new BasicEvaluatedExpression()
.setString(
parser.state.module.layer === null ? "object" : "string"
Expand All @@ -204,7 +211,7 @@ class APIPlugin {

parser.hooks.expression
.for("__webpack_module__.id")
.tap("APIPlugin", expr => {
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
"__webpack_module__.id";
const dep = new ConstDependency(
Expand All @@ -219,7 +226,7 @@ class APIPlugin {

parser.hooks.expression
.for("__webpack_module__")
.tap("APIPlugin", expr => {
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
"__webpack_module__";
const dep = new ConstDependency(
Expand All @@ -233,18 +240,18 @@ class APIPlugin {
});
parser.hooks.evaluateTypeof
.for("__webpack_module__")
.tap("APIPlugin", evaluateToString("object"));
.tap(PLUGIN_NAME, evaluateToString("object"));
};

normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("APIPlugin", handler);
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
.tap(PLUGIN_NAME, handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("APIPlugin", handler);
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
.tap(PLUGIN_NAME, handler);
normalModuleFactory.hooks.parser
.for("javascript/esm")
.tap("APIPlugin", handler);
.for(JAVASCRIPT_MODULE_TYPE_ESM)
.tap(PLUGIN_NAME, handler);
}
);
}
Expand Down
105 changes: 53 additions & 52 deletions lib/CompatibilityPlugin.js
Expand Up @@ -5,12 +5,18 @@

"use strict";

const {
JAVASCRIPT_MODULE_TYPE_AUTO,
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
JAVASCRIPT_MODULE_TYPE_ESM
} = require("./ModuleTypeConstants");
const ConstDependency = require("./dependencies/ConstDependency");

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

const nestedWebpackRequireTag = Symbol("nested __webpack_require__");
const PLUGIN_NAME = "CompatibilityPlugin";

class CompatibilityPlugin {
/**
Expand All @@ -20,49 +26,47 @@ class CompatibilityPlugin {
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"CompatibilityPlugin",
PLUGIN_NAME,
(compilation, { normalModuleFactory }) => {
compilation.dependencyTemplates.set(
ConstDependency,
new ConstDependency.Template()
);

normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("CompatibilityPlugin", (parser, parserOptions) => {
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
.tap(PLUGIN_NAME, (parser, parserOptions) => {
if (
parserOptions.browserify !== undefined &&
!parserOptions.browserify
)
return;

parser.hooks.call
.for("require")
.tap("CompatibilityPlugin", expr => {
// support for browserify style require delegator: "require(o, !0)"
if (expr.arguments.length !== 2) return;
const second = parser.evaluateExpression(expr.arguments[1]);
if (!second.isBoolean()) return;
if (second.asBool() !== true) return;
const dep = new ConstDependency("require", expr.callee.range);
dep.loc = expr.loc;
if (parser.state.current.dependencies.length > 0) {
const last =
parser.state.current.dependencies[
parser.state.current.dependencies.length - 1
];
if (
last.critical &&
last.options &&
last.options.request === "." &&
last.userRequest === "." &&
last.options.recursive
)
parser.state.current.dependencies.pop();
}
parser.state.module.addPresentationalDependency(dep);
return true;
});
parser.hooks.call.for("require").tap(PLUGIN_NAME, expr => {
// support for browserify style require delegator: "require(o, !0)"
if (expr.arguments.length !== 2) return;
const second = parser.evaluateExpression(expr.arguments[1]);
if (!second.isBoolean()) return;
if (second.asBool() !== true) return;
const dep = new ConstDependency("require", expr.callee.range);
dep.loc = expr.loc;
if (parser.state.current.dependencies.length > 0) {
const last =
parser.state.current.dependencies[
parser.state.current.dependencies.length - 1
];
if (
last.critical &&
last.options &&
last.options.request === "." &&
last.userRequest === "." &&
last.options.recursive
)
parser.state.current.dependencies.pop();
}
parser.state.module.addPresentationalDependency(dep);
return true;
});
});

/**
Expand All @@ -71,7 +75,7 @@ class CompatibilityPlugin {
*/
const handler = parser => {
// Handle nested requires
parser.hooks.preStatement.tap("CompatibilityPlugin", statement => {
parser.hooks.preStatement.tap(PLUGIN_NAME, statement => {
if (
statement.type === "FunctionDeclaration" &&
statement.id &&
Expand All @@ -91,7 +95,7 @@ class CompatibilityPlugin {
});
parser.hooks.pattern
.for("__webpack_require__")
.tap("CompatibilityPlugin", pattern => {
.tap(PLUGIN_NAME, pattern => {
const newName = `__nested_webpack_require_${pattern.range[0]}__`;
parser.tagVariable(pattern.name, nestedWebpackRequireTag, {
name: newName,
Expand All @@ -105,7 +109,7 @@ class CompatibilityPlugin {
});
parser.hooks.expression
.for(nestedWebpackRequireTag)
.tap("CompatibilityPlugin", expr => {
.tap(PLUGIN_NAME, expr => {
const { name, declaration } = parser.currentTagData;
if (!declaration.updated) {
const dep = new ConstDependency(name, declaration.range);
Expand All @@ -120,31 +124,28 @@ class CompatibilityPlugin {
});

// Handle hashbang
parser.hooks.program.tap(
"CompatibilityPlugin",
(program, comments) => {
if (comments.length === 0) return;
const c = comments[0];
if (c.type === "Line" && c.range[0] === 0) {
if (parser.state.source.slice(0, 2).toString() !== "#!") return;
// this is a hashbang comment
const dep = new ConstDependency("//", 0);
dep.loc = c.loc;
parser.state.module.addPresentationalDependency(dep);
}
parser.hooks.program.tap(PLUGIN_NAME, (program, comments) => {
if (comments.length === 0) return;
const c = comments[0];
if (c.type === "Line" && c.range[0] === 0) {
if (parser.state.source.slice(0, 2).toString() !== "#!") return;
// this is a hashbang comment
const dep = new ConstDependency("//", 0);
dep.loc = c.loc;
parser.state.module.addPresentationalDependency(dep);
}
);
});
};

normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("CompatibilityPlugin", handler);
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
.tap(PLUGIN_NAME, handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("CompatibilityPlugin", handler);
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
.tap(PLUGIN_NAME, handler);
normalModuleFactory.hooks.parser
.for("javascript/esm")
.tap("CompatibilityPlugin", handler);
.for(JAVASCRIPT_MODULE_TYPE_ESM)
.tap(PLUGIN_NAME, handler);
}
);
}
Expand Down