2013-02-14 04:24:00 +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-11 17:51:58 +08:00
|
|
|
"use strict";
|
2013-02-14 04:24:00 +08:00
|
|
|
|
2018-07-03 16:24:29 +08:00
|
|
|
const {
|
|
|
|
toConstantDependency,
|
|
|
|
toConstantDependencyWithWebpackRequire
|
2018-07-03 16:25:08 +08:00
|
|
|
} = require("./JavascriptParserHelpers");
|
2018-07-30 23:08:51 +08:00
|
|
|
const NullFactory = require("./NullFactory");
|
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
2014-06-16 21:18:49 +08:00
|
|
|
|
2017-01-08 00:05:02 +08:00
|
|
|
module.exports = class RequireJsStuffPlugin {
|
|
|
|
apply(compiler) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"RequireJsStuffPlugin",
|
|
|
|
(compilation, { normalModuleFactory }) => {
|
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(
|
|
|
|
ConstDependency,
|
|
|
|
new ConstDependency.Template()
|
|
|
|
);
|
|
|
|
const handler = (parser, parserOptions) => {
|
|
|
|
if (
|
2018-06-26 16:58:42 +08:00
|
|
|
typeof parserOptions.requireJs === "undefined" ||
|
2018-02-25 09:00:20 +08:00
|
|
|
!parserOptions.requireJs
|
2018-06-26 16:58:42 +08:00
|
|
|
) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return;
|
2018-06-26 16:58:42 +08:00
|
|
|
}
|
2016-09-14 18:04:42 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.call
|
|
|
|
.for("require.config")
|
|
|
|
.tap(
|
|
|
|
"RequireJsStuffPlugin",
|
2018-07-03 16:24:29 +08:00
|
|
|
toConstantDependency(parser, "undefined")
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
parser.hooks.call
|
|
|
|
.for("requirejs.config")
|
|
|
|
.tap(
|
|
|
|
"RequireJsStuffPlugin",
|
2018-07-03 16:24:29 +08:00
|
|
|
toConstantDependency(parser, "undefined")
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-01-08 00:05:02 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.expression
|
|
|
|
.for("require.version")
|
|
|
|
.tap(
|
|
|
|
"RequireJsStuffPlugin",
|
2018-07-03 16:24:29 +08:00
|
|
|
toConstantDependency(parser, JSON.stringify("0.0.0"))
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
parser.hooks.expression
|
|
|
|
.for("requirejs.onError")
|
|
|
|
.tap(
|
|
|
|
"RequireJsStuffPlugin",
|
2018-07-03 16:24:29 +08:00
|
|
|
toConstantDependencyWithWebpackRequire(
|
2018-02-25 09:00:20 +08:00
|
|
|
parser,
|
|
|
|
"__webpack_require__.oe"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/auto")
|
|
|
|
.tap("RequireJsStuffPlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/dynamic")
|
|
|
|
.tap("RequireJsStuffPlugin", handler);
|
|
|
|
}
|
|
|
|
);
|
2017-01-08 00:05:02 +08:00
|
|
|
}
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|