2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-10 18:29:55 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
|
|
|
|
|
|
|
const NullFactory = require("./NullFactory");
|
|
|
|
|
|
|
|
class CompatibilityPlugin {
|
|
|
|
|
|
|
|
apply(compiler) {
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.compilation.tap("CompatibilityPlugin", (compilation, {
|
|
|
|
normalModuleFactory
|
|
|
|
}) => {
|
2017-01-10 18:29:55 +08:00
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
|
|
|
|
2017-12-14 17:22:27 +08:00
|
|
|
normalModuleFactory.hooks.parser.for("javascript/auto").tap("CompatibilityPlugin", (parser, parserOptions) => {
|
2017-01-10 18:29:55 +08:00
|
|
|
|
|
|
|
if(typeof parserOptions.browserify !== "undefined" && !parserOptions.browserify)
|
|
|
|
return;
|
|
|
|
|
2017-12-19 21:35:30 +08:00
|
|
|
parser.hooks.call.for("require").tap("CompatibilityPlugin", (expr) => {
|
2017-01-10 18:29:55 +08:00
|
|
|
// 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];
|
2017-10-16 19:19:53 +08:00
|
|
|
if(last.critical && last.options && last.options.request === "." && last.userRequest === "." && last.options.recursive)
|
2017-01-10 18:29:55 +08:00
|
|
|
parser.state.current.dependencies.pop();
|
|
|
|
}
|
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
2016-09-14 18:04:42 +08:00
|
|
|
});
|
2017-01-10 18:29:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = CompatibilityPlugin;
|