-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ImportExpression.ts
414 lines (378 loc) · 12 KB
/
ImportExpression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import type MagicString from 'magic-string';
import ExternalModule from '../../ExternalModule';
import type Module from '../../Module';
import type { GetInterop, NormalizedOutputOptions } from '../../rollup/types';
import type { PluginDriver } from '../../utils/PluginDriver';
import { EMPTY_ARRAY } from '../../utils/blank';
import type { GenerateCodeSnippets } from '../../utils/generateCodeSnippets';
import {
INTEROP_NAMESPACE_DEFAULT_ONLY_VARIABLE,
namespaceInteropHelpersByInteropType
} from '../../utils/interopHelpers';
import { findFirstOccurrenceOutsideComment, type RenderOptions } from '../../utils/renderHelpers';
import type { InclusionContext } from '../ExecutionContext';
import type ChildScope from '../scopes/ChildScope';
import type NamespaceVariable from '../variables/NamespaceVariable';
import ArrowFunctionExpression from './ArrowFunctionExpression';
import AwaitExpression from './AwaitExpression';
import CallExpression from './CallExpression';
import ExpressionStatement from './ExpressionStatement';
import FunctionExpression from './FunctionExpression';
import Identifier from './Identifier';
import MemberExpression from './MemberExpression';
import type * as NodeType from './NodeType';
import type ObjectExpression from './ObjectExpression';
import ObjectPattern from './ObjectPattern';
import VariableDeclarator from './VariableDeclarator';
import {
type ExpressionNode,
type GenericEsTreeNode,
type IncludeChildren,
NodeBase
} from './shared/Node';
interface DynamicImportMechanism {
left: string;
right: string;
}
// TODO once ImportExpression follows official ESTree specs with "null" as
// default, keys.ts should be updated
export default class ImportExpression extends NodeBase {
declare arguments: ObjectExpression[] | undefined;
inlineNamespace: NamespaceVariable | null = null;
declare source: ExpressionNode;
declare type: NodeType.tImportExpression;
private assertions: string | null | true = null;
private mechanism: DynamicImportMechanism | null = null;
private namespaceExportName: string | false | undefined = undefined;
private resolution: Module | ExternalModule | string | null = null;
private resolutionString: string | null = null;
// Do not bind assertions
bind(): void {
this.source.bind();
}
/**
* Get imported variables for deterministic usage, valid cases are:
*
* - `const { foo } = await import('bar')`.
* - `(await import('bar')).foo`
* - `import('bar').then(({ foo }) => {})`
*
* Returns empty array if it's side-effect only import.
* Returns undefined if it's not fully deterministic.
*/
getDeterministicImportedNames(): readonly string[] | undefined {
const parent1 = this.parent;
// Side-effect only: import('bar')
if (parent1 instanceof ExpressionStatement) {
return EMPTY_ARRAY;
}
if (parent1 instanceof AwaitExpression) {
const parent2 = parent1.parent;
// Side-effect only: await import('bar')
if (parent2 instanceof ExpressionStatement) {
return EMPTY_ARRAY;
}
// Case 1: const { foo } = await import('bar')
if (parent2 instanceof VariableDeclarator) {
const declaration = parent2.id;
return declaration instanceof ObjectPattern
? getDeterministicObjectDestructure(declaration)
: undefined;
}
// Case 2: (await import('bar')).foo
if (parent2 instanceof MemberExpression) {
const id = parent2.property;
if (!parent2.computed && id instanceof Identifier) {
return [id.name];
}
}
return;
}
// Case 3: import('bar').then(({ foo }) => {})
if (parent1 instanceof MemberExpression) {
const callExpression = parent1.parent;
const property = parent1.property;
if (!(callExpression instanceof CallExpression) || !(property instanceof Identifier)) {
return;
}
const memberName = property.name;
// side-effect only, when only chaining .catch or .finally
if (
callExpression.parent instanceof ExpressionStatement &&
['catch', 'finally'].includes(memberName)
) {
return EMPTY_ARRAY;
}
if (memberName !== 'then') return;
// Side-effect only: import('bar').then()
if (callExpression.arguments.length === 0) {
return EMPTY_ARRAY;
}
const argument = callExpression.arguments[0];
if (
callExpression.arguments.length !== 1 ||
!(argument instanceof ArrowFunctionExpression || argument instanceof FunctionExpression)
) {
return;
}
// Side-effect only: import('bar').then(() => {})
if (argument.params.length === 0) {
return EMPTY_ARRAY;
}
const declaration = argument.params[0];
if (argument.params.length === 1 && declaration instanceof ObjectPattern) {
return getDeterministicObjectDestructure(declaration);
}
return;
}
}
hasEffects(): boolean {
return true;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.included) {
this.included = true;
this.context.includeDynamicImport(this);
this.scope.addAccessedDynamicImport(this);
}
this.source.include(context, includeChildrenRecursively);
}
initialise(): void {
this.context.addDynamicImport(this);
}
parseNode(esTreeNode: GenericEsTreeNode): void {
// Keep the source AST to be used by renderDynamicImport
super.parseNode(esTreeNode, ['source']);
}
render(code: MagicString, options: RenderOptions): void {
const {
snippets: { _, getDirectReturnFunction, getObject, getPropertyAccess }
} = options;
if (this.inlineNamespace) {
const [left, right] = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: null,
name: null
});
code.overwrite(
this.start,
this.end,
`Promise.resolve().then(${left}${this.inlineNamespace.getName(getPropertyAccess)}${right})`
);
return;
}
if (this.mechanism) {
code.overwrite(
this.start,
findFirstOccurrenceOutsideComment(code.original, '(', this.start + 6) + 1,
this.mechanism.left
);
code.overwrite(this.end - 1, this.end, this.mechanism.right);
}
if (this.resolutionString) {
code.overwrite(this.source.start, this.source.end, this.resolutionString);
if (this.namespaceExportName) {
const [left, right] = getDirectReturnFunction(['n'], {
functionReturn: true,
lineBreakIndent: null,
name: null
});
code.prependLeft(this.end, `.then(${left}n.${this.namespaceExportName}${right})`);
}
} else {
this.source.render(code, options);
}
if (this.assertions !== true) {
if (this.arguments) {
code.overwrite(this.source.end, this.end - 1, '', { contentOnly: true });
}
if (this.assertions) {
code.appendLeft(
this.end - 1,
`,${_}${getObject([['assert', this.assertions]], {
lineBreakIndent: null
})}`
);
}
}
}
setExternalResolution(
exportMode: 'none' | 'named' | 'default' | 'external',
resolution: Module | ExternalModule | string | null,
options: NormalizedOutputOptions,
snippets: GenerateCodeSnippets,
pluginDriver: PluginDriver,
accessedGlobalsByScope: Map<ChildScope, Set<string>>,
resolutionString: string,
namespaceExportName: string | false | undefined,
assertions: string | null | true
): void {
const { format } = options;
this.inlineNamespace = null;
this.resolution = resolution;
this.resolutionString = resolutionString;
this.namespaceExportName = namespaceExportName;
this.assertions = assertions;
const accessedGlobals = [...(accessedImportGlobals[format] || [])];
let helper: string | null;
({ helper, mechanism: this.mechanism } = this.getDynamicImportMechanismAndHelper(
resolution,
exportMode,
options,
snippets,
pluginDriver
));
if (helper) {
accessedGlobals.push(helper);
}
if (accessedGlobals.length > 0) {
this.scope.addAccessedGlobals(accessedGlobals, accessedGlobalsByScope);
}
}
setInternalResolution(inlineNamespace: NamespaceVariable): void {
this.inlineNamespace = inlineNamespace;
}
protected applyDeoptimizations() {}
private getDynamicImportMechanismAndHelper(
resolution: Module | ExternalModule | string | null,
exportMode: 'none' | 'named' | 'default' | 'external',
{
compact,
dynamicImportFunction,
dynamicImportInCjs,
format,
generatedCode: { arrowFunctions },
interop
}: NormalizedOutputOptions,
{ _, getDirectReturnFunction, getDirectReturnIifeLeft }: GenerateCodeSnippets,
pluginDriver: PluginDriver
): { helper: string | null; mechanism: DynamicImportMechanism | null } {
const mechanism = pluginDriver.hookFirstSync('renderDynamicImport', [
{
customResolution: typeof this.resolution === 'string' ? this.resolution : null,
format,
moduleId: this.context.module.id,
targetModuleId:
this.resolution && typeof this.resolution !== 'string' ? this.resolution.id : null
}
]);
if (mechanism) {
return { helper: null, mechanism };
}
const hasDynamicTarget = !this.resolution || typeof this.resolution === 'string';
switch (format) {
case 'cjs': {
if (
dynamicImportInCjs &&
(!resolution || typeof resolution === 'string' || resolution instanceof ExternalModule)
) {
return { helper: null, mechanism: null };
}
const helper = getInteropHelper(resolution, exportMode, interop);
let left = `require(`;
let right = `)`;
if (helper) {
left = `/*#__PURE__*/${helper}(${left}`;
right += ')';
}
const [functionLeft, functionRight] = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: null,
name: null
});
left = `Promise.resolve().then(${functionLeft}${left}`;
right += `${functionRight})`;
if (!arrowFunctions && hasDynamicTarget) {
left = getDirectReturnIifeLeft(['t'], `${left}t${right}`, {
needsArrowReturnParens: false,
needsWrappedFunction: true
});
right = ')';
}
return {
helper,
mechanism: { left, right }
};
}
case 'amd': {
const resolve = compact ? 'c' : 'resolve';
const reject = compact ? 'e' : 'reject';
const helper = getInteropHelper(resolution, exportMode, interop);
const [resolveLeft, resolveRight] = getDirectReturnFunction(['m'], {
functionReturn: false,
lineBreakIndent: null,
name: null
});
const resolveNamespace = helper
? `${resolveLeft}${resolve}(/*#__PURE__*/${helper}(m))${resolveRight}`
: resolve;
const [handlerLeft, handlerRight] = getDirectReturnFunction([resolve, reject], {
functionReturn: false,
lineBreakIndent: null,
name: null
});
let left = `new Promise(${handlerLeft}require([`;
let right = `],${_}${resolveNamespace},${_}${reject})${handlerRight})`;
if (!arrowFunctions && hasDynamicTarget) {
left = getDirectReturnIifeLeft(['t'], `${left}t${right}`, {
needsArrowReturnParens: false,
needsWrappedFunction: true
});
right = ')';
}
return {
helper,
mechanism: { left, right }
};
}
case 'system': {
return {
helper: null,
mechanism: {
left: 'module.import(',
right: ')'
}
};
}
case 'es': {
if (dynamicImportFunction) {
return {
helper: null,
mechanism: {
left: `${dynamicImportFunction}(`,
right: ')'
}
};
}
}
}
return { helper: null, mechanism: null };
}
}
function getInteropHelper(
resolution: Module | ExternalModule | string | null,
exportMode: 'none' | 'named' | 'default' | 'external',
interop: GetInterop
): string | null {
return exportMode === 'external'
? namespaceInteropHelpersByInteropType[
interop(resolution instanceof ExternalModule ? resolution.id : null)
]
: exportMode === 'default'
? INTEROP_NAMESPACE_DEFAULT_ONLY_VARIABLE
: null;
}
const accessedImportGlobals: Record<string, string[]> = {
amd: ['require'],
cjs: ['require'],
system: ['module']
};
function getDeterministicObjectDestructure(objectPattern: ObjectPattern): string[] | undefined {
const variables: string[] = [];
for (const property of objectPattern.properties) {
if (property.type === 'RestElement' || property.computed || property.key.type !== 'Identifier')
return;
variables.push((property.key as Identifier).name);
}
return variables;
}