forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deconflictChunk.ts
243 lines (236 loc) · 7.82 KB
/
deconflictChunk.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
import type Chunk from '../Chunk';
import ExternalChunk from '../ExternalChunk';
import ExternalModule from '../ExternalModule';
import type Module from '../Module';
import type ChildScope from '../ast/scopes/ChildScope';
import ExportDefaultVariable from '../ast/variables/ExportDefaultVariable';
import type SyntheticNamedExportVariable from '../ast/variables/SyntheticNamedExportVariable';
import type Variable from '../ast/variables/Variable';
import type { GetInterop, InternalModuleFormat } from '../rollup/types';
import { makeLegal } from './identifierHelpers';
import {
canDefaultBeTakenFromNamespace,
defaultInteropHelpersByInteropType,
isDefaultAProperty,
namespaceInteropHelpersByInteropType
} from './interopHelpers';
import { getSafeName } from './safeName';
export interface DependenciesToBeDeconflicted {
deconflictedDefault: ReadonlySet<ExternalChunk>;
deconflictedNamespace: ReadonlySet<Chunk | ExternalChunk>;
dependencies: ReadonlySet<Chunk | ExternalChunk>;
}
const DECONFLICT_IMPORTED_VARIABLES_BY_FORMAT: {
[format in InternalModuleFormat]: (
usedNames: Set<string>,
imports: ReadonlySet<Variable>,
dependenciesToBeDeconflicted: DependenciesToBeDeconflicted,
interop: GetInterop,
preserveModules: boolean,
externalLiveBindings: boolean,
chunkByModule: ReadonlyMap<Module, Chunk>,
externalChunkByModule: ReadonlyMap<ExternalModule, ExternalChunk>,
syntheticExports: ReadonlySet<SyntheticNamedExportVariable>
) => void;
} = {
amd: deconflictImportsOther,
cjs: deconflictImportsOther,
es: deconflictImportsEsmOrSystem,
iife: deconflictImportsOther,
system: deconflictImportsEsmOrSystem,
umd: deconflictImportsOther
};
export function deconflictChunk(
modules: readonly Module[],
dependenciesToBeDeconflicted: DependenciesToBeDeconflicted,
imports: ReadonlySet<Variable>,
usedNames: Set<string>,
format: InternalModuleFormat,
interop: GetInterop,
preserveModules: boolean,
externalLiveBindings: boolean,
chunkByModule: ReadonlyMap<Module, Chunk>,
externalChunkByModule: ReadonlyMap<ExternalModule, ExternalChunk>,
syntheticExports: ReadonlySet<SyntheticNamedExportVariable>,
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>,
accessedGlobalsByScope: ReadonlyMap<ChildScope, ReadonlySet<string>>,
includedNamespaces: ReadonlySet<Module>
): void {
const reversedModules = [...modules].reverse();
for (const module of reversedModules) {
module.scope.addUsedOutsideNames(
usedNames,
format,
exportNamesByVariable,
accessedGlobalsByScope
);
}
deconflictTopLevelVariables(usedNames, reversedModules, includedNamespaces);
DECONFLICT_IMPORTED_VARIABLES_BY_FORMAT[format](
usedNames,
imports,
dependenciesToBeDeconflicted,
interop,
preserveModules,
externalLiveBindings,
chunkByModule,
externalChunkByModule,
syntheticExports
);
for (const module of reversedModules) {
module.scope.deconflict(format, exportNamesByVariable, accessedGlobalsByScope);
}
}
function deconflictImportsEsmOrSystem(
usedNames: Set<string>,
imports: ReadonlySet<Variable>,
dependenciesToBeDeconflicted: DependenciesToBeDeconflicted,
_interop: GetInterop,
preserveModules: boolean,
_externalLiveBindings: boolean,
chunkByModule: ReadonlyMap<Module, Chunk>,
externalChunkByModule: ReadonlyMap<ExternalModule, ExternalChunk>,
syntheticExports: ReadonlySet<SyntheticNamedExportVariable>
) {
// This is needed for namespace reexports
for (const dependency of dependenciesToBeDeconflicted.dependencies) {
if (preserveModules || dependency instanceof ExternalChunk) {
dependency.variableName = getSafeName(dependency.suggestedVariableName, usedNames, null);
}
}
for (const variable of imports) {
const module = variable.module!;
const name = variable.name;
if (variable.isNamespace && (preserveModules || module instanceof ExternalModule)) {
variable.setRenderNames(
null,
(module instanceof ExternalModule
? externalChunkByModule.get(module)!
: chunkByModule.get(module)!
).variableName
);
} else if (module instanceof ExternalModule && name === 'default') {
variable.setRenderNames(
null,
getSafeName(
[...module.exportedVariables].some(
([exportedVariable, exportedName]) => exportedName === '*' && exportedVariable.included
)
? module.suggestedVariableName + '__default'
: module.suggestedVariableName,
usedNames,
variable.forbiddenNames
)
);
} else {
variable.setRenderNames(
null,
getSafeName(makeLegal(name), usedNames, variable.forbiddenNames)
);
}
}
for (const variable of syntheticExports) {
variable.setRenderNames(null, getSafeName(variable.name, usedNames, variable.forbiddenNames));
}
}
function deconflictImportsOther(
usedNames: Set<string>,
imports: ReadonlySet<Variable>,
{ deconflictedDefault, deconflictedNamespace, dependencies }: DependenciesToBeDeconflicted,
interop: GetInterop,
preserveModules: boolean,
externalLiveBindings: boolean,
chunkByModule: ReadonlyMap<Module, Chunk>,
externalChunkByModule: ReadonlyMap<ExternalModule, ExternalChunk>
): void {
for (const chunk of dependencies) {
chunk.variableName = getSafeName(chunk.suggestedVariableName, usedNames, null);
}
for (const chunk of deconflictedNamespace) {
chunk.namespaceVariableName = getSafeName(
`${chunk.suggestedVariableName}__namespace`,
usedNames,
null
);
}
for (const externalModule of deconflictedDefault) {
externalModule.defaultVariableName =
deconflictedNamespace.has(externalModule) &&
canDefaultBeTakenFromNamespace(interop(externalModule.id), externalLiveBindings)
? externalModule.namespaceVariableName
: getSafeName(`${externalModule.suggestedVariableName}__default`, usedNames, null);
}
for (const variable of imports) {
const module = variable.module;
if (module instanceof ExternalModule) {
const chunk = externalChunkByModule.get(module)!;
const name = variable.name;
if (name === 'default') {
const moduleInterop = interop(module.id);
const variableName = defaultInteropHelpersByInteropType[moduleInterop]
? chunk.defaultVariableName
: chunk.variableName;
if (isDefaultAProperty(moduleInterop, externalLiveBindings)) {
variable.setRenderNames(variableName, 'default');
} else {
variable.setRenderNames(null, variableName);
}
} else if (name === '*') {
variable.setRenderNames(
null,
namespaceInteropHelpersByInteropType[interop(module.id)]
? chunk.namespaceVariableName
: chunk.variableName
);
} else {
// if the second parameter is `null`, it uses its "name" for the property name
variable.setRenderNames(chunk.variableName, null);
}
} else {
const chunk = chunkByModule.get(module!)!;
if (preserveModules && variable.isNamespace) {
variable.setRenderNames(
null,
chunk.exportMode === 'default' ? chunk.namespaceVariableName : chunk.variableName
);
} else if (chunk.exportMode === 'default') {
variable.setRenderNames(null, chunk.variableName);
} else {
variable.setRenderNames(
chunk.variableName,
chunk.getVariableExportName(variable) as string | null
);
}
}
}
}
function deconflictTopLevelVariables(
usedNames: Set<string>,
modules: readonly Module[],
includedNamespaces: ReadonlySet<Module>
): void {
for (const module of modules) {
for (const variable of module.scope.variables.values()) {
if (
variable.included &&
// this will only happen for exports in some formats
!(
variable.renderBaseName ||
(variable instanceof ExportDefaultVariable && variable.getOriginalVariable() !== variable)
)
) {
variable.setRenderNames(
null,
getSafeName(variable.name, usedNames, variable.forbiddenNames)
);
}
}
if (includedNamespaces.has(module)) {
const namespace = module.namespace;
namespace.setRenderNames(
null,
getSafeName(namespace.name, usedNames, namespace.forbiddenNames)
);
}
}
}