2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-10 18:29:55 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
|
|
|
|
2018-07-09 20:48:28 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2019-05-21 04:53:58 +08:00
|
|
|
/** @typedef {import("./JavascriptParser")} JavascriptParser */
|
2018-06-20 03:19:20 +08:00
|
|
|
|
2019-08-29 21:28:19 +08:00
|
|
|
const nestedWebpackRequireTag = Symbol("nested __webpack_require__");
|
|
|
|
|
2017-01-10 18:29:55 +08:00
|
|
|
class CompatibilityPlugin {
|
2018-06-16 02:15:27 +08:00
|
|
|
/**
|
|
|
|
* Apply the plugin
|
2018-11-03 04:05:46 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
2018-06-16 02:15:27 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-10 18:29:55 +08:00
|
|
|
apply(compiler) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"CompatibilityPlugin",
|
|
|
|
(compilation, { normalModuleFactory }) => {
|
|
|
|
compilation.dependencyTemplates.set(
|
|
|
|
ConstDependency,
|
|
|
|
new ConstDependency.Template()
|
|
|
|
);
|
2017-01-10 18:29:55 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/auto")
|
|
|
|
.tap("CompatibilityPlugin", (parser, parserOptions) => {
|
|
|
|
if (
|
2018-08-21 08:26:50 +08:00
|
|
|
parserOptions.browserify !== undefined &&
|
2018-02-25 09:00:20 +08:00
|
|
|
!parserOptions.browserify
|
|
|
|
)
|
|
|
|
return;
|
2017-01-10 18:29:55 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
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 > 1) {
|
|
|
|
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.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
2019-05-21 04:53:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {JavascriptParser} parser the parser
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
const nestedWebpackRequireHandler = parser => {
|
|
|
|
parser.hooks.pattern
|
|
|
|
.for("__webpack_require__")
|
|
|
|
.tap("CompatibilityPlugin", pattern => {
|
|
|
|
const newName = `__nested_webpack_require_${pattern.range[0]}__`;
|
|
|
|
const dep = new ConstDependency(newName, pattern.range);
|
|
|
|
dep.loc = pattern.loc;
|
|
|
|
parser.state.current.addDependency(dep);
|
2019-08-29 21:28:19 +08:00
|
|
|
parser.tagVariable(
|
2019-05-21 04:53:58 +08:00
|
|
|
pattern.name,
|
2019-08-29 21:28:19 +08:00
|
|
|
nestedWebpackRequireTag,
|
2019-05-21 04:53:58 +08:00
|
|
|
newName
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.hooks.expression
|
2019-08-29 21:28:19 +08:00
|
|
|
.for(nestedWebpackRequireTag)
|
2019-05-21 04:53:58 +08:00
|
|
|
.tap("CompatibilityPlugin", expr => {
|
2019-08-29 21:28:19 +08:00
|
|
|
const newName = parser.currentTagData;
|
2019-05-21 04:53:58 +08:00
|
|
|
const dep = new ConstDependency(newName, expr.range);
|
|
|
|
dep.loc = expr.loc;
|
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/auto")
|
|
|
|
.tap("CompatibilityPlugin", nestedWebpackRequireHandler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/dynamic")
|
|
|
|
.tap("CompatibilityPlugin", nestedWebpackRequireHandler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/esm")
|
|
|
|
.tap("CompatibilityPlugin", nestedWebpackRequireHandler);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
);
|
2017-01-10 18:29:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = CompatibilityPlugin;
|