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 | evaluateToString,
|
---|
15 | toConstantDependency
|
---|
16 | } = require("../javascript/JavascriptParserHelpers");
|
---|
17 |
|
---|
18 | class RequireEnsurePlugin {
|
---|
19 | apply(compiler) {
|
---|
20 | compiler.hooks.compilation.tap(
|
---|
21 | "RequireEnsurePlugin",
|
---|
22 | (compilation, { normalModuleFactory }) => {
|
---|
23 | compilation.dependencyFactories.set(
|
---|
24 | RequireEnsureItemDependency,
|
---|
25 | normalModuleFactory
|
---|
26 | );
|
---|
27 | compilation.dependencyTemplates.set(
|
---|
28 | RequireEnsureItemDependency,
|
---|
29 | new RequireEnsureItemDependency.Template()
|
---|
30 | );
|
---|
31 |
|
---|
32 | compilation.dependencyTemplates.set(
|
---|
33 | RequireEnsureDependency,
|
---|
34 | new RequireEnsureDependency.Template()
|
---|
35 | );
|
---|
36 |
|
---|
37 | const handler = (parser, parserOptions) => {
|
---|
38 | if (
|
---|
39 | parserOptions.requireEnsure !== undefined &&
|
---|
40 | !parserOptions.requireEnsure
|
---|
41 | )
|
---|
42 | return;
|
---|
43 |
|
---|
44 | new RequireEnsureDependenciesBlockParserPlugin().apply(parser);
|
---|
45 | parser.hooks.evaluateTypeof
|
---|
46 | .for("require.ensure")
|
---|
47 | .tap("RequireEnsurePlugin", evaluateToString("function"));
|
---|
48 | parser.hooks.typeof
|
---|
49 | .for("require.ensure")
|
---|
50 | .tap(
|
---|
51 | "RequireEnsurePlugin",
|
---|
52 | toConstantDependency(parser, JSON.stringify("function"))
|
---|
53 | );
|
---|
54 | };
|
---|
55 |
|
---|
56 | normalModuleFactory.hooks.parser
|
---|
57 | .for("javascript/auto")
|
---|
58 | .tap("RequireEnsurePlugin", handler);
|
---|
59 | normalModuleFactory.hooks.parser
|
---|
60 | .for("javascript/dynamic")
|
---|
61 | .tap("RequireEnsurePlugin", handler);
|
---|
62 | }
|
---|
63 | );
|
---|
64 | }
|
---|
65 | }
|
---|
66 | module.exports = RequireEnsurePlugin;
|
---|