-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ImportExpression.ts
143 lines (132 loc) · 4.04 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
import MagicString from 'magic-string';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { INTEROP_NAMESPACE_VARIABLE } from '../../utils/variableNames';
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 {
source!: ExpressionNode;
type!: NodeType.tImportExpression;
private exportMode: 'none' | 'named' | 'default' | 'auto' = 'auto';
private inlineNamespace?: NamespaceVariable;
hasEffects(): boolean {
return true;
}
include(includeChildrenRecursively: IncludeChildren) {
if (!this.included) {
this.included = true;
this.context.includeDynamicImport(this);
}
this.source.include(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);
}
}
renderFinalResolution(code: MagicString, resolution: string, format: string) {
if (this.included) {
if (format === 'amd' && resolution.startsWith("'.") && resolution.endsWith(".js'")) {
resolution = resolution.slice(0, -4) + "'";
}
code.overwrite(this.source.start, this.source.end, resolution);
}
}
setResolution(
exportMode: 'none' | 'named' | 'default' | 'auto',
inlineNamespace?: NamespaceVariable
): void {
this.exportMode = exportMode;
if (inlineNamespace) {
this.inlineNamespace = inlineNamespace;
} else {
this.scope.addAccessedGlobalsByFormat({
amd: ['require'],
cjs: ['require'],
system: ['module']
});
if (exportMode === 'auto') {
this.scope.addAccessedGlobalsByFormat({
amd: [INTEROP_NAMESPACE_VARIABLE],
cjs: [INTEROP_NAMESPACE_VARIABLE]
});
}
}
}
private getDynamicImportMechanism(options: RenderOptions): DynamicImportMechanism | null {
switch (options.format) {
case 'cjs': {
const _ = options.compact ? '' : ' ';
const resolve = options.compact ? 'c' : 'resolve';
switch (this.exportMode) {
case 'default':
return {
left: `new Promise(function${_}(${resolve})${_}{${_}${resolve}({${_}'default':${_}require(`,
right: `)${_}});${_}})`
};
case 'auto':
return {
left: `new Promise(function${_}(${resolve})${_}{${_}${resolve}(${INTEROP_NAMESPACE_VARIABLE}(require(`,
right: `)));${_}})`
};
default:
return {
left: `new Promise(function${_}(${resolve})${_}{${_}${resolve}(require(`,
right: `));${_}})`
};
}
}
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;
}
}