Skip to content

Commit

Permalink
[babel 8] Remove module-specific options from @babel/core (#12724)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 28, 2021
1 parent 6b39baf commit 8e8954b
Show file tree
Hide file tree
Showing 26 changed files with 137 additions and 86 deletions.
36 changes: 22 additions & 14 deletions packages/babel-cli/src/babel/options.js
Expand Up @@ -98,17 +98,19 @@ commander.option(
"The root from which all sources are relative.",
);

// Config params for certain module output formats.
commander.option(
"--module-root [filename]",
// eslint-disable-next-line max-len
"Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.",
);
commander.option("-M, --module-ids", "Insert an explicit id for modules.");
commander.option(
"--module-id [string]",
"Specify a custom name for module ids.",
);
if (!process.env.BABEL_8_BREAKING) {
// Config params for certain module output formats.
commander.option(
"--module-root [filename]",
// eslint-disable-next-line max-len
"Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.",
);
commander.option("-M, --module-ids", "Insert an explicit id for modules.");
commander.option(
"--module-id [string]",
"Specify a custom name for module ids.",
);
}

// "babel" command specific arguments that are not passed to @babel/core.
commander.option(
Expand Down Expand Up @@ -277,9 +279,6 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
sourceMaps: opts.sourceMaps,
sourceFileName: opts.sourceFileName,
sourceRoot: opts.sourceRoot,
moduleRoot: opts.moduleRoot,
moduleIds: opts.moduleIds,
moduleId: opts.moduleId,

// Commander will default the "--no-" arguments to true, but we want to
// leave them undefined so that @babel/core can handle the
Expand All @@ -289,6 +288,15 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
comments: opts.comments === true ? undefined : opts.comments,
};

if (!process.env.BABEL_8_BREAKING) {
// $FlowIgnore
Object.assign(babelOptions, {
moduleRoot: opts.moduleRoot,
moduleIds: opts.moduleIds,
moduleId: opts.moduleId,
});
}

// If the @babel/cli version is newer than the @babel/core version, and we have added
// new options for @babel/core, we'll potentially get option validation errors from
// @babel/core. To avoid that, we delete undefined options, so @babel/core will only
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Expand Up @@ -50,7 +50,7 @@
"@babel/code-frame": "workspace:^7.12.13",
"@babel/generator": "workspace:^7.13.9",
"@babel/helper-compilation-targets": "workspace:^7.13.13",
"@babel/helper-module-transforms": "workspace:^7.13.12",
"@babel/helper-module-transforms": "condition:BABEL_8_BREAKING ? : workspace:^7.13.12",
"@babel/helpers": "workspace:^7.13.10",
"@babel/parser": "workspace:^7.13.13",
"@babel/template": "workspace:^7.12.13",
Expand Down
27 changes: 9 additions & 18 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -173,25 +173,22 @@ const COMMON_VALIDATORS: ValidatorSet = {
sourceRoot: (assertString: Validator<
$PropertyType<ValidatedOptions, "sourceRoot">,
>),
getModuleId: (assertFunction: Validator<
$PropertyType<ValidatedOptions, "getModuleId">,
>),
moduleRoot: (assertString: Validator<
$PropertyType<ValidatedOptions, "moduleRoot">,
>),
moduleIds: (assertBoolean: Validator<
$PropertyType<ValidatedOptions, "moduleIds">,
>),
moduleId: (assertString: Validator<
$PropertyType<ValidatedOptions, "moduleId">,
>),
parserOpts: (assertObject: Validator<
$PropertyType<ValidatedOptions, "parserOpts">,
>),
generatorOpts: (assertObject: Validator<
$PropertyType<ValidatedOptions, "generatorOpts">,
>),
};
if (!process.env.BABEL_8_BREAKING) {
Object.assign(COMMON_VALIDATORS, {
getModuleId: assertFunction,
moduleRoot: assertString,
moduleIds: assertBoolean,
moduleId: assertString,
});
}

export type InputOptions = ValidatedOptions;

export type ValidatedOptions = {
Expand Down Expand Up @@ -253,12 +250,6 @@ export type ValidatedOptions = {
sourceFileName?: string,
sourceRoot?: string,

// AMD/UMD/SystemJS module naming options.
getModuleId?: Function,
moduleRoot?: string,
moduleIds?: boolean,
moduleId?: string,

// Deprecate top level parserOpts
parserOpts?: {},
// Deprecate top level generatorOpts
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-core/src/transformation/normalize-opts.js
Expand Up @@ -13,9 +13,9 @@ export default function normalizeOptions(config: ResolvedConfig): {} {
sourceType = "module",
inputSourceMap,
sourceMaps = !!inputSourceMap,

moduleRoot,
sourceRoot = moduleRoot,
sourceRoot = process.env.BABEL_8_BREAKING
? undefined
: config.options.moduleRoot,

sourceFileName = path.basename(filenameRelative),

Expand Down
11 changes: 7 additions & 4 deletions packages/babel-core/src/transformation/plugin-pass.js
Expand Up @@ -44,11 +44,14 @@ export default class PluginPass {
return this.file.addImport();
}

getModuleName(): ?string {
return this.file.getModuleName();
}

buildCodeFrameError(node: ?NodeLocation, msg: string, Error?: typeof Error) {
return this.file.buildCodeFrameError(node, msg, Error);
}
}

if (!process.env.BABEL_8_BREAKING) {
// $FlowIgnore
PluginPass.prototype.getModuleName = function getModuleName(): ?string {
return this.file.getModuleName();
};
}
48 changes: 39 additions & 9 deletions packages/babel-helper-module-transforms/src/get-module-name.ts
@@ -1,21 +1,51 @@
type RootOptions = {
filename?: string;
filenameRelative?: string;
sourceRoot?: string;
};

type PluginOptions = {
moduleId?: string;
moduleIds?: boolean;
getModuleId?: (moduleName: string) => string | null | undefined;
moduleRoot?: string;
};

if (!process.env.BABEL_8_BREAKING) {
const originalGetModuleName = getModuleName;

// @ts-expect-error TS doesn't like reassigning a function.
// eslint-disable-next-line no-func-assign
getModuleName = function getModuleName(
rootOpts: RootOptions & PluginOptions,
pluginOpts: PluginOptions,
): string | null {
return originalGetModuleName(rootOpts, {
moduleId: pluginOpts.moduleId ?? rootOpts.moduleId,
moduleIds: pluginOpts.moduleIds ?? rootOpts.moduleIds,
getModuleId: pluginOpts.getModuleId ?? rootOpts.getModuleId,
moduleRoot: pluginOpts.moduleRoot ?? rootOpts.moduleRoot,
});
};
}

export default function getModuleName(
rootOpts: any,
pluginOpts: any,
): string | undefined | null {
rootOpts: RootOptions,
pluginOpts: PluginOptions,
): string | null {
const {
filename,
filenameRelative = filename,

sourceRoot = pluginOpts.moduleRoot ?? rootOpts.moduleRoot,
sourceRoot = pluginOpts.moduleRoot,
} = rootOpts;

const {
moduleId = rootOpts.moduleId,
moduleIds = rootOpts.moduleIds ?? !!moduleId,
moduleId,
moduleIds = !!moduleId,

getModuleId = rootOpts.getModuleId,
getModuleId,

moduleRoot = rootOpts.moduleRoot ?? sourceRoot,
moduleRoot = sourceRoot,
} = pluginOpts;

if (!moduleIds) return null;
Expand Down
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name"
Expand Down
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true
}
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name",
Expand Down
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true,
"plugins": [["transform-modules-amd", { "loose": true }]]
Expand Down
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"moduleIds": true,
"moduleId": "my custom module name"
}
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name",
Expand Down
Expand Up @@ -7,9 +7,9 @@
"my custom module name": "foo.bar"
},
"exactGlobals": true,
"loose": true
"loose": true,
"moduleId": "my custom module name"
}
]
],
"moduleId": "my custom module name"
]
}
Expand Up @@ -7,9 +7,9 @@
"my custom module name": "foo.bar.baz.qux"
},
"exactGlobals": true,
"loose": true
"loose": true,
"moduleId": "my custom module name"
}
]
],
"moduleId": "my custom module name"
]
}
Expand Up @@ -7,9 +7,9 @@
"my custom module name": "baz"
},
"exactGlobals": true,
"loose": true
"loose": true,
"moduleId": "my custom module name"
}
]
],
"moduleId": "my custom module name"
]
}
@@ -1,5 +1,4 @@
{
"sourceType": "module",
"moduleId": "MyLib",
"plugins": [["transform-modules-umd", { "loose": true }]]
"plugins": [["transform-modules-umd", { "loose": true, "moduleId": "MyLib" }]]
}
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true,
"plugins": [["transform-modules-umd", { "loose": true }]]
Expand Down
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
[
"transform-modules-umd",
Expand Down
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true,
"moduleId": "my custom module name"
Expand Down
Expand Up @@ -6,9 +6,9 @@
"globals": {
"my custom module name": "foo.bar"
},
"exactGlobals": true
"exactGlobals": true,
"moduleId": "my custom module name"
}
]
],
"moduleId": "my custom module name"
]
}
Expand Up @@ -6,9 +6,9 @@
"globals": {
"my custom module name": "foo.bar.baz.qux"
},
"exactGlobals": true
"exactGlobals": true,
"moduleId": "my custom module name"
}
]
],
"moduleId": "my custom module name"
]
}
Expand Up @@ -6,9 +6,9 @@
"globals": {
"my custom module name": "baz"
},
"exactGlobals": true
"exactGlobals": true,
"moduleId": "my custom module name"
}
]
],
"moduleId": "my custom module name"
]
}
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"moduleId": "MyLib"
"plugins": [["transform-modules-umd", { "moduleId": "MyLib" }]]
}
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"sourceType": "module",
"moduleIds": true
}
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
[
"transform-modules-umd",
Expand Down

0 comments on commit 8e8954b

Please sign in to comment.