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 WebpackIsIncludedDependency = require("./dependencies/WebpackIsIncludedDependency");
|
---|
10 | const {
|
---|
11 | toConstantDependency
|
---|
12 | } = require("./javascript/JavascriptParserHelpers");
|
---|
13 |
|
---|
14 | /** @typedef {import("enhanced-resolve/lib/Resolver")} Resolver */
|
---|
15 | /** @typedef {import("./Compiler")} Compiler */
|
---|
16 | /** @typedef {import("./Module")} Module */
|
---|
17 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
18 |
|
---|
19 | class WebpackIsIncludedPlugin {
|
---|
20 | /**
|
---|
21 | * @param {Compiler} compiler the compiler instance
|
---|
22 | * @returns {void}
|
---|
23 | */
|
---|
24 | apply(compiler) {
|
---|
25 | compiler.hooks.compilation.tap(
|
---|
26 | "WebpackIsIncludedPlugin",
|
---|
27 | (compilation, { normalModuleFactory }) => {
|
---|
28 | compilation.dependencyFactories.set(
|
---|
29 | WebpackIsIncludedDependency,
|
---|
30 | new IgnoreErrorModuleFactory(normalModuleFactory)
|
---|
31 | );
|
---|
32 | compilation.dependencyTemplates.set(
|
---|
33 | WebpackIsIncludedDependency,
|
---|
34 | new WebpackIsIncludedDependency.Template()
|
---|
35 | );
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * @param {JavascriptParser} parser the parser
|
---|
39 | * @returns {void}
|
---|
40 | */
|
---|
41 | const handler = parser => {
|
---|
42 | parser.hooks.call
|
---|
43 | .for("__webpack_is_included__")
|
---|
44 | .tap("WebpackIsIncludedPlugin", expr => {
|
---|
45 | if (
|
---|
46 | expr.type !== "CallExpression" ||
|
---|
47 | expr.arguments.length !== 1 ||
|
---|
48 | expr.arguments[0].type === "SpreadElement"
|
---|
49 | )
|
---|
50 | return;
|
---|
51 |
|
---|
52 | const request = parser.evaluateExpression(expr.arguments[0]);
|
---|
53 |
|
---|
54 | if (!request.isString()) return;
|
---|
55 |
|
---|
56 | const dep = new WebpackIsIncludedDependency(
|
---|
57 | request.string,
|
---|
58 | expr.range
|
---|
59 | );
|
---|
60 | dep.loc = expr.loc;
|
---|
61 | parser.state.module.addDependency(dep);
|
---|
62 | return true;
|
---|
63 | });
|
---|
64 | parser.hooks.typeof
|
---|
65 | .for("__webpack_is_included__")
|
---|
66 | .tap(
|
---|
67 | "WebpackIsIncludedPlugin",
|
---|
68 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
69 | );
|
---|
70 | };
|
---|
71 | normalModuleFactory.hooks.parser
|
---|
72 | .for("javascript/auto")
|
---|
73 | .tap("WebpackIsIncludedPlugin", handler);
|
---|
74 | normalModuleFactory.hooks.parser
|
---|
75 | .for("javascript/dynamic")
|
---|
76 | .tap("WebpackIsIncludedPlugin", handler);
|
---|
77 | normalModuleFactory.hooks.parser
|
---|
78 | .for("javascript/esm")
|
---|
79 | .tap("WebpackIsIncludedPlugin", handler);
|
---|
80 | }
|
---|
81 | );
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | module.exports = WebpackIsIncludedPlugin;
|
---|