Allow defining the moduleIds-related option in the transform p… (#11194)
* Update "moduleIds" tests * Allow defining the moduleIds related options in the transform plugins - moduleIds - moduleId - getModuleId - moduleRoot * Sort deps
This commit is contained in:
parent
3ce7c2e394
commit
a875560c31
@ -41,6 +41,7 @@
|
||||
"dependencies": {
|
||||
"@babel/code-frame": "^7.8.3",
|
||||
"@babel/generator": "^7.8.7",
|
||||
"@babel/helper-module-transforms": "^7.8.6",
|
||||
"@babel/helpers": "^7.8.4",
|
||||
"@babel/parser": "^7.8.7",
|
||||
"@babel/template": "^7.8.6",
|
||||
|
||||
@ -5,6 +5,7 @@ import { NodePath, Scope, type HubInterface } from "@babel/traverse";
|
||||
import { codeFrameColumns } from "@babel/code-frame";
|
||||
import traverse from "@babel/traverse";
|
||||
import * as t from "@babel/types";
|
||||
import { getModuleName } from "@babel/helper-module-transforms";
|
||||
import semver from "semver";
|
||||
|
||||
import type { NormalizedFile } from "../normalize-file";
|
||||
@ -106,49 +107,7 @@ export default class File {
|
||||
}
|
||||
|
||||
getModuleName(): ?string {
|
||||
const {
|
||||
filename,
|
||||
filenameRelative = filename,
|
||||
|
||||
moduleId,
|
||||
moduleIds = !!moduleId,
|
||||
|
||||
getModuleId,
|
||||
|
||||
sourceRoot: sourceRootTmp,
|
||||
moduleRoot = sourceRootTmp,
|
||||
sourceRoot = moduleRoot,
|
||||
} = this.opts;
|
||||
|
||||
if (!moduleIds) return null;
|
||||
|
||||
// moduleId is n/a if a `getModuleId()` is provided
|
||||
if (moduleId != null && !getModuleId) {
|
||||
return moduleId;
|
||||
}
|
||||
|
||||
let moduleName = moduleRoot != null ? moduleRoot + "/" : "";
|
||||
|
||||
if (filenameRelative) {
|
||||
const sourceRootReplacer =
|
||||
sourceRoot != null ? new RegExp("^" + sourceRoot + "/?") : "";
|
||||
|
||||
moduleName += filenameRelative
|
||||
// remove sourceRoot from filename
|
||||
.replace(sourceRootReplacer, "")
|
||||
// remove extension
|
||||
.replace(/\.(\w*?)$/, "");
|
||||
}
|
||||
|
||||
// normalize path separators
|
||||
moduleName = moduleName.replace(/\\/g, "/");
|
||||
|
||||
if (getModuleId) {
|
||||
// If return is falsy, assume they want us to use our generated default name
|
||||
return getModuleId(moduleName) || moduleName;
|
||||
} else {
|
||||
return moduleName;
|
||||
}
|
||||
return getModuleName(this.opts, this.opts);
|
||||
}
|
||||
|
||||
addImport() {
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
// @flow
|
||||
|
||||
export default function getModuleName(
|
||||
rootOpts: Object,
|
||||
pluginOpts: Object,
|
||||
): ?string {
|
||||
const {
|
||||
filename,
|
||||
filenameRelative = filename,
|
||||
|
||||
sourceRoot = pluginOpts.moduleRoot ?? rootOpts.moduleRoot,
|
||||
} = rootOpts;
|
||||
|
||||
const {
|
||||
moduleId = rootOpts.moduleId,
|
||||
moduleIds = rootOpts.moduleIds ?? !!moduleId,
|
||||
|
||||
getModuleId = rootOpts.getModuleId,
|
||||
|
||||
moduleRoot = rootOpts.moduleRoot ?? sourceRoot,
|
||||
} = pluginOpts;
|
||||
|
||||
if (!moduleIds) return null;
|
||||
|
||||
// moduleId is n/a if a `getModuleId()` is provided
|
||||
if (moduleId != null && !getModuleId) {
|
||||
return moduleId;
|
||||
}
|
||||
|
||||
let moduleName = moduleRoot != null ? moduleRoot + "/" : "";
|
||||
|
||||
if (filenameRelative) {
|
||||
const sourceRootReplacer =
|
||||
sourceRoot != null ? new RegExp("^" + sourceRoot + "/?") : "";
|
||||
|
||||
moduleName += filenameRelative
|
||||
// remove sourceRoot from filename
|
||||
.replace(sourceRootReplacer, "")
|
||||
// remove extension
|
||||
.replace(/\.(\w*?)$/, "");
|
||||
}
|
||||
|
||||
// normalize path separators
|
||||
moduleName = moduleName.replace(/\\/g, "/");
|
||||
|
||||
if (getModuleId) {
|
||||
// If return is falsy, assume they want us to use our generated default name
|
||||
return getModuleId(moduleName) || moduleName;
|
||||
} else {
|
||||
return moduleName;
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,8 @@ import normalizeAndLoadModuleMetadata, {
|
||||
isSideEffectImport,
|
||||
} from "./normalize-and-load-metadata";
|
||||
|
||||
export { default as getModuleName } from "./get-module-name";
|
||||
|
||||
export { hasExports, isSideEffectImport, isModule, rewriteThis };
|
||||
|
||||
/**
|
||||
|
||||
@ -7,6 +7,7 @@ import {
|
||||
buildNamespaceInitStatements,
|
||||
ensureStatementsHoisted,
|
||||
wrapInterop,
|
||||
getModuleName,
|
||||
} from "@babel/helper-module-transforms";
|
||||
import { template, types as t } from "@babel/core";
|
||||
import { getImportSource } from "babel-plugin-dynamic-import-node/utils";
|
||||
@ -96,7 +97,7 @@ export default declare((api, options) => {
|
||||
importNames.push(requireId);
|
||||
}
|
||||
|
||||
let moduleName = this.getModuleName();
|
||||
let moduleName = getModuleName(this.file.opts, options);
|
||||
if (moduleName) moduleName = t.stringLiteral(moduleName);
|
||||
|
||||
const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name"
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
define("my custom module name", [], function () {
|
||||
"use strict";
|
||||
});
|
||||
@ -1,5 +1,10 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-amd", {
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name"
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
1
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/input.js
vendored
Normal file
1
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
foobar();
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true
|
||||
}
|
||||
5
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/output.js
vendored
Normal file
5
packages/babel-plugin-transform-modules-amd/test/fixtures/amd/module-name-compat/output.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
define("amd/module-name-compat/input", [], function () {
|
||||
"use strict";
|
||||
|
||||
foobar();
|
||||
});
|
||||
@ -1,4 +1,7 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-amd", { "moduleIds": true }]
|
||||
]
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name",
|
||||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
define("my custom module name", [], function () {
|
||||
"use strict";
|
||||
});
|
||||
@ -1,6 +1,11 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-amd", {
|
||||
"loose": true,
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name",
|
||||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
|
||||
"moduleId": "my custom module name"
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
1
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/input.js
vendored
Normal file
1
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
foobar();
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
|
||||
}
|
||||
5
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/output.js
vendored
Normal file
5
packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/output.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
define("loose/module-name-compat/input", [], function () {
|
||||
"use strict";
|
||||
|
||||
foobar();
|
||||
});
|
||||
@ -1,5 +1,10 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-amd", {
|
||||
"loose": true,
|
||||
"moduleIds": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import {
|
||||
buildNamespaceInitStatements,
|
||||
ensureStatementsHoisted,
|
||||
wrapInterop,
|
||||
getModuleName,
|
||||
} from "@babel/helper-module-transforms";
|
||||
import simplifyAccess from "@babel/helper-simple-access";
|
||||
import { template, types as t } from "@babel/core";
|
||||
@ -161,7 +162,7 @@ export default declare((api, options) => {
|
||||
});
|
||||
}
|
||||
|
||||
let moduleName = this.getModuleName();
|
||||
let moduleName = getModuleName(this.file.opts, options);
|
||||
if (moduleName) moduleName = t.stringLiteral(moduleName);
|
||||
|
||||
const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(
|
||||
|
||||
@ -2,7 +2,7 @@ import { declare } from "@babel/helper-plugin-utils";
|
||||
import hoistVariables from "@babel/helper-hoist-variables";
|
||||
import { template, types as t } from "@babel/core";
|
||||
import { getImportSource } from "babel-plugin-dynamic-import-node/utils";
|
||||
import { rewriteThis } from "@babel/helper-module-transforms";
|
||||
import { rewriteThis, getModuleName } from "@babel/helper-module-transforms";
|
||||
|
||||
const buildTemplate = template(`
|
||||
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
|
||||
@ -501,7 +501,7 @@ export default declare((api, options) => {
|
||||
);
|
||||
});
|
||||
|
||||
let moduleName = this.getModuleName();
|
||||
let moduleName = getModuleName(this.file.opts, options);
|
||||
if (moduleName) moduleName = t.stringLiteral(moduleName);
|
||||
|
||||
hoistVariables(
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name"
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
System.register("my custom module name", [], function (_export, _context) {
|
||||
"use strict";
|
||||
|
||||
return {
|
||||
setters: [],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
@ -1,4 +1,9 @@
|
||||
{
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-systemjs", {
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name"
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import {
|
||||
buildNamespaceInitStatements,
|
||||
ensureStatementsHoisted,
|
||||
wrapInterop,
|
||||
getModuleName,
|
||||
} from "@babel/helper-module-transforms";
|
||||
import { types as t, template } from "@babel/core";
|
||||
|
||||
@ -140,7 +141,7 @@ export default declare((api, options) => {
|
||||
|
||||
const browserGlobals = globals || {};
|
||||
|
||||
let moduleName = this.getModuleName();
|
||||
let moduleName = getModuleName(this.file.opts, options);
|
||||
if (moduleName) moduleName = t.stringLiteral(moduleName);
|
||||
|
||||
const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name",
|
||||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("my custom module name", [], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory();
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory();
|
||||
global.myCustomModuleName = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
});
|
||||
@ -1,6 +1,11 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-umd", {
|
||||
"loose": true,
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name",
|
||||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
|
||||
"moduleId": "my custom module name"
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
1
packages/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/input.js
vendored
Normal file
1
packages/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
foobar();
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
|
||||
}
|
||||
17
packages/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/output.js
vendored
Normal file
17
packages/babel-plugin-transform-modules-umd/test/fixtures/loose/module-name-compat/output.js
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("loose/module-name-compat/input", [], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory();
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory();
|
||||
global.looseModuleNameCompatInput = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
|
||||
foobar();
|
||||
});
|
||||
@ -0,0 +1 @@
|
||||
export default 42;
|
||||
@ -0,0 +1,16 @@
|
||||
{
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
[
|
||||
"transform-modules-umd",
|
||||
{
|
||||
"globals": {
|
||||
"umd/module-name-with-overridden-global/expected": "baz"
|
||||
},
|
||||
"exactGlobals": true,
|
||||
"loose": true
|
||||
}
|
||||
]
|
||||
],
|
||||
"moduleIds": true
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("loose/module-name-with-overridden-global-compat/input", ["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(mod.exports);
|
||||
global.looseModuleNameWithOverriddenGlobalCompatInput = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) {
|
||||
"use strict";
|
||||
|
||||
_exports.__esModule = true;
|
||||
_exports.default = void 0;
|
||||
var _default = 42;
|
||||
_exports.default = _default;
|
||||
});
|
||||
@ -8,9 +8,9 @@
|
||||
"umd/module-name-with-overridden-global/expected": "baz"
|
||||
},
|
||||
"exactGlobals": true,
|
||||
"moduleIds": true,
|
||||
"loose": true
|
||||
}
|
||||
]
|
||||
],
|
||||
"moduleIds": true
|
||||
]
|
||||
}
|
||||
|
||||
@ -1,5 +1,10 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-umd", {
|
||||
"loose": true,
|
||||
"moduleIds": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name"
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("my custom module name", [], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory();
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory();
|
||||
global.myCustomModuleName = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
});
|
||||
@ -1,5 +1,10 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-umd", {
|
||||
"moduleIds": true,
|
||||
"moduleId": "my custom module name"
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
1
packages/babel-plugin-transform-modules-umd/test/fixtures/umd/module-name-compat/input.js
vendored
Normal file
1
packages/babel-plugin-transform-modules-umd/test/fixtures/umd/module-name-compat/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
foobar();
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"moduleIds": true
|
||||
}
|
||||
17
packages/babel-plugin-transform-modules-umd/test/fixtures/umd/module-name-compat/output.js
vendored
Normal file
17
packages/babel-plugin-transform-modules-umd/test/fixtures/umd/module-name-compat/output.js
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("umd/module-name-compat/input", [], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory();
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory();
|
||||
global.umdModuleNameCompatInput = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () {
|
||||
"use strict";
|
||||
|
||||
foobar();
|
||||
});
|
||||
@ -0,0 +1 @@
|
||||
export default 42;
|
||||
@ -0,0 +1,15 @@
|
||||
{
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
[
|
||||
"transform-modules-umd",
|
||||
{
|
||||
"globals": {
|
||||
"umd/module-name-with-overridden-global/expected": "baz"
|
||||
},
|
||||
"exactGlobals": true
|
||||
}
|
||||
]
|
||||
],
|
||||
"moduleIds": true
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("umd/module-name-with-overridden-global-compat/input", ["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(mod.exports);
|
||||
global.umdModuleNameWithOverriddenGlobalCompatInput = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) {
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(_exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
_exports.default = void 0;
|
||||
var _default = 42;
|
||||
_exports.default = _default;
|
||||
});
|
||||
@ -7,9 +7,9 @@
|
||||
"globals": {
|
||||
"umd/module-name-with-overridden-global/expected": "baz"
|
||||
},
|
||||
"exactGlobals": true
|
||||
"exactGlobals": true,
|
||||
"moduleIds": true
|
||||
}
|
||||
]
|
||||
],
|
||||
"moduleIds": true
|
||||
]
|
||||
}
|
||||
|
||||
@ -1,4 +1,9 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
["transform-modules-umd", {
|
||||
"moduleIds": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user