/
build-external-helpers.ts
208 lines (184 loc) 路 5.06 KB
/
build-external-helpers.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
import * as helpers from "@babel/helpers";
import generator from "@babel/generator";
import template from "@babel/template";
import {
arrayExpression,
assignmentExpression,
binaryExpression,
blockStatement,
callExpression,
cloneNode,
conditionalExpression,
exportNamedDeclaration,
exportSpecifier,
expressionStatement,
functionExpression,
identifier,
memberExpression,
objectExpression,
program,
stringLiteral,
unaryExpression,
variableDeclaration,
variableDeclarator,
} from "@babel/types";
import type * as t from "@babel/types";
import File from "../transformation/file/file";
import type { PublicReplacements } from "@babel/template/src/options";
// Wrapped to avoid wasting time parsing this when almost no-one uses
// build-external-helpers.
const buildUmdWrapper = (replacements: PublicReplacements) =>
template.statement`
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(AMD_ARGUMENTS, factory);
} else if (typeof exports === "object") {
factory(COMMON_ARGUMENTS);
} else {
factory(BROWSER_ARGUMENTS);
}
})(UMD_ROOT, function (FACTORY_PARAMETERS) {
FACTORY_BODY
});
`(replacements);
function buildGlobal(allowlist?: Array<string>) {
const namespace = identifier("babelHelpers");
const body: t.Statement[] = [];
const container = functionExpression(
null,
[identifier("global")],
blockStatement(body),
);
const tree = program([
expressionStatement(
callExpression(container, [
// typeof global === "undefined" ? self : global
conditionalExpression(
binaryExpression(
"===",
unaryExpression("typeof", identifier("global")),
stringLiteral("undefined"),
),
identifier("self"),
identifier("global"),
),
]),
),
]);
body.push(
variableDeclaration("var", [
variableDeclarator(
namespace,
assignmentExpression(
"=",
memberExpression(identifier("global"), namespace),
objectExpression([]),
),
),
]),
);
buildHelpers(body, namespace, allowlist);
return tree;
}
function buildModule(allowlist?: Array<string>) {
const body: t.Statement[] = [];
const refs = buildHelpers(body, null, allowlist);
body.unshift(
exportNamedDeclaration(
null,
Object.keys(refs).map(name => {
return exportSpecifier(cloneNode(refs[name]), identifier(name));
}),
),
);
return program(body, [], "module");
}
function buildUmd(allowlist?: Array<string>) {
const namespace = identifier("babelHelpers");
const body: t.Statement[] = [];
body.push(
variableDeclaration("var", [
variableDeclarator(namespace, identifier("global")),
]),
);
buildHelpers(body, namespace, allowlist);
return program([
buildUmdWrapper({
FACTORY_PARAMETERS: identifier("global"),
BROWSER_ARGUMENTS: assignmentExpression(
"=",
memberExpression(identifier("root"), namespace),
objectExpression([]),
),
COMMON_ARGUMENTS: identifier("exports"),
AMD_ARGUMENTS: arrayExpression([stringLiteral("exports")]),
FACTORY_BODY: body,
UMD_ROOT: identifier("this"),
}),
]);
}
function buildVar(allowlist?: Array<string>) {
const namespace = identifier("babelHelpers");
const body: t.Statement[] = [];
body.push(
variableDeclaration("var", [
variableDeclarator(namespace, objectExpression([])),
]),
);
const tree = program(body);
buildHelpers(body, namespace, allowlist);
body.push(expressionStatement(namespace));
return tree;
}
function buildHelpers(
body: t.Statement[],
namespace: t.Expression,
allowlist?: Array<string>,
): Record<string, t.MemberExpression>;
function buildHelpers(
body: t.Statement[],
namespace: null,
allowlist?: Array<string>,
): Record<string, t.Identifier>;
function buildHelpers(
body: t.Statement[],
namespace: t.Expression | null,
allowlist?: Array<string>,
) {
const getHelperReference = (name: string) => {
return namespace
? memberExpression(namespace, identifier(name))
: identifier(`_${name}`);
};
const refs: { [key: string]: t.Identifier | t.MemberExpression } = {};
helpers.list.forEach(function (name) {
if (allowlist && allowlist.indexOf(name) < 0) return;
const ref = (refs[name] = getHelperReference(name));
helpers.ensure(name, File);
const { nodes } = helpers.get(name, getHelperReference, ref);
body.push(
expressionStatement(
callExpression(functionExpression(null, [], blockStatement(nodes)), []),
),
);
});
return refs;
}
export default function (
allowlist?: Array<string>,
outputType: "global" | "module" | "umd" | "var" = "global",
) {
let tree: t.Program;
const build = {
global: buildGlobal,
module: buildModule,
umd: buildUmd,
var: buildVar,
}[outputType];
if (build) {
tree = build(allowlist);
} else {
throw new Error(`Unsupported output type ${outputType}`);
}
return generator(tree).code;
}