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 HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin");
|
---|
9 | const InnerGraph = require("../optimize/InnerGraph");
|
---|
10 | const ConstDependency = require("./ConstDependency");
|
---|
11 | const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
|
---|
12 | const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
|
---|
13 | const HarmonyExports = require("./HarmonyExports");
|
---|
14 | const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
|
---|
15 | const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
|
---|
16 |
|
---|
17 | /** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */
|
---|
18 | /** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */
|
---|
19 | /** @typedef {import("estree").Identifier} Identifier */
|
---|
20 | /** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
|
---|
21 | /** @typedef {import("estree").ImportExpression} ImportExpression */
|
---|
22 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
---|
23 | /** @typedef {import("../optimize/InnerGraph").InnerGraph} InnerGraph */
|
---|
24 | /** @typedef {import("../optimize/InnerGraph").TopLevelSymbol} TopLevelSymbol */
|
---|
25 | /** @typedef {import("./HarmonyImportDependency")} HarmonyImportDependency */
|
---|
26 |
|
---|
27 | const harmonySpecifierTag = Symbol("harmony import");
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @typedef {Object} HarmonySettings
|
---|
31 | * @property {string[]} ids
|
---|
32 | * @property {string} source
|
---|
33 | * @property {number} sourceOrder
|
---|
34 | * @property {string} name
|
---|
35 | * @property {boolean} await
|
---|
36 | * @property {Record<string, any> | undefined} assertions
|
---|
37 | */
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions
|
---|
41 | * @returns {Record<string, any> | undefined} assertions
|
---|
42 | */
|
---|
43 | function getAssertions(node) {
|
---|
44 | // TODO remove cast when @types/estree has been updated to import assertions
|
---|
45 | const assertions = /** @type {{ assertions?: ImportAttributeNode[] }} */ (
|
---|
46 | node
|
---|
47 | ).assertions;
|
---|
48 | if (assertions === undefined) {
|
---|
49 | return undefined;
|
---|
50 | }
|
---|
51 | const result = {};
|
---|
52 | for (const assertion of assertions) {
|
---|
53 | const key =
|
---|
54 | assertion.key.type === "Identifier"
|
---|
55 | ? assertion.key.name
|
---|
56 | : assertion.key.value;
|
---|
57 | result[key] = assertion.value.value;
|
---|
58 | }
|
---|
59 | return result;
|
---|
60 | }
|
---|
61 |
|
---|
62 | module.exports = class HarmonyImportDependencyParserPlugin {
|
---|
63 | constructor(options) {
|
---|
64 | this.strictExportPresence = options.strictExportPresence;
|
---|
65 | this.strictThisContextOnImports = options.strictThisContextOnImports;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * @param {JavascriptParser} parser the parser
|
---|
70 | * @returns {void}
|
---|
71 | */
|
---|
72 | apply(parser) {
|
---|
73 | parser.hooks.isPure
|
---|
74 | .for("Identifier")
|
---|
75 | .tap("HarmonyImportDependencyParserPlugin", expression => {
|
---|
76 | const expr = /** @type {Identifier} */ (expression);
|
---|
77 | if (
|
---|
78 | parser.isVariableDefined(expr.name) ||
|
---|
79 | parser.getTagData(expr.name, harmonySpecifierTag)
|
---|
80 | ) {
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 | });
|
---|
84 | parser.hooks.import.tap(
|
---|
85 | "HarmonyImportDependencyParserPlugin",
|
---|
86 | (statement, source) => {
|
---|
87 | parser.state.lastHarmonyImportOrder =
|
---|
88 | (parser.state.lastHarmonyImportOrder || 0) + 1;
|
---|
89 | const clearDep = new ConstDependency(
|
---|
90 | parser.isAsiPosition(statement.range[0]) ? ";" : "",
|
---|
91 | statement.range
|
---|
92 | );
|
---|
93 | clearDep.loc = statement.loc;
|
---|
94 | parser.state.module.addPresentationalDependency(clearDep);
|
---|
95 | parser.unsetAsiPosition(statement.range[1]);
|
---|
96 | const assertions = getAssertions(statement);
|
---|
97 | const sideEffectDep = new HarmonyImportSideEffectDependency(
|
---|
98 | source,
|
---|
99 | parser.state.lastHarmonyImportOrder,
|
---|
100 | assertions
|
---|
101 | );
|
---|
102 | sideEffectDep.loc = statement.loc;
|
---|
103 | parser.state.module.addDependency(sideEffectDep);
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | );
|
---|
107 | parser.hooks.importSpecifier.tap(
|
---|
108 | "HarmonyImportDependencyParserPlugin",
|
---|
109 | (statement, source, id, name) => {
|
---|
110 | const ids = id === null ? [] : [id];
|
---|
111 | parser.tagVariable(name, harmonySpecifierTag, {
|
---|
112 | name,
|
---|
113 | source,
|
---|
114 | ids,
|
---|
115 | sourceOrder: parser.state.lastHarmonyImportOrder,
|
---|
116 | assertions: getAssertions(statement)
|
---|
117 | });
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 | );
|
---|
121 | parser.hooks.expression
|
---|
122 | .for(harmonySpecifierTag)
|
---|
123 | .tap("HarmonyImportDependencyParserPlugin", expr => {
|
---|
124 | const settings = /** @type {HarmonySettings} */ (parser.currentTagData);
|
---|
125 | const dep = new HarmonyImportSpecifierDependency(
|
---|
126 | settings.source,
|
---|
127 | settings.sourceOrder,
|
---|
128 | settings.ids,
|
---|
129 | settings.name,
|
---|
130 | expr.range,
|
---|
131 | this.strictExportPresence,
|
---|
132 | settings.assertions
|
---|
133 | );
|
---|
134 | dep.shorthand = parser.scope.inShorthand;
|
---|
135 | dep.directImport = true;
|
---|
136 | dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
|
---|
137 | dep.loc = expr.loc;
|
---|
138 | parser.state.module.addDependency(dep);
|
---|
139 | InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
|
---|
140 | return true;
|
---|
141 | });
|
---|
142 | parser.hooks.expressionMemberChain
|
---|
143 | .for(harmonySpecifierTag)
|
---|
144 | .tap("HarmonyImportDependencyParserPlugin", (expr, members) => {
|
---|
145 | const settings = /** @type {HarmonySettings} */ (parser.currentTagData);
|
---|
146 | const ids = settings.ids.concat(members);
|
---|
147 | const dep = new HarmonyImportSpecifierDependency(
|
---|
148 | settings.source,
|
---|
149 | settings.sourceOrder,
|
---|
150 | ids,
|
---|
151 | settings.name,
|
---|
152 | expr.range,
|
---|
153 | this.strictExportPresence,
|
---|
154 | settings.assertions
|
---|
155 | );
|
---|
156 | dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
|
---|
157 | dep.loc = expr.loc;
|
---|
158 | parser.state.module.addDependency(dep);
|
---|
159 | InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
|
---|
160 | return true;
|
---|
161 | });
|
---|
162 | parser.hooks.callMemberChain
|
---|
163 | .for(harmonySpecifierTag)
|
---|
164 | .tap("HarmonyImportDependencyParserPlugin", (expr, members) => {
|
---|
165 | const { arguments: args, callee } = expr;
|
---|
166 | const settings = /** @type {HarmonySettings} */ (parser.currentTagData);
|
---|
167 | const ids = settings.ids.concat(members);
|
---|
168 | const dep = new HarmonyImportSpecifierDependency(
|
---|
169 | settings.source,
|
---|
170 | settings.sourceOrder,
|
---|
171 | ids,
|
---|
172 | settings.name,
|
---|
173 | callee.range,
|
---|
174 | this.strictExportPresence,
|
---|
175 | settings.assertions
|
---|
176 | );
|
---|
177 | dep.directImport = members.length === 0;
|
---|
178 | dep.call = true;
|
---|
179 | dep.asiSafe = !parser.isAsiPosition(callee.range[0]);
|
---|
180 | // only in case when we strictly follow the spec we need a special case here
|
---|
181 | dep.namespaceObjectAsContext =
|
---|
182 | members.length > 0 && this.strictThisContextOnImports;
|
---|
183 | dep.loc = callee.loc;
|
---|
184 | parser.state.module.addDependency(dep);
|
---|
185 | if (args) parser.walkExpressions(args);
|
---|
186 | InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
|
---|
187 | return true;
|
---|
188 | });
|
---|
189 | const { hotAcceptCallback, hotAcceptWithoutCallback } =
|
---|
190 | HotModuleReplacementPlugin.getParserHooks(parser);
|
---|
191 | hotAcceptCallback.tap(
|
---|
192 | "HarmonyImportDependencyParserPlugin",
|
---|
193 | (expr, requests) => {
|
---|
194 | if (!HarmonyExports.isEnabled(parser.state)) {
|
---|
195 | // This is not a harmony module, skip it
|
---|
196 | return;
|
---|
197 | }
|
---|
198 | const dependencies = requests.map(request => {
|
---|
199 | const dep = new HarmonyAcceptImportDependency(request);
|
---|
200 | dep.loc = expr.loc;
|
---|
201 | parser.state.module.addDependency(dep);
|
---|
202 | return dep;
|
---|
203 | });
|
---|
204 | if (dependencies.length > 0) {
|
---|
205 | const dep = new HarmonyAcceptDependency(
|
---|
206 | expr.range,
|
---|
207 | dependencies,
|
---|
208 | true
|
---|
209 | );
|
---|
210 | dep.loc = expr.loc;
|
---|
211 | parser.state.module.addDependency(dep);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | );
|
---|
215 | hotAcceptWithoutCallback.tap(
|
---|
216 | "HarmonyImportDependencyParserPlugin",
|
---|
217 | (expr, requests) => {
|
---|
218 | if (!HarmonyExports.isEnabled(parser.state)) {
|
---|
219 | // This is not a harmony module, skip it
|
---|
220 | return;
|
---|
221 | }
|
---|
222 | const dependencies = requests.map(request => {
|
---|
223 | const dep = new HarmonyAcceptImportDependency(request);
|
---|
224 | dep.loc = expr.loc;
|
---|
225 | parser.state.module.addDependency(dep);
|
---|
226 | return dep;
|
---|
227 | });
|
---|
228 | if (dependencies.length > 0) {
|
---|
229 | const dep = new HarmonyAcceptDependency(
|
---|
230 | expr.range,
|
---|
231 | dependencies,
|
---|
232 | false
|
---|
233 | );
|
---|
234 | dep.loc = expr.loc;
|
---|
235 | parser.state.module.addDependency(dep);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | );
|
---|
239 | }
|
---|
240 | };
|
---|
241 |
|
---|
242 | module.exports.harmonySpecifierTag = harmonySpecifierTag;
|
---|
243 | module.exports.getAssertions = getAssertions;
|
---|