[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
| 8 | var _path = require("path");
|
---|
| 9 | var _helperModuleTransforms = require("@babel/helper-module-transforms");
|
---|
| 10 | var _core = require("@babel/core");
|
---|
| 11 | const buildPrerequisiteAssignment = (0, _core.template)(`
|
---|
| 12 | GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}
|
---|
| 13 | `);
|
---|
| 14 | const buildWrapper = (0, _core.template)(`
|
---|
| 15 | (function (global, factory) {
|
---|
| 16 | if (typeof define === "function" && define.amd) {
|
---|
| 17 | define(MODULE_NAME, AMD_ARGUMENTS, factory);
|
---|
| 18 | } else if (typeof exports !== "undefined") {
|
---|
| 19 | factory(COMMONJS_ARGUMENTS);
|
---|
| 20 | } else {
|
---|
| 21 | var mod = { exports: {} };
|
---|
| 22 | factory(BROWSER_ARGUMENTS);
|
---|
| 23 |
|
---|
| 24 | GLOBAL_TO_ASSIGN;
|
---|
| 25 | }
|
---|
| 26 | })(
|
---|
| 27 | typeof globalThis !== "undefined" ? globalThis
|
---|
| 28 | : typeof self !== "undefined" ? self
|
---|
| 29 | : this,
|
---|
| 30 | function(IMPORT_NAMES) {
|
---|
| 31 | })
|
---|
| 32 | `);
|
---|
| 33 | var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
|
---|
| 34 | var _api$assumption, _api$assumption2;
|
---|
| 35 | api.assertVersion(7);
|
---|
| 36 | const {
|
---|
| 37 | globals,
|
---|
| 38 | exactGlobals,
|
---|
| 39 | allowTopLevelThis,
|
---|
| 40 | strict,
|
---|
| 41 | strictMode,
|
---|
| 42 | noInterop,
|
---|
| 43 | importInterop
|
---|
| 44 | } = options;
|
---|
| 45 | const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
|
---|
| 46 | const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
|
---|
| 47 | function buildBrowserInit(browserGlobals, exactGlobals, filename, moduleName) {
|
---|
| 48 | const moduleNameOrBasename = moduleName ? moduleName.value : (0, _path.basename)(filename, (0, _path.extname)(filename));
|
---|
| 49 | let globalToAssign = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(moduleNameOrBasename)));
|
---|
| 50 | let initAssignments = [];
|
---|
| 51 | if (exactGlobals) {
|
---|
| 52 | const globalName = browserGlobals[moduleNameOrBasename];
|
---|
| 53 | if (globalName) {
|
---|
| 54 | initAssignments = [];
|
---|
| 55 | const members = globalName.split(".");
|
---|
| 56 | globalToAssign = members.slice(1).reduce((accum, curr) => {
|
---|
| 57 | initAssignments.push(buildPrerequisiteAssignment({
|
---|
| 58 | GLOBAL_REFERENCE: _core.types.cloneNode(accum)
|
---|
| 59 | }));
|
---|
| 60 | return _core.types.memberExpression(accum, _core.types.identifier(curr));
|
---|
| 61 | }, _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(members[0])));
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | initAssignments.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", globalToAssign, _core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")))));
|
---|
| 65 | return initAssignments;
|
---|
| 66 | }
|
---|
| 67 | function buildBrowserArg(browserGlobals, exactGlobals, source) {
|
---|
| 68 | let memberExpression;
|
---|
| 69 | if (exactGlobals) {
|
---|
| 70 | const globalRef = browserGlobals[source];
|
---|
| 71 | if (globalRef) {
|
---|
| 72 | memberExpression = globalRef.split(".").reduce((accum, curr) => _core.types.memberExpression(accum, _core.types.identifier(curr)), _core.types.identifier("global"));
|
---|
| 73 | } else {
|
---|
| 74 | memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(source)));
|
---|
| 75 | }
|
---|
| 76 | } else {
|
---|
| 77 | const requireName = (0, _path.basename)(source, (0, _path.extname)(source));
|
---|
| 78 | const globalName = browserGlobals[requireName] || requireName;
|
---|
| 79 | memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(globalName)));
|
---|
| 80 | }
|
---|
| 81 | return memberExpression;
|
---|
| 82 | }
|
---|
| 83 | return {
|
---|
| 84 | name: "transform-modules-umd",
|
---|
| 85 | visitor: {
|
---|
| 86 | Program: {
|
---|
| 87 | exit(path) {
|
---|
| 88 | if (!(0, _helperModuleTransforms.isModule)(path)) return;
|
---|
| 89 | const browserGlobals = globals || {};
|
---|
| 90 | const moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
|
---|
| 91 | let moduleNameLiteral;
|
---|
| 92 | if (moduleName) moduleNameLiteral = _core.types.stringLiteral(moduleName);
|
---|
| 93 | const {
|
---|
| 94 | meta,
|
---|
| 95 | headers
|
---|
| 96 | } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
|
---|
| 97 | constantReexports,
|
---|
| 98 | enumerableModuleMeta,
|
---|
| 99 | strict,
|
---|
| 100 | strictMode,
|
---|
| 101 | allowTopLevelThis,
|
---|
| 102 | noInterop,
|
---|
| 103 | importInterop,
|
---|
| 104 | filename: this.file.opts.filename
|
---|
| 105 | });
|
---|
| 106 | const amdArgs = [];
|
---|
| 107 | const commonjsArgs = [];
|
---|
| 108 | const browserArgs = [];
|
---|
| 109 | const importNames = [];
|
---|
| 110 | if ((0, _helperModuleTransforms.hasExports)(meta)) {
|
---|
| 111 | amdArgs.push(_core.types.stringLiteral("exports"));
|
---|
| 112 | commonjsArgs.push(_core.types.identifier("exports"));
|
---|
| 113 | browserArgs.push(_core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")));
|
---|
| 114 | importNames.push(_core.types.identifier(meta.exportName));
|
---|
| 115 | }
|
---|
| 116 | for (const [source, metadata] of meta.source) {
|
---|
| 117 | amdArgs.push(_core.types.stringLiteral(source));
|
---|
| 118 | commonjsArgs.push(_core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]));
|
---|
| 119 | browserArgs.push(buildBrowserArg(browserGlobals, exactGlobals, source));
|
---|
| 120 | importNames.push(_core.types.identifier(metadata.name));
|
---|
| 121 | if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
|
---|
| 122 | const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
|
---|
| 123 | if (interop) {
|
---|
| 124 | const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
|
---|
| 125 | header.loc = meta.loc;
|
---|
| 126 | headers.push(header);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
|
---|
| 130 | }
|
---|
| 131 | (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
|
---|
| 132 | path.unshiftContainer("body", headers);
|
---|
| 133 | const {
|
---|
| 134 | body,
|
---|
| 135 | directives
|
---|
| 136 | } = path.node;
|
---|
| 137 | path.node.directives = [];
|
---|
| 138 | path.node.body = [];
|
---|
| 139 | const umdWrapper = path.pushContainer("body", [buildWrapper({
|
---|
| 140 | MODULE_NAME: moduleNameLiteral,
|
---|
| 141 | AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
|
---|
| 142 | COMMONJS_ARGUMENTS: commonjsArgs,
|
---|
| 143 | BROWSER_ARGUMENTS: browserArgs,
|
---|
| 144 | IMPORT_NAMES: importNames,
|
---|
| 145 | GLOBAL_TO_ASSIGN: buildBrowserInit(browserGlobals, exactGlobals, this.filename || "unknown", moduleNameLiteral)
|
---|
| 146 | })])[0];
|
---|
| 147 | const umdFactory = umdWrapper.get("expression.arguments")[1].get("body");
|
---|
| 148 | umdFactory.pushContainer("directives", directives);
|
---|
| 149 | umdFactory.pushContainer("body", body);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | };
|
---|
| 154 | });
|
---|
| 155 |
|
---|
| 156 | //# sourceMappingURL=index.js.map
|
---|