2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-03 01:00:08 +08:00
|
|
|
"use strict";
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-01-03 01:00:08 +08:00
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
2017-01-09 02:11:26 +08:00
|
|
|
const ParserHelpers = require("./ParserHelpers");
|
2014-06-16 21:18:49 +08:00
|
|
|
|
2017-01-03 01:00:08 +08:00
|
|
|
const NullFactory = require("./NullFactory");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-12-13 18:14:00 +08:00
|
|
|
/* eslint-disable camelcase */
|
2017-01-03 01:00:08 +08:00
|
|
|
const REPLACEMENTS = {
|
2017-12-13 18:14:00 +08:00
|
|
|
__webpack_require__: "__webpack_require__",
|
|
|
|
__webpack_public_path__: "__webpack_require__.p",
|
|
|
|
__webpack_modules__: "__webpack_require__.m",
|
|
|
|
__webpack_chunk_load__: "__webpack_require__.e",
|
|
|
|
__non_webpack_require__: "require",
|
|
|
|
__webpack_nonce__: "__webpack_require__.nc",
|
|
|
|
"require.onError": "__webpack_require__.oe"
|
|
|
|
};
|
|
|
|
const NO_WEBPACK_REQUIRE = {
|
|
|
|
__non_webpack_require__: true
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
2017-01-03 01:00:08 +08:00
|
|
|
const REPLACEMENT_TYPES = {
|
2017-12-13 18:14:00 +08:00
|
|
|
__webpack_public_path__: "string",
|
|
|
|
__webpack_require__: "function",
|
|
|
|
__webpack_modules__: "object",
|
|
|
|
__webpack_chunk_load__: "function",
|
|
|
|
__webpack_nonce__: "string"
|
2013-07-11 05:20:07 +08:00
|
|
|
};
|
2017-12-13 18:14:00 +08:00
|
|
|
/* eslint-enable camelcase */
|
2017-01-03 01:00:08 +08:00
|
|
|
|
|
|
|
class APIPlugin {
|
|
|
|
apply(compiler) {
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.compilation.tap("APIPlugin", (compilation, {
|
|
|
|
normalModuleFactory
|
|
|
|
}) => {
|
2017-01-03 01:00:08 +08:00
|
|
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
|
|
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
|
|
|
|
|
2017-12-14 17:22:27 +08:00
|
|
|
const handler = parser => {
|
2017-01-03 01:00:08 +08:00
|
|
|
Object.keys(REPLACEMENTS).forEach(key => {
|
2017-12-19 21:35:30 +08:00
|
|
|
parser.hooks.expression.for(key).tap("APIPlugin", NO_WEBPACK_REQUIRE[key] ? ParserHelpers.toConstantDependency(parser, REPLACEMENTS[key]) : ParserHelpers.toConstantDependencyWithWebpackRequire(parser, REPLACEMENTS[key]));
|
|
|
|
parser.hooks.evaluateTypeof.for(key).tap("APIPlugin", ParserHelpers.evaluateToString(REPLACEMENT_TYPES[key]));
|
2016-09-14 18:04:42 +08:00
|
|
|
});
|
2017-12-14 17:22:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
normalModuleFactory.hooks.parser.for("javascript/auto").tap("APIPlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser.for("javascript/dynamic").tap("APIPlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser.for("javascript/esm").tap("APIPlugin", handler);
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
2017-01-03 01:00:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = APIPlugin;
|