Debian Codegraph @

packages/babel-core/src/tools/build-external-helpers.ts

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import * as helpers from "@babel/helpers";
2 import generator from "@babel/generator";
3 import template from "@babel/template";
4 import {
5 arrayExpression,
6 assignmentExpression,
7 binaryExpression,
8 blockStatement,
9 callExpression,
10 cloneNode,
11 conditionalExpression,
12 exportNamedDeclaration,
13 exportSpecifier,
14 expressionStatement,
15 functionExpression,
16 identifier,
17 memberExpression,
18 objectExpression,
19 program,
20 stringLiteral,
21 unaryExpression,
22 variableDeclaration,
23 variableDeclarator,
24 } from "@babel/types";
25 import type * as t from "@babel/types";
26 import File from "../transformation/file/file";
27 import type { PublicReplacements } from "@babel/template/src/options";
28
29 // Wrapped to avoid wasting time parsing this when almost no-one uses
30 // build-external-helpers.
31 const buildUmdWrapper = (replacements: PublicReplacements) =>
32 template.statement`
33 (function (root, factory) {
34 if (typeof define === "function" && define.amd) {
35 define(AMD_ARGUMENTS, factory);
36 } else if (typeof exports === "object") {
37 factory(COMMON_ARGUMENTS);
38 } else {
39 factory(BROWSER_ARGUMENTS);
40 }
41 })(UMD_ROOT, function (FACTORY_PARAMETERS) {
42 FACTORY_BODY
43 });
44 `(replacements);
45
46 function buildGlobal(allowlist?: Array<string>) {
47 const namespace = identifier("babelHelpers");
48
49 const body: t.Statement[] = [];
50 const container = functionExpression(
51 null,
52 [identifier("global")],
53 blockStatement(body),
54 );
55 const tree = program([
56 expressionStatement(
57 callExpression(container, [
58 // typeof global === "undefined" ? self : global
59 conditionalExpression(
60 binaryExpression(
61 "===",
62 unaryExpression("typeof", identifier("global")),
63 stringLiteral("undefined"),
64 ),
65 identifier("self"),
66 identifier("global"),
67 ),
68 ]),
69 ),
70 ]);
71
72 body.push(
73 variableDeclaration("var", [
74 variableDeclarator(
75 namespace,
76 assignmentExpression(
77 "=",
78 memberExpression(identifier("global"), namespace),
79 objectExpression([]),
80 ),
81 ),
82 ]),
83 );
84
85 buildHelpers(body, namespace, allowlist);
86
87 return tree;
88 }
89
90 function buildModule(allowlist?: Array<string>) {
91 const body: t.Statement[] = [];
92 const refs = buildHelpers(body, null, allowlist);
93
94 body.unshift(
95 exportNamedDeclaration(
96 null,
97 Object.keys(refs).map(name => {
98 return exportSpecifier(cloneNode(refs[name]), identifier(name));
99 }),
100 ),
101 );
102
103 return program(body, [], "module");
104 }
105
106 function buildUmd(allowlist?: Array<string>) {
107 const namespace = identifier("babelHelpers");
108
109 const body: t.Statement[] = [];
110 body.push(
111 variableDeclaration("var", [
112 variableDeclarator(namespace, identifier("global")),
113 ]),
114 );
115
116 buildHelpers(body, namespace, allowlist);
117
118 return program([
119 buildUmdWrapper({
120 FACTORY_PARAMETERS: identifier("global"),
121 BROWSER_ARGUMENTS: assignmentExpression(
122 "=",
123 memberExpression(identifier("root"), namespace),
124 objectExpression([]),
125 ),
126 COMMON_ARGUMENTS: identifier("exports"),
127 AMD_ARGUMENTS: arrayExpression([stringLiteral("exports")]),
128 FACTORY_BODY: body,
129 UMD_ROOT: identifier("this"),
130 }),
131 ]);
132 }
133
134 function buildVar(allowlist?: Array<string>) {
135 const namespace = identifier("babelHelpers");
136
137 const body: t.Statement[] = [];
138 body.push(
139 variableDeclaration("var", [
140 variableDeclarator(namespace, objectExpression([])),
141 ]),
142 );
143 const tree = program(body);
144 buildHelpers(body, namespace, allowlist);
145 body.push(expressionStatement(namespace));
146 return tree;
147 }
148
149 function buildHelpers(
150 body: t.Statement[],
151 namespace: t.Expression,
152 allowlist?: Array<string>,
153 ): Record<string, t.MemberExpression>;
154 function buildHelpers(
155 body: t.Statement[],
156 namespace: null,
157 allowlist?: Array<string>,
158 ): Record<string, t.Identifier>;
159
160 function buildHelpers(
161 body: t.Statement[],
162 namespace: t.Expression | null,
163 allowlist?: Array<string>,
164 ) {
165 const getHelperReference = (name: string) => {
166 return namespace
167 ? memberExpression(namespace, identifier(name))
168 : identifier(`_${name}`);
169 };
170
171 const refs: { [key: string]: t.Identifier | t.MemberExpression } = {};
172 helpers.list.forEach(function (name) {
173 if (allowlist && allowlist.indexOf(name) < 0) return;
174
175 const ref = (refs[name] = getHelperReference(name));
176
177 helpers.ensure(name, File);
178 const { nodes } = helpers.get(name, getHelperReference, ref);
179
180 body.push(...nodes);
181 });
182 return refs;
183 }
184 export default function (
185 allowlist?: Array<string>,
186 outputType: "global" | "module" | "umd" | "var" = "global",
187 ) {
188 let tree: t.Program;
189
190 const build = {
191 global: buildGlobal,
192 module: buildModule,
193 umd: buildUmd,
194 var: buildVar,
195 }[outputType];
196
197 if (build) {
198 tree = build(allowlist);
199 } else {
200 throw new Error(`Unsupported output type ${outputType}`);
201 }
202
203 return generator(tree).code;
204 }