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:
Nicolò Ribaudo 2020-03-16 23:58:04 +01:00 committed by GitHub
parent 3ce7c2e394
commit a875560c31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 342 additions and 71 deletions

View File

@ -41,6 +41,7 @@
"dependencies": { "dependencies": {
"@babel/code-frame": "^7.8.3", "@babel/code-frame": "^7.8.3",
"@babel/generator": "^7.8.7", "@babel/generator": "^7.8.7",
"@babel/helper-module-transforms": "^7.8.6",
"@babel/helpers": "^7.8.4", "@babel/helpers": "^7.8.4",
"@babel/parser": "^7.8.7", "@babel/parser": "^7.8.7",
"@babel/template": "^7.8.6", "@babel/template": "^7.8.6",

View File

@ -5,6 +5,7 @@ import { NodePath, Scope, type HubInterface } from "@babel/traverse";
import { codeFrameColumns } from "@babel/code-frame"; import { codeFrameColumns } from "@babel/code-frame";
import traverse from "@babel/traverse"; import traverse from "@babel/traverse";
import * as t from "@babel/types"; import * as t from "@babel/types";
import { getModuleName } from "@babel/helper-module-transforms";
import semver from "semver"; import semver from "semver";
import type { NormalizedFile } from "../normalize-file"; import type { NormalizedFile } from "../normalize-file";
@ -106,49 +107,7 @@ export default class File {
} }
getModuleName(): ?string { getModuleName(): ?string {
const { return getModuleName(this.opts, this.opts);
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;
}
} }
addImport() { addImport() {

View File

@ -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;
}
}

View File

@ -12,6 +12,8 @@ import normalizeAndLoadModuleMetadata, {
isSideEffectImport, isSideEffectImport,
} from "./normalize-and-load-metadata"; } from "./normalize-and-load-metadata";
export { default as getModuleName } from "./get-module-name";
export { hasExports, isSideEffectImport, isModule, rewriteThis }; export { hasExports, isSideEffectImport, isModule, rewriteThis };
/** /**

View File

@ -7,6 +7,7 @@ import {
buildNamespaceInitStatements, buildNamespaceInitStatements,
ensureStatementsHoisted, ensureStatementsHoisted,
wrapInterop, wrapInterop,
getModuleName,
} from "@babel/helper-module-transforms"; } from "@babel/helper-module-transforms";
import { template, types as t } from "@babel/core"; import { template, types as t } from "@babel/core";
import { getImportSource } from "babel-plugin-dynamic-import-node/utils"; import { getImportSource } from "babel-plugin-dynamic-import-node/utils";
@ -96,7 +97,7 @@ export default declare((api, options) => {
importNames.push(requireId); importNames.push(requireId);
} }
let moduleName = this.getModuleName(); let moduleName = getModuleName(this.file.opts, options);
if (moduleName) moduleName = t.stringLiteral(moduleName); if (moduleName) moduleName = t.stringLiteral(moduleName);
const { meta, headers } = rewriteModuleStatementsAndPrepareHeader( const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(

View File

@ -0,0 +1,5 @@
{
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name"
}

View File

@ -0,0 +1,3 @@
define("my custom module name", [], function () {
"use strict";
});

View File

@ -1,5 +1,10 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "plugins": [
"moduleId": "my custom module name" "external-helpers",
["transform-modules-amd", {
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
} }

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"moduleIds": true
}

View File

@ -0,0 +1,5 @@
define("amd/module-name-compat/input", [], function () {
"use strict";
foobar();
});

View File

@ -1,4 +1,7 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true "plugins": [
"external-helpers",
["transform-modules-amd", { "moduleIds": true }]
]
} }

View File

@ -0,0 +1,6 @@
{
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name",
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
}

View File

@ -0,0 +1,3 @@
define("my custom module name", [], function () {
"use strict";
});

View File

@ -1,6 +1,11 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "plugins": [
"moduleId": "my custom module name", "external-helpers",
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]] ["transform-modules-amd", {
"loose": true,
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
} }

View File

@ -0,0 +1,5 @@
{
"sourceType": "module",
"moduleIds": true,
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
}

View File

@ -0,0 +1,5 @@
define("loose/module-name-compat/input", [], function () {
"use strict";
foobar();
});

View File

@ -1,5 +1,10 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "plugins": [
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]] "external-helpers",
["transform-modules-amd", {
"loose": true,
"moduleIds": true
}]
]
} }

View File

@ -6,6 +6,7 @@ import {
buildNamespaceInitStatements, buildNamespaceInitStatements,
ensureStatementsHoisted, ensureStatementsHoisted,
wrapInterop, wrapInterop,
getModuleName,
} from "@babel/helper-module-transforms"; } from "@babel/helper-module-transforms";
import simplifyAccess from "@babel/helper-simple-access"; import simplifyAccess from "@babel/helper-simple-access";
import { template, types as t } from "@babel/core"; 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); if (moduleName) moduleName = t.stringLiteral(moduleName);
const { meta, headers } = rewriteModuleStatementsAndPrepareHeader( const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(

View File

@ -2,7 +2,7 @@ import { declare } from "@babel/helper-plugin-utils";
import hoistVariables from "@babel/helper-hoist-variables"; import hoistVariables from "@babel/helper-hoist-variables";
import { template, types as t } from "@babel/core"; import { template, types as t } from "@babel/core";
import { getImportSource } from "babel-plugin-dynamic-import-node/utils"; 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(` const buildTemplate = template(`
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) { 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); if (moduleName) moduleName = t.stringLiteral(moduleName);
hoistVariables( hoistVariables(

View File

@ -0,0 +1,4 @@
{
"moduleIds": true,
"moduleId": "my custom module name"
}

View File

@ -0,0 +1,8 @@
System.register("my custom module name", [], function (_export, _context) {
"use strict";
return {
setters: [],
execute: function () {}
};
});

View File

@ -1,4 +1,9 @@
{ {
"moduleIds": true, "plugins": [
"moduleId": "my custom module name" "external-helpers",
["transform-modules-systemjs", {
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
} }

View File

@ -8,6 +8,7 @@ import {
buildNamespaceInitStatements, buildNamespaceInitStatements,
ensureStatementsHoisted, ensureStatementsHoisted,
wrapInterop, wrapInterop,
getModuleName,
} from "@babel/helper-module-transforms"; } from "@babel/helper-module-transforms";
import { types as t, template } from "@babel/core"; import { types as t, template } from "@babel/core";
@ -140,7 +141,7 @@ export default declare((api, options) => {
const browserGlobals = globals || {}; const browserGlobals = globals || {};
let moduleName = this.getModuleName(); let moduleName = getModuleName(this.file.opts, options);
if (moduleName) moduleName = t.stringLiteral(moduleName); if (moduleName) moduleName = t.stringLiteral(moduleName);
const { meta, headers } = rewriteModuleStatementsAndPrepareHeader( const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(

View File

@ -0,0 +1,6 @@
{
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name",
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
}

View File

@ -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";
});

View File

@ -1,6 +1,11 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "plugins": [
"moduleId": "my custom module name", "external-helpers",
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]] ["transform-modules-umd", {
"loose": true,
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
} }

View File

@ -0,0 +1,5 @@
{
"sourceType": "module",
"moduleIds": true,
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
}

View 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();
});

View File

@ -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
}

View File

@ -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;
});

View File

@ -8,9 +8,9 @@
"umd/module-name-with-overridden-global/expected": "baz" "umd/module-name-with-overridden-global/expected": "baz"
}, },
"exactGlobals": true, "exactGlobals": true,
"moduleIds": true,
"loose": true "loose": true
} }
] ]
], ]
"moduleIds": true
} }

View File

@ -1,5 +1,10 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "plugins": [
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]] "external-helpers",
["transform-modules-umd", {
"loose": true,
"moduleIds": true
}]
]
} }

View File

@ -0,0 +1,5 @@
{
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name"
}

View File

@ -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";
});

View File

@ -1,5 +1,10 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "plugins": [
"moduleId": "my custom module name" "external-helpers",
["transform-modules-umd", {
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
} }

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"moduleIds": true
}

View 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();
});

View File

@ -0,0 +1,15 @@
{
"plugins": [
"external-helpers",
[
"transform-modules-umd",
{
"globals": {
"umd/module-name-with-overridden-global/expected": "baz"
},
"exactGlobals": true
}
]
],
"moduleIds": true
}

View File

@ -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;
});

View File

@ -7,9 +7,9 @@
"globals": { "globals": {
"umd/module-name-with-overridden-global/expected": "baz" "umd/module-name-with-overridden-global/expected": "baz"
}, },
"exactGlobals": true "exactGlobals": true,
"moduleIds": true
} }
] ]
], ]
"moduleIds": true
} }

View File

@ -1,4 +1,9 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleIds": true "plugins": [
"external-helpers",
["transform-modules-umd", {
"moduleIds": true
}]
]
} }