[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Ivan Kopeykin @vankop
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const IgnoreErrorModuleFactory = require("./IgnoreErrorModuleFactory");
|
---|
| 9 | const {
|
---|
| 10 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
| 11 | JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
---|
| 12 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
| 13 | } = require("./ModuleTypeConstants");
|
---|
| 14 | const WebpackIsIncludedDependency = require("./dependencies/WebpackIsIncludedDependency");
|
---|
| 15 | const {
|
---|
| 16 | toConstantDependency
|
---|
| 17 | } = require("./javascript/JavascriptParserHelpers");
|
---|
| 18 |
|
---|
| 19 | /** @typedef {import("enhanced-resolve").Resolver} Resolver */
|
---|
| 20 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 21 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
---|
| 22 | /** @typedef {import("./Module")} Module */
|
---|
| 23 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
| 24 | /** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
---|
| 25 |
|
---|
| 26 | const PLUGIN_NAME = "WebpackIsIncludedPlugin";
|
---|
| 27 |
|
---|
| 28 | class WebpackIsIncludedPlugin {
|
---|
| 29 | /**
|
---|
| 30 | * @param {Compiler} compiler the compiler instance
|
---|
| 31 | * @returns {void}
|
---|
| 32 | */
|
---|
| 33 | apply(compiler) {
|
---|
| 34 | compiler.hooks.compilation.tap(
|
---|
| 35 | PLUGIN_NAME,
|
---|
| 36 | (compilation, { normalModuleFactory }) => {
|
---|
| 37 | compilation.dependencyFactories.set(
|
---|
| 38 | WebpackIsIncludedDependency,
|
---|
| 39 | new IgnoreErrorModuleFactory(normalModuleFactory)
|
---|
| 40 | );
|
---|
| 41 | compilation.dependencyTemplates.set(
|
---|
| 42 | WebpackIsIncludedDependency,
|
---|
| 43 | new WebpackIsIncludedDependency.Template()
|
---|
| 44 | );
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * @param {JavascriptParser} parser the parser
|
---|
| 48 | * @returns {void}
|
---|
| 49 | */
|
---|
| 50 | const handler = parser => {
|
---|
| 51 | parser.hooks.call
|
---|
| 52 | .for("__webpack_is_included__")
|
---|
| 53 | .tap(PLUGIN_NAME, expr => {
|
---|
| 54 | if (
|
---|
| 55 | expr.type !== "CallExpression" ||
|
---|
| 56 | expr.arguments.length !== 1 ||
|
---|
| 57 | expr.arguments[0].type === "SpreadElement"
|
---|
| 58 | )
|
---|
| 59 | return;
|
---|
| 60 |
|
---|
| 61 | const request = parser.evaluateExpression(expr.arguments[0]);
|
---|
| 62 |
|
---|
| 63 | if (!request.isString()) return;
|
---|
| 64 |
|
---|
| 65 | const dep = new WebpackIsIncludedDependency(
|
---|
| 66 | /** @type {string} */ (request.string),
|
---|
| 67 | /** @type {Range} */ (expr.range)
|
---|
| 68 | );
|
---|
| 69 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 70 | parser.state.module.addDependency(dep);
|
---|
| 71 | return true;
|
---|
| 72 | });
|
---|
| 73 | parser.hooks.typeof
|
---|
| 74 | .for("__webpack_is_included__")
|
---|
| 75 | .tap(
|
---|
| 76 | PLUGIN_NAME,
|
---|
| 77 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
| 78 | );
|
---|
| 79 | };
|
---|
| 80 | normalModuleFactory.hooks.parser
|
---|
| 81 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
| 82 | .tap(PLUGIN_NAME, handler);
|
---|
| 83 | normalModuleFactory.hooks.parser
|
---|
| 84 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
| 85 | .tap(PLUGIN_NAME, handler);
|
---|
| 86 | normalModuleFactory.hooks.parser
|
---|
| 87 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
| 88 | .tap(PLUGIN_NAME, handler);
|
---|
| 89 | }
|
---|
| 90 | );
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | module.exports = WebpackIsIncludedPlugin;
|
---|