forked from rollup/rollup
/
deconflictChunk.ts
137 lines (131 loc) · 3.9 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
import ExportDefaultVariable from '../ast/variables/ExportDefaultVariable';
import Variable from '../ast/variables/Variable';
import Chunk from '../Chunk';
import ExternalModule from '../ExternalModule';
import Module from '../Module';
import { getSafeName } from './safeName';
const DECONFLICT_IMPORTED_VARIABLES_BY_FORMAT: {
[format: string]: (
usedNames: Set<string>,
imports: Set<Variable>,
dependencies: Set<ExternalModule | Chunk>,
interop: boolean,
preserveModules: boolean
) => void;
} = {
amd: deconflictImportsOther,
cjs: deconflictImportsOther,
es: deconflictImportsEsm,
iife: deconflictImportsOther,
system: deconflictImportsEsm,
umd: deconflictImportsOther
};
export function deconflictChunk(
modules: Module[],
dependencies: Set<ExternalModule | Chunk>,
imports: Set<Variable>,
usedNames: Set<string>,
format: string,
interop: boolean,
preserveModules: boolean
) {
for (const module of modules) {
module.scope.addUsedOutsideNames(usedNames, format);
}
deconflictTopLevelVariables(usedNames, modules);
DECONFLICT_IMPORTED_VARIABLES_BY_FORMAT[format](
usedNames,
imports,
dependencies,
interop,
preserveModules
);
for (const module of modules) {
module.scope.deconflict(format);
}
}
function deconflictImportsEsm(
usedNames: Set<string>,
imports: Set<Variable>,
dependencies: Set<ExternalModule | Chunk>,
interop: boolean
) {
for (const chunkOrExternalModule of dependencies) {
// only deconflict index aliased variables for ESM/System
if (chunkOrExternalModule.variableName === 'index') {
chunkOrExternalModule.variableName = getSafeName(
chunkOrExternalModule.variableName,
usedNames
);
}
}
for (const variable of imports) {
const module = variable.module;
const name = variable.name;
let proposedName: string;
if (module instanceof ExternalModule && (name === '*' || name === 'default')) {
if (name === 'default' && interop && module.exportsNamespace) {
proposedName = module.variableName + '__default';
} else {
proposedName = module.variableName;
}
} else {
proposedName = name;
}
variable.setRenderNames(null, getSafeName(proposedName, usedNames));
}
}
function deconflictImportsOther(
usedNames: Set<string>,
imports: Set<Variable>,
dependencies: Set<ExternalModule | Chunk>,
interop: boolean,
preserveModules: boolean
) {
for (const chunkOrExternalModule of dependencies) {
chunkOrExternalModule.variableName = getSafeName(chunkOrExternalModule.variableName, usedNames);
}
for (const variable of imports) {
const module = variable.module;
if (module instanceof ExternalModule) {
const name = variable.name;
if (name === 'default' && interop && (module.exportsNamespace || module.exportsNames)) {
variable.setRenderNames(null, module.variableName + '__default');
} else if (name === '*' || name === 'default') {
variable.setRenderNames(null, module.variableName);
} else {
variable.setRenderNames(module.variableName, null);
}
} else {
const chunk = module!.chunk!;
if (chunk.exportMode === 'default' || (preserveModules && variable.isNamespace)) {
variable.setRenderNames(null, chunk.variableName);
} else {
variable.setRenderNames(
chunk.variableName,
chunk.getVariableExportName(variable) as string | null
);
}
}
}
}
function deconflictTopLevelVariables(usedNames: Set<string>, modules: Module[]) {
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));
}
}
const namespace = module.getOrCreateNamespace();
if (namespace.included) {
namespace.setRenderNames(null, getSafeName(namespace.name, usedNames));
}
}
}