/
ImportExpression.ts
172 lines (160 loc) · 4.97 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
import MagicString from 'magic-string';
import ExternalModule from '../../ExternalModule';
import Module from '../../Module';
import { NormalizedOutputOptions } from '../../rollup/types';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { INTEROP_NAMESPACE_VARIABLE } from '../../utils/variableNames';
import { InclusionContext } from '../ExecutionContext';
import NamespaceVariable from '../variables/NamespaceVariable';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
interface DynamicImportMechanism {
left: string;
right: string;
}
export default class Import extends NodeBase {
inlineNamespace: NamespaceVariable | null = null;
source!: ExpressionNode;
type!: NodeType.tImportExpression;
private exportMode: 'none' | 'named' | 'default' | 'auto' = 'auto';
private resolution: Module | ExternalModule | string | null = null;
hasEffects(): boolean {
return true;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
if (!this.included) {
this.included = true;
this.context.includeDynamicImport(this);
this.scope.addAccessedDynamicImport(this);
}
this.source.include(context, includeChildrenRecursively);
}
initialise() {
this.context.addDynamicImport(this);
}
render(code: MagicString, options: RenderOptions) {
if (this.inlineNamespace) {
const _ = options.compact ? '' : ' ';
const s = options.compact ? '' : ';';
code.overwrite(
this.start,
this.end,
`Promise.resolve().then(function${_}()${_}{${_}return ${this.inlineNamespace.getName()}${s}${_}})`
);
return;
}
const importMechanism = this.getDynamicImportMechanism(options);
if (importMechanism) {
code.overwrite(
this.start,
findFirstOccurrenceOutsideComment(code.original, '(', this.start + 6) + 1,
importMechanism.left
);
code.overwrite(this.end - 1, this.end, importMechanism.right);
}
this.source.render(code, options);
}
renderFinalResolution(
code: MagicString,
resolution: string,
namespaceExportName: string | false | undefined,
options: NormalizedOutputOptions
) {
code.overwrite(this.source.start, this.source.end, resolution);
if (namespaceExportName) {
const _ = options.compact ? '' : ' ';
const s = options.compact ? '' : ';';
code.prependLeft(
this.end,
`.then(function${_}(n)${_}{${_}return n.${namespaceExportName}${s}${_}})`
);
}
}
setExternalResolution(
exportMode: 'none' | 'named' | 'default' | 'auto',
resolution: Module | ExternalModule | string | null
): void {
this.exportMode = exportMode;
this.resolution = resolution;
this.scope.addAccessedGlobalsByFormat({
amd: ['require'],
cjs: ['require'],
system: ['module']
});
if (exportMode === 'auto') {
this.scope.addAccessedGlobalsByFormat({
amd: [INTEROP_NAMESPACE_VARIABLE],
cjs: [INTEROP_NAMESPACE_VARIABLE]
});
}
}
setInternalResolution(inlineNamespace: NamespaceVariable) {
this.inlineNamespace = inlineNamespace;
}
private getDynamicImportMechanism(options: RenderOptions): DynamicImportMechanism | null {
const mechanism = options.outputPluginDriver.hookFirstSync('renderDynamicImport', [
{
customResolution: typeof this.resolution === 'string' ? this.resolution : null,
format: options.format,
moduleId: this.context.module.id,
targetModuleId:
this.resolution && typeof this.resolution !== 'string' ? this.resolution.id : null
}
]);
if (mechanism) {
return mechanism;
}
switch (options.format) {
case 'cjs': {
const _ = options.compact ? '' : ' ';
const s = options.compact ? '' : ';';
const leftStart = `Promise.resolve().then(function${_}()${_}{${_}return`;
switch (this.exportMode) {
case 'default':
return {
left: `${leftStart}${_}{${_}'default':${_}require(`,
right: `)${_}}${s}${_}})`
};
case 'auto':
return {
left: `${leftStart} ${INTEROP_NAMESPACE_VARIABLE}(require(`,
right: `))${s}${_}})`
};
default:
return {
left: `${leftStart} require(`,
right: `)${s}${_}})`
};
}
}
case 'amd': {
const _ = options.compact ? '' : ' ';
const resolve = options.compact ? 'c' : 'resolve';
const reject = options.compact ? 'e' : 'reject';
const resolveNamespace =
this.exportMode === 'default'
? `function${_}(m)${_}{${_}${resolve}({${_}'default':${_}m${_}});${_}}`
: this.exportMode === 'auto'
? `function${_}(m)${_}{${_}${resolve}(${INTEROP_NAMESPACE_VARIABLE}(m));${_}}`
: resolve;
return {
left: `new Promise(function${_}(${resolve},${_}${reject})${_}{${_}require([`,
right: `],${_}${resolveNamespace},${_}${reject})${_}})`
};
}
case 'system':
return {
left: 'module.import(',
right: ')'
};
case 'es':
if (options.dynamicImportFunction) {
return {
left: `${options.dynamicImportFunction}(`,
right: ')'
};
}
}
return null;
}
}