diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@babel/plugin-transform-modules-umd | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/@babel/plugin-transform-modules-umd')
3 files changed, 449 insertions, 0 deletions
diff --git a/node_modules/@babel/plugin-transform-modules-umd/README.md b/node_modules/@babel/plugin-transform-modules-umd/README.md new file mode 100644 index 00000000..a8a919ad --- /dev/null +++ b/node_modules/@babel/plugin-transform-modules-umd/README.md @@ -0,0 +1,214 @@ +# @babel/plugin-transform-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-modules-umd +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["@babel/plugin-transform-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": [ + ["@babel/plugin-transform-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": [ + "@babel/plugin-external-helpers", + ["@babel/plugin-transform-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 @babel/plugin-transform-modules-umd script.js +``` + +### Via Node API + +```javascript +require("@babel/core").transform("code", { + plugins: ["@babel/plugin-transform-modules-umd"] +}); +``` diff --git a/node_modules/@babel/plugin-transform-modules-umd/lib/index.js b/node_modules/@babel/plugin-transform-modules-umd/lib/index.js new file mode 100644 index 00000000..7da15b1f --- /dev/null +++ b/node_modules/@babel/plugin-transform-modules-umd/lib/index.js @@ -0,0 +1,213 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +function _helperPluginUtils() { + const data = require("@babel/helper-plugin-utils"); + + _helperPluginUtils = function _helperPluginUtils() { + return data; + }; + + return data; +} + +function _path() { + const data = require("path"); + + _path = function _path() { + return data; + }; + + return data; +} + +function _helperModuleTransforms() { + const data = require("@babel/helper-module-transforms"); + + _helperModuleTransforms = function _helperModuleTransforms() { + return data; + }; + + return data; +} + +function _core() { + const data = require("@babel/core"); + + _core = function _core() { + return data; + }; + + return data; +} + +const buildPrerequisiteAssignment = (0, _core().template)(` + GLOBAL_REFERENCE = GLOBAL_REFERENCE || {} +`); +const buildWrapper = (0, _core().template)(` + (function (global, factory) { + if (typeof define === "function" && define.amd) { + define(MODULE_NAME, AMD_ARGUMENTS, factory); + } else if (typeof exports !== "undefined") { + factory(COMMONJS_ARGUMENTS); + } else { + var mod = { exports: {} }; + factory(BROWSER_ARGUMENTS); + + GLOBAL_TO_ASSIGN; + } + })(this, function(IMPORT_NAMES) { + }) +`); + +var _default = (0, _helperPluginUtils().declare)((api, options) => { + api.assertVersion(7); + const globals = options.globals, + exactGlobals = options.exactGlobals, + loose = options.loose, + allowTopLevelThis = options.allowTopLevelThis, + strict = options.strict, + strictMode = options.strictMode, + noInterop = options.noInterop; + + function buildBrowserInit(browserGlobals, exactGlobals, filename, moduleName) { + const moduleNameOrBasename = moduleName ? moduleName.value : (0, _path().basename)(filename, (0, _path().extname)(filename)); + + let globalToAssign = _core().types.memberExpression(_core().types.identifier("global"), _core().types.identifier(_core().types.toIdentifier(moduleNameOrBasename))); + + let initAssignments = []; + + if (exactGlobals) { + const globalName = browserGlobals[moduleNameOrBasename]; + + if (globalName) { + initAssignments = []; + const members = globalName.split("."); + globalToAssign = members.slice(1).reduce((accum, curr) => { + initAssignments.push(buildPrerequisiteAssignment({ + GLOBAL_REFERENCE: _core().types.cloneNode(accum) + })); + return _core().types.memberExpression(accum, _core().types.identifier(curr)); + }, _core().types.memberExpression(_core().types.identifier("global"), _core().types.identifier(members[0]))); + } + } + + initAssignments.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", globalToAssign, _core().types.memberExpression(_core().types.identifier("mod"), _core().types.identifier("exports"))))); + return initAssignments; + } + + function buildBrowserArg(browserGlobals, exactGlobals, source) { + let memberExpression; + + if (exactGlobals) { + const globalRef = browserGlobals[source]; + + if (globalRef) { + memberExpression = globalRef.split(".").reduce((accum, curr) => _core().types.memberExpression(accum, _core().types.identifier(curr)), _core().types.identifier("global")); + } else { + memberExpression = _core().types.memberExpression(_core().types.identifier("global"), _core().types.identifier(_core().types.toIdentifier(source))); + } + } else { + const requireName = (0, _path().basename)(source, (0, _path().extname)(source)); + const globalName = browserGlobals[requireName] || requireName; + memberExpression = _core().types.memberExpression(_core().types.identifier("global"), _core().types.identifier(_core().types.toIdentifier(globalName))); + } + + return memberExpression; + } + + return { + visitor: { + Program: { + exit(path) { + if (!(0, _helperModuleTransforms().isModule)(path)) return; + const browserGlobals = globals || {}; + let moduleName = this.getModuleName(); + if (moduleName) moduleName = _core().types.stringLiteral(moduleName); + + const _rewriteModuleStateme = (0, _helperModuleTransforms().rewriteModuleStatementsAndPrepareHeader)(path, { + loose, + strict, + strictMode, + allowTopLevelThis, + noInterop + }), + meta = _rewriteModuleStateme.meta, + headers = _rewriteModuleStateme.headers; + + const amdArgs = []; + const commonjsArgs = []; + const browserArgs = []; + const importNames = []; + + if ((0, _helperModuleTransforms().hasExports)(meta)) { + amdArgs.push(_core().types.stringLiteral("exports")); + commonjsArgs.push(_core().types.identifier("exports")); + browserArgs.push(_core().types.memberExpression(_core().types.identifier("mod"), _core().types.identifier("exports"))); + importNames.push(_core().types.identifier(meta.exportName)); + } + + for (var _iterator = meta.source, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + const _ref2 = _ref, + source = _ref2[0], + metadata = _ref2[1]; + amdArgs.push(_core().types.stringLiteral(source)); + commonjsArgs.push(_core().types.callExpression(_core().types.identifier("require"), [_core().types.stringLiteral(source)])); + browserArgs.push(buildBrowserArg(browserGlobals, exactGlobals, source)); + importNames.push(_core().types.identifier(metadata.name)); + + if (!(0, _helperModuleTransforms().isSideEffectImport)(metadata)) { + const interop = (0, _helperModuleTransforms().wrapInterop)(path, _core().types.identifier(metadata.name), metadata.interop); + + if (interop) { + const header = _core().types.expressionStatement(_core().types.assignmentExpression("=", _core().types.identifier(metadata.name), interop)); + + header.loc = meta.loc; + headers.push(header); + } + } + + headers.push(...(0, _helperModuleTransforms().buildNamespaceInitStatements)(meta, metadata, loose)); + } + + (0, _helperModuleTransforms().ensureStatementsHoisted)(headers); + path.unshiftContainer("body", headers); + const _path$node = path.node, + body = _path$node.body, + directives = _path$node.directives; + path.node.directives = []; + path.node.body = []; + const umdWrapper = path.pushContainer("body", [buildWrapper({ + MODULE_NAME: moduleName, + AMD_ARGUMENTS: _core().types.arrayExpression(amdArgs), + COMMONJS_ARGUMENTS: commonjsArgs, + BROWSER_ARGUMENTS: browserArgs, + IMPORT_NAMES: importNames, + GLOBAL_TO_ASSIGN: buildBrowserInit(browserGlobals, exactGlobals, this.filename || "unknown", moduleName) + })])[0]; + const umdFactory = umdWrapper.get("expression.arguments")[1].get("body"); + umdFactory.pushContainer("directives", directives); + umdFactory.pushContainer("body", body); + } + + } + } + }; +}); + +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-transform-modules-umd/package.json b/node_modules/@babel/plugin-transform-modules-umd/package.json new file mode 100644 index 00000000..3e8a5cdf --- /dev/null +++ b/node_modules/@babel/plugin-transform-modules-umd/package.json @@ -0,0 +1,22 @@ +{ + "name": "@babel/plugin-transform-modules-umd", + "version": "7.0.0-beta.47", + "description": "This plugin transforms ES2015 modules to UMD", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-modules-umd", + "license": "MIT", + "main": "lib/index.js", + "dependencies": { + "@babel/helper-module-transforms": "7.0.0-beta.47", + "@babel/helper-plugin-utils": "7.0.0-beta.47" + }, + "keywords": [ + "babel-plugin" + ], + "peerDependencies": { + "@babel/core": "7.0.0-beta.47" + }, + "devDependencies": { + "@babel/core": "7.0.0-beta.47", + "@babel/helper-plugin-test-runner": "7.0.0-beta.47" + } +} |
