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