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 HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
|
---|
9 | const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
|
---|
10 | const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
|
---|
11 | const HarmonyEvaluatedImportSpecifierDependency = require("./HarmonyEvaluatedImportSpecifierDependency");
|
---|
12 | const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
|
---|
13 | const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
|
---|
14 | const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
|
---|
15 | const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
|
---|
16 | const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
|
---|
17 | const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
|
---|
18 |
|
---|
19 | const {
|
---|
20 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
21 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
22 | } = require("../ModuleTypeConstants");
|
---|
23 | const HarmonyDetectionParserPlugin = require("./HarmonyDetectionParserPlugin");
|
---|
24 | const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyParserPlugin");
|
---|
25 | const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin");
|
---|
26 | const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlugin");
|
---|
27 |
|
---|
28 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
29 | /** @typedef {import("../Compiler")} Compiler */
|
---|
30 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
31 |
|
---|
32 | const PLUGIN_NAME = "HarmonyModulesPlugin";
|
---|
33 |
|
---|
34 | /** @typedef {{ topLevelAwait?: boolean }} HarmonyModulesPluginOptions */
|
---|
35 |
|
---|
36 | class HarmonyModulesPlugin {
|
---|
37 | /**
|
---|
38 | * @param {HarmonyModulesPluginOptions} options options
|
---|
39 | */
|
---|
40 | constructor(options) {
|
---|
41 | this.options = options;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Apply the plugin
|
---|
46 | * @param {Compiler} compiler the compiler instance
|
---|
47 | * @returns {void}
|
---|
48 | */
|
---|
49 | apply(compiler) {
|
---|
50 | compiler.hooks.compilation.tap(
|
---|
51 | PLUGIN_NAME,
|
---|
52 | (compilation, { normalModuleFactory }) => {
|
---|
53 | compilation.dependencyTemplates.set(
|
---|
54 | HarmonyCompatibilityDependency,
|
---|
55 | new HarmonyCompatibilityDependency.Template()
|
---|
56 | );
|
---|
57 |
|
---|
58 | compilation.dependencyFactories.set(
|
---|
59 | HarmonyImportSideEffectDependency,
|
---|
60 | normalModuleFactory
|
---|
61 | );
|
---|
62 | compilation.dependencyTemplates.set(
|
---|
63 | HarmonyImportSideEffectDependency,
|
---|
64 | new HarmonyImportSideEffectDependency.Template()
|
---|
65 | );
|
---|
66 |
|
---|
67 | compilation.dependencyFactories.set(
|
---|
68 | HarmonyImportSpecifierDependency,
|
---|
69 | normalModuleFactory
|
---|
70 | );
|
---|
71 | compilation.dependencyTemplates.set(
|
---|
72 | HarmonyImportSpecifierDependency,
|
---|
73 | new HarmonyImportSpecifierDependency.Template()
|
---|
74 | );
|
---|
75 |
|
---|
76 | compilation.dependencyFactories.set(
|
---|
77 | HarmonyEvaluatedImportSpecifierDependency,
|
---|
78 | normalModuleFactory
|
---|
79 | );
|
---|
80 | compilation.dependencyTemplates.set(
|
---|
81 | HarmonyEvaluatedImportSpecifierDependency,
|
---|
82 | new HarmonyEvaluatedImportSpecifierDependency.Template()
|
---|
83 | );
|
---|
84 |
|
---|
85 | compilation.dependencyTemplates.set(
|
---|
86 | HarmonyExportHeaderDependency,
|
---|
87 | new HarmonyExportHeaderDependency.Template()
|
---|
88 | );
|
---|
89 |
|
---|
90 | compilation.dependencyTemplates.set(
|
---|
91 | HarmonyExportExpressionDependency,
|
---|
92 | new HarmonyExportExpressionDependency.Template()
|
---|
93 | );
|
---|
94 |
|
---|
95 | compilation.dependencyTemplates.set(
|
---|
96 | HarmonyExportSpecifierDependency,
|
---|
97 | new HarmonyExportSpecifierDependency.Template()
|
---|
98 | );
|
---|
99 |
|
---|
100 | compilation.dependencyFactories.set(
|
---|
101 | HarmonyExportImportedSpecifierDependency,
|
---|
102 | normalModuleFactory
|
---|
103 | );
|
---|
104 | compilation.dependencyTemplates.set(
|
---|
105 | HarmonyExportImportedSpecifierDependency,
|
---|
106 | new HarmonyExportImportedSpecifierDependency.Template()
|
---|
107 | );
|
---|
108 |
|
---|
109 | compilation.dependencyTemplates.set(
|
---|
110 | HarmonyAcceptDependency,
|
---|
111 | new HarmonyAcceptDependency.Template()
|
---|
112 | );
|
---|
113 |
|
---|
114 | compilation.dependencyFactories.set(
|
---|
115 | HarmonyAcceptImportDependency,
|
---|
116 | normalModuleFactory
|
---|
117 | );
|
---|
118 | compilation.dependencyTemplates.set(
|
---|
119 | HarmonyAcceptImportDependency,
|
---|
120 | new HarmonyAcceptImportDependency.Template()
|
---|
121 | );
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * @param {Parser} parser parser parser
|
---|
125 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
126 | * @returns {void}
|
---|
127 | */
|
---|
128 | const handler = (parser, parserOptions) => {
|
---|
129 | // TODO webpack 6: rename harmony to esm or module
|
---|
130 | if (parserOptions.harmony !== undefined && !parserOptions.harmony)
|
---|
131 | return;
|
---|
132 |
|
---|
133 | new HarmonyDetectionParserPlugin(this.options).apply(parser);
|
---|
134 | new HarmonyImportDependencyParserPlugin(parserOptions).apply(parser);
|
---|
135 | new HarmonyExportDependencyParserPlugin(parserOptions).apply(parser);
|
---|
136 | new HarmonyTopLevelThisParserPlugin().apply(parser);
|
---|
137 | };
|
---|
138 |
|
---|
139 | normalModuleFactory.hooks.parser
|
---|
140 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
141 | .tap(PLUGIN_NAME, handler);
|
---|
142 | normalModuleFactory.hooks.parser
|
---|
143 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
144 | .tap(PLUGIN_NAME, handler);
|
---|
145 | }
|
---|
146 | );
|
---|
147 | }
|
---|
148 | }
|
---|
149 | module.exports = HarmonyModulesPlugin;
|
---|