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 ImportContextDependency = require("./ImportContextDependency");
|
---|
9 | const ImportDependency = require("./ImportDependency");
|
---|
10 | const ImportEagerDependency = require("./ImportEagerDependency");
|
---|
11 | const ImportParserPlugin = require("./ImportParserPlugin");
|
---|
12 | const ImportWeakDependency = require("./ImportWeakDependency");
|
---|
13 |
|
---|
14 | /** @typedef {import("../Compiler")} Compiler */
|
---|
15 |
|
---|
16 | class ImportPlugin {
|
---|
17 | /**
|
---|
18 | * Apply the plugin
|
---|
19 | * @param {Compiler} compiler the compiler instance
|
---|
20 | * @returns {void}
|
---|
21 | */
|
---|
22 | apply(compiler) {
|
---|
23 | compiler.hooks.compilation.tap(
|
---|
24 | "ImportPlugin",
|
---|
25 | (compilation, { contextModuleFactory, normalModuleFactory }) => {
|
---|
26 | compilation.dependencyFactories.set(
|
---|
27 | ImportDependency,
|
---|
28 | normalModuleFactory
|
---|
29 | );
|
---|
30 | compilation.dependencyTemplates.set(
|
---|
31 | ImportDependency,
|
---|
32 | new ImportDependency.Template()
|
---|
33 | );
|
---|
34 |
|
---|
35 | compilation.dependencyFactories.set(
|
---|
36 | ImportEagerDependency,
|
---|
37 | normalModuleFactory
|
---|
38 | );
|
---|
39 | compilation.dependencyTemplates.set(
|
---|
40 | ImportEagerDependency,
|
---|
41 | new ImportEagerDependency.Template()
|
---|
42 | );
|
---|
43 |
|
---|
44 | compilation.dependencyFactories.set(
|
---|
45 | ImportWeakDependency,
|
---|
46 | normalModuleFactory
|
---|
47 | );
|
---|
48 | compilation.dependencyTemplates.set(
|
---|
49 | ImportWeakDependency,
|
---|
50 | new ImportWeakDependency.Template()
|
---|
51 | );
|
---|
52 |
|
---|
53 | compilation.dependencyFactories.set(
|
---|
54 | ImportContextDependency,
|
---|
55 | contextModuleFactory
|
---|
56 | );
|
---|
57 | compilation.dependencyTemplates.set(
|
---|
58 | ImportContextDependency,
|
---|
59 | new ImportContextDependency.Template()
|
---|
60 | );
|
---|
61 |
|
---|
62 | const handler = (parser, parserOptions) => {
|
---|
63 | if (parserOptions.import !== undefined && !parserOptions.import)
|
---|
64 | return;
|
---|
65 |
|
---|
66 | new ImportParserPlugin(parserOptions).apply(parser);
|
---|
67 | };
|
---|
68 |
|
---|
69 | normalModuleFactory.hooks.parser
|
---|
70 | .for("javascript/auto")
|
---|
71 | .tap("ImportPlugin", handler);
|
---|
72 | normalModuleFactory.hooks.parser
|
---|
73 | .for("javascript/dynamic")
|
---|
74 | .tap("ImportPlugin", handler);
|
---|
75 | normalModuleFactory.hooks.parser
|
---|
76 | .for("javascript/esm")
|
---|
77 | .tap("ImportPlugin", handler);
|
---|
78 | }
|
---|
79 | );
|
---|
80 | }
|
---|
81 | }
|
---|
82 | module.exports = ImportPlugin;
|
---|