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