Skip to content

Commit

Permalink
Allow defining the moduleIds-related option in the transform p… (#11194)
Browse files Browse the repository at this point in the history
* Update "moduleIds" tests

* Allow defining the moduleIds related options in the transform plugins

- moduleIds
- moduleId
- getModuleId
- moduleRoot

* Sort deps
  • Loading branch information
nicolo-ribaudo committed Mar 16, 2020
1 parent 3ce7c2e commit a875560
Show file tree
Hide file tree
Showing 52 changed files with 342 additions and 71 deletions.
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;

0 comments on commit a875560

Please sign in to comment.