forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.ts
152 lines (146 loc) · 4.99 KB
/
es.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
import type { Bundle as MagicStringBundle } from 'magic-string';
import type { ChunkDependency, ChunkExports, ImportSpecifier, ReexportSpecifier } from '../Chunk';
import type { ImportAttributesKey, NormalizedOutputOptions } from '../rollup/types';
import type { GenerateCodeSnippets } from '../utils/generateCodeSnippets';
import { stringifyIdentifierIfNeeded } from '../utils/identifierHelpers';
import { getHelpersBlock } from '../utils/interopHelpers';
import type { FinaliserOptions } from './index';
export default function es(
magicString: MagicStringBundle,
{ accessedGlobals, indent: t, intro, outro, dependencies, exports, snippets }: FinaliserOptions,
{
externalLiveBindings,
freeze,
generatedCode: { symbols },
importAttributesKey
}: NormalizedOutputOptions
): void {
const { n } = snippets;
const importBlock = getImportBlock(dependencies, importAttributesKey, snippets);
if (importBlock.length > 0) intro += importBlock.join(n) + n + n;
intro += getHelpersBlock(
null,
accessedGlobals,
t,
snippets,
externalLiveBindings,
freeze,
symbols
);
if (intro) magicString.prepend(intro);
const exportBlock = getExportBlock(exports, snippets);
if (exportBlock.length > 0) magicString.append(n + n + exportBlock.join(n).trim());
if (outro) magicString.append(outro);
magicString.trim();
}
function getImportBlock(
dependencies: readonly ChunkDependency[],
importAttributesKey: ImportAttributesKey,
{ _ }: GenerateCodeSnippets
): string[] {
const importBlock: string[] = [];
for (const { importPath, reexports, imports, name, attributes } of dependencies) {
const assertion = attributes ? `${_}${importAttributesKey}${_}${attributes}` : '';
const pathWithAssertion = `'${importPath}'${assertion};`;
if (!reexports && !imports) {
importBlock.push(`import${_}${pathWithAssertion}`);
continue;
}
if (imports) {
let defaultImport: ImportSpecifier | null = null;
let starImport: ImportSpecifier | null = null;
const importedNames: ImportSpecifier[] = [];
for (const specifier of imports) {
if (specifier.imported === 'default') {
defaultImport = specifier;
} else if (specifier.imported === '*') {
starImport = specifier;
} else {
importedNames.push(specifier);
}
}
if (starImport) {
importBlock.push(`import${_}*${_}as ${starImport.local} from${_}${pathWithAssertion}`);
}
if (defaultImport && importedNames.length === 0) {
importBlock.push(`import ${defaultImport.local} from${_}${pathWithAssertion}`);
} else if (importedNames.length > 0) {
importBlock.push(
`import ${defaultImport ? `${defaultImport.local},${_}` : ''}{${_}${importedNames
.map(specifier =>
specifier.imported === specifier.local
? specifier.imported
: `${stringifyIdentifierIfNeeded(specifier.imported)} as ${specifier.local}`
)
.join(`,${_}`)}${_}}${_}from${_}${pathWithAssertion}`
);
}
}
if (reexports) {
let starExport: ReexportSpecifier | null = null;
const namespaceReexports: ReexportSpecifier[] = [];
const namedReexports: ReexportSpecifier[] = [];
for (const specifier of reexports) {
if (specifier.reexported === '*') {
starExport = specifier;
} else if (specifier.imported === '*') {
namespaceReexports.push(specifier);
} else {
namedReexports.push(specifier);
}
}
if (starExport) {
importBlock.push(`export${_}*${_}from${_}${pathWithAssertion}`);
}
if (namespaceReexports.length > 0) {
if (
!imports ||
!imports.some(specifier => specifier.imported === '*' && specifier.local === name)
) {
importBlock.push(`import${_}*${_}as ${name} from${_}${pathWithAssertion}`);
}
for (const specifier of namespaceReexports) {
importBlock.push(
`export${_}{${_}${
name === specifier.reexported
? name
: `${name} as ${stringifyIdentifierIfNeeded(specifier.reexported)}`
} };`
);
}
}
if (namedReexports.length > 0) {
importBlock.push(
`export${_}{${_}${namedReexports
.map(specifier =>
specifier.imported === specifier.reexported
? stringifyIdentifierIfNeeded(specifier.imported)
: `${stringifyIdentifierIfNeeded(
specifier.imported
)} as ${stringifyIdentifierIfNeeded(specifier.reexported)}`
)
.join(`,${_}`)}${_}}${_}from${_}${pathWithAssertion}`
);
}
}
}
return importBlock;
}
function getExportBlock(exports: ChunkExports, { _, cnst }: GenerateCodeSnippets): string[] {
const exportBlock: string[] = [];
const exportDeclaration: string[] = [];
for (const specifier of exports) {
if (specifier.expression) {
exportBlock.push(`${cnst} ${specifier.local}${_}=${_}${specifier.expression};`);
}
exportDeclaration.push(
specifier.exported === specifier.local
? specifier.local
: `${specifier.local} as ${stringifyIdentifierIfNeeded(specifier.exported)}`
);
}
if (exportDeclaration.length > 0) {
exportBlock.push(`export${_}{${_}${exportDeclaration.join(`,${_}`)}${_}};`);
}
return exportBlock;
}