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 { getImportAttributes } = require("../javascript/JavascriptParser");
|
---|
9 | const InnerGraph = require("../optimize/InnerGraph");
|
---|
10 | const ConstDependency = require("./ConstDependency");
|
---|
11 | const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
|
---|
12 | const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
|
---|
13 | const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
|
---|
14 | const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
|
---|
15 | const { ExportPresenceModes } = require("./HarmonyImportDependency");
|
---|
16 | const {
|
---|
17 | harmonySpecifierTag
|
---|
18 | } = require("./HarmonyImportDependencyParserPlugin");
|
---|
19 | const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
|
---|
20 |
|
---|
21 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
---|
22 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
---|
23 | /** @typedef {import("../javascript/JavascriptParser").FunctionDeclaration} FunctionDeclaration */
|
---|
24 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
25 |
|
---|
26 | const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;
|
---|
27 |
|
---|
28 | module.exports = class HarmonyExportDependencyParserPlugin {
|
---|
29 | /**
|
---|
30 | * @param {import("../../declarations/WebpackOptions").JavascriptParserOptions} options options
|
---|
31 | */
|
---|
32 | constructor(options) {
|
---|
33 | this.exportPresenceMode =
|
---|
34 | options.reexportExportsPresence !== undefined
|
---|
35 | ? ExportPresenceModes.fromUserOption(options.reexportExportsPresence)
|
---|
36 | : options.exportsPresence !== undefined
|
---|
37 | ? ExportPresenceModes.fromUserOption(options.exportsPresence)
|
---|
38 | : options.strictExportPresence
|
---|
39 | ? ExportPresenceModes.ERROR
|
---|
40 | : ExportPresenceModes.AUTO;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @param {JavascriptParser} parser the parser
|
---|
45 | * @returns {void}
|
---|
46 | */
|
---|
47 | apply(parser) {
|
---|
48 | const { exportPresenceMode } = this;
|
---|
49 | parser.hooks.export.tap(
|
---|
50 | "HarmonyExportDependencyParserPlugin",
|
---|
51 | statement => {
|
---|
52 | const dep = new HarmonyExportHeaderDependency(
|
---|
53 | /** @type {Range | false} */ (
|
---|
54 | statement.declaration && statement.declaration.range
|
---|
55 | ),
|
---|
56 | /** @type {Range} */ (statement.range)
|
---|
57 | );
|
---|
58 | dep.loc = Object.create(
|
---|
59 | /** @type {DependencyLocation} */ (statement.loc)
|
---|
60 | );
|
---|
61 | dep.loc.index = -1;
|
---|
62 | parser.state.module.addPresentationalDependency(dep);
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 | );
|
---|
66 | parser.hooks.exportImport.tap(
|
---|
67 | "HarmonyExportDependencyParserPlugin",
|
---|
68 | (statement, source) => {
|
---|
69 | parser.state.lastHarmonyImportOrder =
|
---|
70 | (parser.state.lastHarmonyImportOrder || 0) + 1;
|
---|
71 | const clearDep = new ConstDependency(
|
---|
72 | "",
|
---|
73 | /** @type {Range} */ (statement.range)
|
---|
74 | );
|
---|
75 | clearDep.loc = /** @type {DependencyLocation} */ (statement.loc);
|
---|
76 | clearDep.loc.index = -1;
|
---|
77 | parser.state.module.addPresentationalDependency(clearDep);
|
---|
78 | const sideEffectDep = new HarmonyImportSideEffectDependency(
|
---|
79 | /** @type {string} */ (source),
|
---|
80 | parser.state.lastHarmonyImportOrder,
|
---|
81 | getImportAttributes(statement)
|
---|
82 | );
|
---|
83 | sideEffectDep.loc = Object.create(
|
---|
84 | /** @type {DependencyLocation} */ (statement.loc)
|
---|
85 | );
|
---|
86 | sideEffectDep.loc.index = -1;
|
---|
87 | parser.state.current.addDependency(sideEffectDep);
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 | );
|
---|
91 | parser.hooks.exportExpression.tap(
|
---|
92 | "HarmonyExportDependencyParserPlugin",
|
---|
93 | (statement, expr) => {
|
---|
94 | const isFunctionDeclaration = expr.type === "FunctionDeclaration";
|
---|
95 | const exprRange = /** @type {Range} */ (expr.range);
|
---|
96 | const statementRange = /** @type {Range} */ (statement.range);
|
---|
97 | const comments = parser.getComments([statementRange[0], exprRange[0]]);
|
---|
98 | const dep = new HarmonyExportExpressionDependency(
|
---|
99 | exprRange,
|
---|
100 | statementRange,
|
---|
101 | comments
|
---|
102 | .map(c => {
|
---|
103 | switch (c.type) {
|
---|
104 | case "Block":
|
---|
105 | return `/*${c.value}*/`;
|
---|
106 | case "Line":
|
---|
107 | return `//${c.value}\n`;
|
---|
108 | }
|
---|
109 | return "";
|
---|
110 | })
|
---|
111 | .join(""),
|
---|
112 | expr.type.endsWith("Declaration") && expr.id
|
---|
113 | ? expr.id.name
|
---|
114 | : isFunctionDeclaration
|
---|
115 | ? {
|
---|
116 | range: [
|
---|
117 | exprRange[0],
|
---|
118 | expr.params.length > 0
|
---|
119 | ? /** @type {Range} */ (expr.params[0].range)[0]
|
---|
120 | : /** @type {Range} */ (expr.body.range)[0]
|
---|
121 | ],
|
---|
122 | prefix: `${expr.async ? "async " : ""}function${
|
---|
123 | expr.generator ? "*" : ""
|
---|
124 | } `,
|
---|
125 | suffix: `(${expr.params.length > 0 ? "" : ") "}`
|
---|
126 | }
|
---|
127 | : undefined
|
---|
128 | );
|
---|
129 | dep.loc = Object.create(
|
---|
130 | /** @type {DependencyLocation} */ (statement.loc)
|
---|
131 | );
|
---|
132 | dep.loc.index = -1;
|
---|
133 | parser.state.current.addDependency(dep);
|
---|
134 | InnerGraph.addVariableUsage(
|
---|
135 | parser,
|
---|
136 | expr.type.endsWith("Declaration") && expr.id
|
---|
137 | ? expr.id.name
|
---|
138 | : "*default*",
|
---|
139 | "default"
|
---|
140 | );
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 | );
|
---|
144 | parser.hooks.exportSpecifier.tap(
|
---|
145 | "HarmonyExportDependencyParserPlugin",
|
---|
146 | (statement, id, name, idx) => {
|
---|
147 | const settings = parser.getTagData(id, harmonySpecifierTag);
|
---|
148 | const harmonyNamedExports = (parser.state.harmonyNamedExports =
|
---|
149 | parser.state.harmonyNamedExports || new Set());
|
---|
150 | harmonyNamedExports.add(name);
|
---|
151 | InnerGraph.addVariableUsage(parser, id, name);
|
---|
152 | const dep = settings
|
---|
153 | ? new HarmonyExportImportedSpecifierDependency(
|
---|
154 | settings.source,
|
---|
155 | settings.sourceOrder,
|
---|
156 | settings.ids,
|
---|
157 | name,
|
---|
158 | harmonyNamedExports,
|
---|
159 | null,
|
---|
160 | exportPresenceMode,
|
---|
161 | null,
|
---|
162 | settings.assertions
|
---|
163 | )
|
---|
164 | : new HarmonyExportSpecifierDependency(id, name);
|
---|
165 | dep.loc = Object.create(
|
---|
166 | /** @type {DependencyLocation} */ (statement.loc)
|
---|
167 | );
|
---|
168 | dep.loc.index = idx;
|
---|
169 | const isAsiSafe = !parser.isAsiPosition(
|
---|
170 | /** @type {Range} */
|
---|
171 | (statement.range)[0]
|
---|
172 | );
|
---|
173 | if (!isAsiSafe) {
|
---|
174 | parser.setAsiPosition(/** @type {Range} */ (statement.range)[1]);
|
---|
175 | }
|
---|
176 | parser.state.current.addDependency(dep);
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 | );
|
---|
180 | parser.hooks.exportImportSpecifier.tap(
|
---|
181 | "HarmonyExportDependencyParserPlugin",
|
---|
182 | (statement, source, id, name, idx) => {
|
---|
183 | const harmonyNamedExports = (parser.state.harmonyNamedExports =
|
---|
184 | parser.state.harmonyNamedExports || new Set());
|
---|
185 | let harmonyStarExports = null;
|
---|
186 | if (name) {
|
---|
187 | harmonyNamedExports.add(name);
|
---|
188 | } else {
|
---|
189 | harmonyStarExports = parser.state.harmonyStarExports =
|
---|
190 | parser.state.harmonyStarExports || new HarmonyStarExportsList();
|
---|
191 | }
|
---|
192 | const dep = new HarmonyExportImportedSpecifierDependency(
|
---|
193 | /** @type {string} */ (source),
|
---|
194 | parser.state.lastHarmonyImportOrder,
|
---|
195 | id ? [id] : [],
|
---|
196 | name,
|
---|
197 | harmonyNamedExports,
|
---|
198 | harmonyStarExports && harmonyStarExports.slice(),
|
---|
199 | exportPresenceMode,
|
---|
200 | harmonyStarExports
|
---|
201 | );
|
---|
202 | if (harmonyStarExports) {
|
---|
203 | harmonyStarExports.push(dep);
|
---|
204 | }
|
---|
205 | dep.loc = Object.create(
|
---|
206 | /** @type {DependencyLocation} */ (statement.loc)
|
---|
207 | );
|
---|
208 | dep.loc.index = idx;
|
---|
209 | const isAsiSafe = !parser.isAsiPosition(
|
---|
210 | /** @type {Range} */
|
---|
211 | (statement.range)[0]
|
---|
212 | );
|
---|
213 | if (!isAsiSafe) {
|
---|
214 | parser.setAsiPosition(/** @type {Range} */ (statement.range)[1]);
|
---|
215 | }
|
---|
216 | parser.state.current.addDependency(dep);
|
---|
217 | return true;
|
---|
218 | }
|
---|
219 | );
|
---|
220 | }
|
---|
221 | };
|
---|