webpack/lib/RequireJsStuffPlugin.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

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-11-06 02:03:12 +08:00
const { toConstantDependency } = require("./JavascriptParserHelpers");
const RuntimeGlobals = require("./RuntimeGlobals");
2018-07-30 23:08:51 +08:00
const ConstDependency = require("./dependencies/ConstDependency");
2018-11-03 04:05:46 +08:00
/** @typedef {import("./Compiler")} Compiler */
module.exports = class RequireJsStuffPlugin {
2018-11-03 04:05:46 +08:00
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
"RequireJsStuffPlugin",
(compilation, { normalModuleFactory }) => {
compilation.dependencyTemplates.set(
ConstDependency,
new ConstDependency.Template()
);
const handler = (parser, parserOptions) => {
if (
2018-08-21 17:02:16 +08:00
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
}
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
);
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-11-06 02:03:12 +08:00
toConstantDependency(
2018-02-25 09:00:20 +08:00
parser,
2018-11-06 02:03:12 +08:00
RuntimeGlobals.uncaughtErrorHandler,
[RuntimeGlobals.uncaughtErrorHandler]
2018-02-25 09:00:20 +08:00
)
);
};
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("RequireJsStuffPlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("RequireJsStuffPlugin", handler);
}
);
}
2017-01-11 17:51:58 +08:00
};