/
Import.ts
101 lines (89 loc) · 2.9 KB
/
Import.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
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import CallExpression from './CallExpression';
import * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';
interface DynamicImportMechanism {
left: string;
right: string;
interopLeft?: string;
interopRight?: string;
}
const getDynamicImportMechanism = (format: string, compact: boolean): DynamicImportMechanism => {
switch (format) {
case 'cjs': {
const _ = compact ? '' : ' ';
return {
left: 'Promise.resolve(require(',
right: '))',
interopLeft: `Promise.resolve({${_}default:${_}require(`,
interopRight: `)${_}})`
};
}
case 'amd': {
const _ = compact ? '' : ' ';
const resolve = compact ? 'c' : 'resolve';
const reject = compact ? 'e' : 'reject';
return {
left: `new Promise(function${_}(${resolve},${_}${reject})${_}{${_}require([`,
right: `],${_}${resolve},${_}${reject})${_}})`,
interopLeft: `new Promise(function${_}(${resolve},${_}${reject})${_}{${_}require([`,
interopRight: `],${_}function${_}(m)${_}{${_}${resolve}({${_}default:${_}m${_}})${_}},${_}${reject})${_}})`
};
}
case 'system':
return {
left: 'module.import(',
right: ')'
};
}
};
export default class Import extends NodeBase {
type: NodeType.tImport;
parent: CallExpression;
private resolutionNamespace: string;
private resolutionInterop: boolean;
private replaced: boolean;
include() {
this.included = true;
this.context.includeDynamicImport(this);
}
initialise() {
this.included = false;
this.replaced = false;
this.resolutionNamespace = undefined;
this.resolutionInterop = false;
this.context.addDynamicImport(this);
}
renderFinalResolution(code: MagicString, resolution: string) {
if (this.included && !this.replaced) {
code.overwrite(this.parent.arguments[0].start, this.parent.arguments[0].end, resolution);
}
}
render(code: MagicString, options: RenderOptions) {
if (this.resolutionNamespace) {
const _ = options.compact ? '' : ' ';
const s = options.compact ? '' : ';';
code.overwrite(
this.parent.start,
this.parent.end,
`Promise.resolve().then(function${_}()${_}{${_}return ${this.resolutionNamespace}${s}${_}})`
);
this.replaced = true;
return;
}
const importMechanism = getDynamicImportMechanism(options.format, options.compact);
if (importMechanism) {
const leftMechanism =
(this.resolutionInterop && importMechanism.interopLeft) || importMechanism.left;
code.overwrite(this.parent.start, this.parent.arguments[0].start, leftMechanism);
const rightMechanism =
(this.resolutionInterop && importMechanism.interopRight) || importMechanism.right;
code.overwrite(this.parent.arguments[0].end, this.parent.end, rightMechanism);
}
}
setResolution(interop: boolean, namespace: string = undefined): void {
this.resolutionInterop = interop;
this.resolutionNamespace = namespace;
}
}