1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const RequireEnsureDependency = require("./RequireEnsureDependency");
|
---|
9 | const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
|
---|
10 |
|
---|
11 | const RequireEnsureDependenciesBlockParserPlugin = require("./RequireEnsureDependenciesBlockParserPlugin");
|
---|
12 |
|
---|
13 | const {
|
---|
14 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
15 | JAVASCRIPT_MODULE_TYPE_DYNAMIC
|
---|
16 | } = require("../ModuleTypeConstants");
|
---|
17 | const {
|
---|
18 | evaluateToString,
|
---|
19 | toConstantDependency
|
---|
20 | } = require("../javascript/JavascriptParserHelpers");
|
---|
21 |
|
---|
22 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
23 | /** @typedef {import("../Compiler")} Compiler */
|
---|
24 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
25 |
|
---|
26 | const PLUGIN_NAME = "RequireEnsurePlugin";
|
---|
27 |
|
---|
28 | class RequireEnsurePlugin {
|
---|
29 | /**
|
---|
30 | * Apply the plugin
|
---|
31 | * @param {Compiler} compiler the compiler instance
|
---|
32 | * @returns {void}
|
---|
33 | */
|
---|
34 | apply(compiler) {
|
---|
35 | compiler.hooks.compilation.tap(
|
---|
36 | PLUGIN_NAME,
|
---|
37 | (compilation, { normalModuleFactory }) => {
|
---|
38 | compilation.dependencyFactories.set(
|
---|
39 | RequireEnsureItemDependency,
|
---|
40 | normalModuleFactory
|
---|
41 | );
|
---|
42 | compilation.dependencyTemplates.set(
|
---|
43 | RequireEnsureItemDependency,
|
---|
44 | new RequireEnsureItemDependency.Template()
|
---|
45 | );
|
---|
46 |
|
---|
47 | compilation.dependencyTemplates.set(
|
---|
48 | RequireEnsureDependency,
|
---|
49 | new RequireEnsureDependency.Template()
|
---|
50 | );
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @param {Parser} parser parser parser
|
---|
54 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
55 | * @returns {void}
|
---|
56 | */
|
---|
57 | const handler = (parser, parserOptions) => {
|
---|
58 | if (
|
---|
59 | parserOptions.requireEnsure !== undefined &&
|
---|
60 | !parserOptions.requireEnsure
|
---|
61 | )
|
---|
62 | return;
|
---|
63 |
|
---|
64 | new RequireEnsureDependenciesBlockParserPlugin().apply(parser);
|
---|
65 | parser.hooks.evaluateTypeof
|
---|
66 | .for("require.ensure")
|
---|
67 | .tap(PLUGIN_NAME, evaluateToString("function"));
|
---|
68 | parser.hooks.typeof
|
---|
69 | .for("require.ensure")
|
---|
70 | .tap(
|
---|
71 | PLUGIN_NAME,
|
---|
72 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
73 | );
|
---|
74 | };
|
---|
75 |
|
---|
76 | normalModuleFactory.hooks.parser
|
---|
77 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
78 | .tap(PLUGIN_NAME, handler);
|
---|
79 | normalModuleFactory.hooks.parser
|
---|
80 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
81 | .tap(PLUGIN_NAME, handler);
|
---|
82 | }
|
---|
83 | );
|
---|
84 | }
|
---|
85 | }
|
---|
86 | module.exports = RequireEnsurePlugin;
|
---|