-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
186 lines (167 loc) · 5.08 KB
/
index.js
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
const camelcase = require("camelcase");
const MagicString = require("magic-string");
const {walk} = require("estree-walker");
const isReference = require("is-reference");
const {attachScopes} = require("@rollup/pluginutils");
function analyzeImport(node, importBindings, code) {
code.remove(node.start, node.end);
for (const spec of node.specifiers) {
importBindings.set(spec.local.name, [
spec.imported ? spec.imported.name : "default",
node.source.value
]);
}
}
function analyzeExportDefault(node, exportBindings, code) {
if (node.declaration.type === "Identifier") {
exportBindings.set("default", node.declaration.name);
code.remove(node.start, node.end);
} else if (
node.declaration.id && (
node.declaration.type === "ClassDeclaration" ||
node.declaration.type === "FunctionDeclaration"
)
) {
exportBindings.set("default", node.declaration.id.name);
code.remove(node.start, node.declaration.start);
} else {
exportBindings.set("default", "_iife_default");
code.overwrite(node.start, node.declaration.start, "var _iife_default = ", {
contentOnly: true
});
}
}
function analyzeExportNamed(node, exportBindings, code) {
if (!node.declaration) {
for (const spec of node.specifiers) {
exportBindings.set(
spec.exported.name, node.source ?
[spec.local.name, node.source.value] : spec.local.name
);
}
code.remove(node.start, node.end);
} else {
if (node.declaration.type === "VariableDeclaration") {
for (const dec of node.declaration.declarations) {
exportBindings.set(dec.id.name, dec.id.name);
}
} else {
exportBindings.set(node.declaration.id.name, node.declaration.id.name);
}
code.remove(node.start, node.declaration.start);
}
}
function transform({
code,
parse,
ast = parse(code),
sourcemap = false,
strict = false,
resolveGlobal = () => {},
name
}) {
code = new MagicString(code);
resolveGlobal = createResolveGlobal(resolveGlobal);
const importBindings = new Map; // name -> [property, source]
const exportBindings = new Map;
let scope = attachScopes(ast, "scope");
for (const node of ast.body) {
if (node.type === "ImportDeclaration") {
analyzeImport(node, importBindings, code);
} else if (node.type === "ExportDefaultDeclaration") {
analyzeExportDefault(node, exportBindings, code);
} else if (node.type === "ExportNamedDeclaration") {
analyzeExportNamed(node, exportBindings, code);
}
}
const globals = new Set;
for (const [, source] of importBindings.values()) {
globals.add(resolveGlobal(source));
}
walk(ast, {
enter(node, parent) {
if (/^(import|export)/i.test(node.type)) {
this.skip();
}
if (node.scope) {
scope = node.scope;
}
if (isReference(node, parent)) {
if (importBindings.has(node.name) && !scope.contains(node.name)) {
overwriteVar(node, parent, getBindingName(importBindings.get(node.name)));
} else if (globals.has(node.name) && scope.contains(node.name)) {
overwriteVar(node, parent, `_local_${node.name}`);
}
}
},
leave(node) {
if (node.scope) {
scope = node.scope.parent;
}
}
});
code.appendLeft(
ast.body[0].start,
`${getPrefix()}(function () {\n${strict ? "'use strict';\n" : ""}`
);
code.appendRight(
ast.body[ast.body.length - 1].end,
`\n${getReturn()}})();`
);
return {
code: code.toString(),
map: sourcemap ? code.generateMap({hires: true}) : null
};
function getReturn() {
if (!exportBindings.size) {
return "";
}
if (exportBindings.size === 1 && exportBindings.has("default")) {
return `return ${exportBindings.get("default")};\n`;
}
return `return {\n${
[...exportBindings.entries()]
.map(([left, right]) => ` ${left}: ${getName(right)}`)
.join(",\n")
}\n};\n`;
function getName(name) {
if (Array.isArray(name)) {
return getBindingName(name);
}
return name;
}
}
function getPrefix() {
return exportBindings.size ? `var ${name} = ` : "";
}
function getBindingName([prop, source]) {
if (prop === "default") {
return resolveGlobal(source);
}
return `${resolveGlobal(source)}.${prop}`;
}
function overwriteVar(node, parent, name) {
if (node.name === name || node.isOverwritten) {
return;
}
if (parent.type === "Property" && parent.key.start === parent.value.start) {
code.appendLeft(node.end, `: ${name}`);
parent.key.isOverwritten = true;
parent.value.isOverwritten = true;
} else {
code.overwrite(node.start, node.end, name, {contentOnly: true});
// with object shorthand, the node would be accessed twice (.key and .value)
node.isOverwritten = true;
}
}
function createResolveGlobal(resolveGlobal) {
const cache = new Map;
return name => {
if (!cache.has(name)) {
cache.set(name, resolveGlobal(name) || camelcase(name));
}
return cache.get(name);
};
}
}
module.exports = {transform};