Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,214 @@
# babel-plugin-transform-es2015-modules-umd
> This plugin transforms ES2015 modules to [Universal Module Definition (UMD)](https://github.com/umdjs/umd).
## Example
**In**
```javascript
export default 42;
```
**Out**
```javascript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.actual = mod.exports;
}
})(this, function (exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = 42;
});
```
## Installation
```sh
npm install --save-dev babel-plugin-transform-es2015-modules-umd
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-es2015-modules-umd"]
}
```
You can also override the names of particular libraries when this module is
running in the browser. For example the `es6-promise` library exposes itself
as `global.Promise` rather than `global.es6Promise`. This can be accommodated by:
```json
{
"plugins": [
["transform-es2015-modules-umd", {
"globals": {
"es6-promise": "Promise"
}
}]
]
}
```
#### Default semantics
There are a few things to note about the default semantics.
_First_, this transform uses the
[basename](https://en.wikipedia.org/wiki/Basename) of each import to generate
the global names in the UMD output. This means that if you're importing
multiple modules with the same basename, like:
```js
import fooBar1 from "foo-bar";
import fooBar2 from "./mylib/foo-bar";
```
it will transpile into two references to the same browser global:
```js
factory(global.fooBar, global.fooBar);
```
If you set the plugin options to:
```json
{
"globals": {
"foo-bar": "fooBAR",
"./mylib/foo-bar": "mylib.fooBar"
}
}
```
it will still transpile both to one browser global:
```js
factory(global.fooBAR, global.fooBAR);
```
because again the transform is only using the basename of the import.
_Second_, the specified override will still be passed to the `toIdentifier`
function in [babel-types/src/converters](https://github.com/babel/babel/blob/master/packages/babel-types/src/converters.js).
This means that if you specify an override as a member expression like:
```json
{
"globals": {
"fizzbuzz": "fizz.buzz"
}
}
```
this will _not_ transpile to `factory(global.fizz.buzz)`. Instead, it will
transpile to `factory(global.fizzBuzz)` based on the logic in `toIdentifier`.
_Third_, you cannot override the exported global name.
#### More flexible semantics with `exactGlobals: true`
All of these behaviors can limit the flexibility of the `globals` map. To
remove these limitations, you can set the `exactGlobals` option to `true`.
Doing this instructs the plugin to:
1. always use the full import string instead of the basename when generating
the global names
2. skip passing `globals` overrides to the `toIdentifier` function. Instead,
they are used exactly as written, so you will get errors if you do not use
valid identifiers or valid uncomputed (dot) member expressions.
3. allow the exported global name to be overridden via the `globals` map. Any
override must again be a valid identifier or valid member expression.
Thus, if you set `exactGlobals` to `true` and do not pass any overrides, the
first example of:
```js
import fooBar1 from "foo-bar";
import fooBar2 from "./mylib/foo-bar";
```
will transpile to:
```js
factory(global.fooBar, global.mylibFooBar);
```
And if you set the plugin options to:
```json
{
"globals": {
"foo-bar": "fooBAR",
"./mylib/foo-bar": "mylib.fooBar"
},
"exactGlobals": true
}
```
then it'll transpile to:
```js
factory(global.fooBAR, global.mylib.fooBar)
```
Finally, with the plugin options set to:
```json
{
"plugins": [
"external-helpers",
["transform-es2015-modules-umd", {
"globals": {
"my/custom/module/name": "My.Custom.Module.Name"
},
"exactGlobals": true
}]
],
"moduleId": "my/custom/module/name"
}
```
it will transpile to:
```js
factory(mod.exports);
global.My = global.My || {};
global.My.Custom = global.My.Custom || {};
global.My.Custom.Module = global.My.Custom.Module || {};
global.My.Custom.Module.Name = mod.exports;
```
### Via CLI
```sh
babel --plugins transform-es2015-modules-umd script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-es2015-modules-umd"]
});
```

View File

@ -0,0 +1,127 @@
"use strict";
exports.__esModule = true;
exports.default = function (_ref) {
var t = _ref.types;
function isValidDefine(path) {
if (!path.isExpressionStatement()) return;
var expr = path.get("expression");
if (!expr.isCallExpression()) return false;
if (!expr.get("callee").isIdentifier({ name: "define" })) return false;
var args = expr.get("arguments");
if (args.length === 3 && !args.shift().isStringLiteral()) return false;
if (args.length !== 2) return false;
if (!args.shift().isArrayExpression()) return false;
if (!args.shift().isFunctionExpression()) return false;
return true;
}
return {
inherits: require("babel-plugin-transform-es2015-modules-amd"),
visitor: {
Program: {
exit: function exit(path, state) {
var last = path.get("body").pop();
if (!isValidDefine(last)) return;
var call = last.node.expression;
var args = call.arguments;
var moduleName = args.length === 3 ? args.shift() : null;
var amdArgs = call.arguments[0];
var func = call.arguments[1];
var browserGlobals = state.opts.globals || {};
var commonArgs = amdArgs.elements.map(function (arg) {
if (arg.value === "module" || arg.value === "exports") {
return t.identifier(arg.value);
} else {
return t.callExpression(t.identifier("require"), [arg]);
}
});
var browserArgs = amdArgs.elements.map(function (arg) {
if (arg.value === "module") {
return t.identifier("mod");
} else if (arg.value === "exports") {
return t.memberExpression(t.identifier("mod"), t.identifier("exports"));
} else {
var memberExpression = void 0;
if (state.opts.exactGlobals) {
var globalRef = browserGlobals[arg.value];
if (globalRef) {
memberExpression = globalRef.split(".").reduce(function (accum, curr) {
return t.memberExpression(accum, t.identifier(curr));
}, t.identifier("global"));
} else {
memberExpression = t.memberExpression(t.identifier("global"), t.identifier(t.toIdentifier(arg.value)));
}
} else {
var requireName = (0, _path.basename)(arg.value, (0, _path.extname)(arg.value));
var globalName = browserGlobals[requireName] || requireName;
memberExpression = t.memberExpression(t.identifier("global"), t.identifier(t.toIdentifier(globalName)));
}
return memberExpression;
}
});
var moduleNameOrBasename = moduleName ? moduleName.value : this.file.opts.basename;
var globalToAssign = t.memberExpression(t.identifier("global"), t.identifier(t.toIdentifier(moduleNameOrBasename)));
var prerequisiteAssignments = null;
if (state.opts.exactGlobals) {
var globalName = browserGlobals[moduleNameOrBasename];
if (globalName) {
prerequisiteAssignments = [];
var members = globalName.split(".");
globalToAssign = members.slice(1).reduce(function (accum, curr) {
prerequisiteAssignments.push(buildPrerequisiteAssignment({ GLOBAL_REFERENCE: accum }));
return t.memberExpression(accum, t.identifier(curr));
}, t.memberExpression(t.identifier("global"), t.identifier(members[0])));
}
}
var globalExport = buildGlobalExport({
BROWSER_ARGUMENTS: browserArgs,
PREREQUISITE_ASSIGNMENTS: prerequisiteAssignments,
GLOBAL_TO_ASSIGN: globalToAssign
});
last.replaceWith(buildWrapper({
MODULE_NAME: moduleName,
AMD_ARGUMENTS: amdArgs,
COMMON_ARGUMENTS: commonArgs,
GLOBAL_EXPORT: globalExport,
FUNC: func
}));
}
}
}
};
};
var _path = require("path");
var _babelTemplate = require("babel-template");
var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var buildPrerequisiteAssignment = (0, _babelTemplate2.default)("\n GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}\n");
var buildGlobalExport = (0, _babelTemplate2.default)("\n var mod = { exports: {} };\n factory(BROWSER_ARGUMENTS);\n PREREQUISITE_ASSIGNMENTS\n GLOBAL_TO_ASSIGN = mod.exports;\n");
var buildWrapper = (0, _babelTemplate2.default)("\n (function (global, factory) {\n if (typeof define === \"function\" && define.amd) {\n define(MODULE_NAME, AMD_ARGUMENTS, factory);\n } else if (typeof exports !== \"undefined\") {\n factory(COMMON_ARGUMENTS);\n } else {\n GLOBAL_EXPORT\n }\n })(this, FUNC);\n");
module.exports = exports["default"];

View File

@ -0,0 +1,47 @@
{
"_from": "babel-plugin-transform-es2015-modules-umd@^6.23.0",
"_id": "babel-plugin-transform-es2015-modules-umd@6.24.1",
"_inBundle": false,
"_integrity": "sha1-rJl+YoXNGO1hdq22B9YCNErThGg=",
"_location": "/babel-plugin-transform-es2015-modules-umd",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "babel-plugin-transform-es2015-modules-umd@^6.23.0",
"name": "babel-plugin-transform-es2015-modules-umd",
"escapedName": "babel-plugin-transform-es2015-modules-umd",
"rawSpec": "^6.23.0",
"saveSpec": null,
"fetchSpec": "^6.23.0"
},
"_requiredBy": [
"/babel-preset-env"
],
"_resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-modules-umd/-/babel-plugin-transform-es2015-modules-umd-6.24.1.tgz",
"_shasum": "ac997e6285cd18ed6176adb607d602344ad38468",
"_spec": "babel-plugin-transform-es2015-modules-umd@^6.23.0",
"_where": "/home/manfred/enviPath/ketcher2/ketcher/node_modules/babel-preset-env",
"bundleDependencies": false,
"dependencies": {
"babel-plugin-transform-es2015-modules-amd": "^6.24.1",
"babel-runtime": "^6.22.0",
"babel-template": "^6.24.1"
},
"deprecated": false,
"description": "This plugin transforms ES2015 modules to UMD",
"devDependencies": {
"babel-helper-plugin-test-runner": "^6.24.1"
},
"keywords": [
"babel-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"name": "babel-plugin-transform-es2015-modules-umd",
"repository": {
"type": "git",
"url": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-modules-umd"
},
"version": "6.24.1"
}