Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining the moduleIds-related option in the transform plugins #11194

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-core/package.json
Expand Up @@ -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",
Expand Down
45 changes: 2 additions & 43 deletions packages/babel-core/src/transformation/file/file.js
Expand Up @@ -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";
Expand Down Expand Up @@ -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() {
Expand Down
52 changes: 52 additions & 0 deletions packages/babel-helper-module-transforms/src/get-module-name.js
@@ -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;
}
}
2 changes: 2 additions & 0 deletions packages/babel-helper-module-transforms/src/index.js
Expand Up @@ -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 };

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-modules-amd/src/index.js
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down
@@ -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",
"moduleIds": true,
"moduleId": "my custom module name"
"plugins": [
"external-helpers",
["transform-modules-amd", {
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
}
@@ -0,0 +1 @@
foobar();
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"moduleIds": true
}
@@ -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",
"moduleIds": true,
"moduleId": "my custom module name",
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
"plugins": [
"external-helpers",
["transform-modules-amd", {
"loose": true,
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
}
@@ -0,0 +1 @@
foobar();
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"moduleIds": true,
"plugins": ["external-helpers", ["transform-modules-amd", { "loose": true }]]
}
@@ -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
}]
]
}
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-transform-modules-systemjs/src/index.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
@@ -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 @@
{
"moduleIds": true,
"moduleId": "my custom module name"
"plugins": [
"external-helpers",
["transform-modules-systemjs", {
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
}
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-modules-umd/src/index.js
Expand Up @@ -8,6 +8,7 @@ import {
buildNamespaceInitStatements,
ensureStatementsHoisted,
wrapInterop,
getModuleName,
} from "@babel/helper-module-transforms";
import { types as t, template } from "@babel/core";

Expand Down Expand Up @@ -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(
Expand Down
@@ -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",
"moduleIds": true,
"moduleId": "my custom module name",
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
"plugins": [
"external-helpers",
["transform-modules-umd", {
"loose": true,
"moduleIds": true,
"moduleId": "my custom module name"
}]
]
}
@@ -0,0 +1 @@
foobar();
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"moduleIds": true,
"plugins": ["external-helpers", ["transform-modules-umd", { "loose": true }]]
}
@@ -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;