[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = _default;
|
---|
| 7 |
|
---|
| 8 | function helpers() {
|
---|
| 9 | const data = require("@babel/helpers");
|
---|
| 10 |
|
---|
| 11 | helpers = function () {
|
---|
| 12 | return data;
|
---|
| 13 | };
|
---|
| 14 |
|
---|
| 15 | return data;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | function _generator() {
|
---|
| 19 | const data = require("@babel/generator");
|
---|
| 20 |
|
---|
| 21 | _generator = function () {
|
---|
| 22 | return data;
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | return data;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | function _template() {
|
---|
| 29 | const data = require("@babel/template");
|
---|
| 30 |
|
---|
| 31 | _template = function () {
|
---|
| 32 | return data;
|
---|
| 33 | };
|
---|
| 34 |
|
---|
| 35 | return data;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | function t() {
|
---|
| 39 | const data = require("@babel/types");
|
---|
| 40 |
|
---|
| 41 | t = function () {
|
---|
| 42 | return data;
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | return data;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | var _file = require("../transformation/file/file");
|
---|
| 49 |
|
---|
| 50 | const buildUmdWrapper = replacements => (0, _template().default)`
|
---|
| 51 | (function (root, factory) {
|
---|
| 52 | if (typeof define === "function" && define.amd) {
|
---|
| 53 | define(AMD_ARGUMENTS, factory);
|
---|
| 54 | } else if (typeof exports === "object") {
|
---|
| 55 | factory(COMMON_ARGUMENTS);
|
---|
| 56 | } else {
|
---|
| 57 | factory(BROWSER_ARGUMENTS);
|
---|
| 58 | }
|
---|
| 59 | })(UMD_ROOT, function (FACTORY_PARAMETERS) {
|
---|
| 60 | FACTORY_BODY
|
---|
| 61 | });
|
---|
| 62 | `(replacements);
|
---|
| 63 |
|
---|
| 64 | function buildGlobal(allowlist) {
|
---|
| 65 | const namespace = t().identifier("babelHelpers");
|
---|
| 66 | const body = [];
|
---|
| 67 | const container = t().functionExpression(null, [t().identifier("global")], t().blockStatement(body));
|
---|
| 68 | const tree = t().program([t().expressionStatement(t().callExpression(container, [t().conditionalExpression(t().binaryExpression("===", t().unaryExpression("typeof", t().identifier("global")), t().stringLiteral("undefined")), t().identifier("self"), t().identifier("global"))]))]);
|
---|
| 69 | body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().assignmentExpression("=", t().memberExpression(t().identifier("global"), namespace), t().objectExpression([])))]));
|
---|
| 70 | buildHelpers(body, namespace, allowlist);
|
---|
| 71 | return tree;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | function buildModule(allowlist) {
|
---|
| 75 | const body = [];
|
---|
| 76 | const refs = buildHelpers(body, null, allowlist);
|
---|
| 77 | body.unshift(t().exportNamedDeclaration(null, Object.keys(refs).map(name => {
|
---|
| 78 | return t().exportSpecifier(t().cloneNode(refs[name]), t().identifier(name));
|
---|
| 79 | })));
|
---|
| 80 | return t().program(body, [], "module");
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | function buildUmd(allowlist) {
|
---|
| 84 | const namespace = t().identifier("babelHelpers");
|
---|
| 85 | const body = [];
|
---|
| 86 | body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().identifier("global"))]));
|
---|
| 87 | buildHelpers(body, namespace, allowlist);
|
---|
| 88 | return t().program([buildUmdWrapper({
|
---|
| 89 | FACTORY_PARAMETERS: t().identifier("global"),
|
---|
| 90 | BROWSER_ARGUMENTS: t().assignmentExpression("=", t().memberExpression(t().identifier("root"), namespace), t().objectExpression([])),
|
---|
| 91 | COMMON_ARGUMENTS: t().identifier("exports"),
|
---|
| 92 | AMD_ARGUMENTS: t().arrayExpression([t().stringLiteral("exports")]),
|
---|
| 93 | FACTORY_BODY: body,
|
---|
| 94 | UMD_ROOT: t().identifier("this")
|
---|
| 95 | })]);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | function buildVar(allowlist) {
|
---|
| 99 | const namespace = t().identifier("babelHelpers");
|
---|
| 100 | const body = [];
|
---|
| 101 | body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().objectExpression([]))]));
|
---|
| 102 | const tree = t().program(body);
|
---|
| 103 | buildHelpers(body, namespace, allowlist);
|
---|
| 104 | body.push(t().expressionStatement(namespace));
|
---|
| 105 | return tree;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | function buildHelpers(body, namespace, allowlist) {
|
---|
| 109 | const getHelperReference = name => {
|
---|
| 110 | return namespace ? t().memberExpression(namespace, t().identifier(name)) : t().identifier(`_${name}`);
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | const refs = {};
|
---|
| 114 | helpers().list.forEach(function (name) {
|
---|
| 115 | if (allowlist && allowlist.indexOf(name) < 0) return;
|
---|
| 116 | const ref = refs[name] = getHelperReference(name);
|
---|
| 117 | helpers().ensure(name, _file.default);
|
---|
| 118 | const {
|
---|
| 119 | nodes
|
---|
| 120 | } = helpers().get(name, getHelperReference, ref);
|
---|
| 121 | body.push(...nodes);
|
---|
| 122 | });
|
---|
| 123 | return refs;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | function _default(allowlist, outputType = "global") {
|
---|
| 127 | let tree;
|
---|
| 128 | const build = {
|
---|
| 129 | global: buildGlobal,
|
---|
| 130 | module: buildModule,
|
---|
| 131 | umd: buildUmd,
|
---|
| 132 | var: buildVar
|
---|
| 133 | }[outputType];
|
---|
| 134 |
|
---|
| 135 | if (build) {
|
---|
| 136 | tree = build(allowlist);
|
---|
| 137 | } else {
|
---|
| 138 | throw new Error(`Unsupported output type ${outputType}`);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return (0, _generator().default)(tree).code;
|
---|
| 142 | } |
---|