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