Skip to content

Commit 8e8954b

Browse files
[babel 8] Remove module-specific options from @babel/core (#12724)
1 parent 6b39baf commit 8e8954b

File tree

26 files changed

+137
-86
lines changed

26 files changed

+137
-86
lines changed

packages/babel-cli/src/babel/options.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,19 @@ commander.option(
9898
"The root from which all sources are relative.",
9999
);
100100

101-
// Config params for certain module output formats.
102-
commander.option(
103-
"--module-root [filename]",
104-
// eslint-disable-next-line max-len
105-
"Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.",
106-
);
107-
commander.option("-M, --module-ids", "Insert an explicit id for modules.");
108-
commander.option(
109-
"--module-id [string]",
110-
"Specify a custom name for module ids.",
111-
);
101+
if (!process.env.BABEL_8_BREAKING) {
102+
// Config params for certain module output formats.
103+
commander.option(
104+
"--module-root [filename]",
105+
// eslint-disable-next-line max-len
106+
"Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.",
107+
);
108+
commander.option("-M, --module-ids", "Insert an explicit id for modules.");
109+
commander.option(
110+
"--module-id [string]",
111+
"Specify a custom name for module ids.",
112+
);
113+
}
112114

113115
// "babel" command specific arguments that are not passed to @babel/core.
114116
commander.option(
@@ -277,9 +279,6 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
277279
sourceMaps: opts.sourceMaps,
278280
sourceFileName: opts.sourceFileName,
279281
sourceRoot: opts.sourceRoot,
280-
moduleRoot: opts.moduleRoot,
281-
moduleIds: opts.moduleIds,
282-
moduleId: opts.moduleId,
283282

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

291+
if (!process.env.BABEL_8_BREAKING) {
292+
// $FlowIgnore
293+
Object.assign(babelOptions, {
294+
moduleRoot: opts.moduleRoot,
295+
moduleIds: opts.moduleIds,
296+
moduleId: opts.moduleId,
297+
});
298+
}
299+
292300
// If the @babel/cli version is newer than the @babel/core version, and we have added
293301
// new options for @babel/core, we'll potentially get option validation errors from
294302
// @babel/core. To avoid that, we delete undefined options, so @babel/core will only

packages/babel-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@babel/code-frame": "workspace:^7.12.13",
5151
"@babel/generator": "workspace:^7.13.9",
5252
"@babel/helper-compilation-targets": "workspace:^7.13.13",
53-
"@babel/helper-module-transforms": "workspace:^7.13.12",
53+
"@babel/helper-module-transforms": "condition:BABEL_8_BREAKING ? : workspace:^7.13.12",
5454
"@babel/helpers": "workspace:^7.13.10",
5555
"@babel/parser": "workspace:^7.13.13",
5656
"@babel/template": "workspace:^7.12.13",

packages/babel-core/src/config/validation/options.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,22 @@ const COMMON_VALIDATORS: ValidatorSet = {
173173
sourceRoot: (assertString: Validator<
174174
$PropertyType<ValidatedOptions, "sourceRoot">,
175175
>),
176-
getModuleId: (assertFunction: Validator<
177-
$PropertyType<ValidatedOptions, "getModuleId">,
178-
>),
179-
moduleRoot: (assertString: Validator<
180-
$PropertyType<ValidatedOptions, "moduleRoot">,
181-
>),
182-
moduleIds: (assertBoolean: Validator<
183-
$PropertyType<ValidatedOptions, "moduleIds">,
184-
>),
185-
moduleId: (assertString: Validator<
186-
$PropertyType<ValidatedOptions, "moduleId">,
187-
>),
188176
parserOpts: (assertObject: Validator<
189177
$PropertyType<ValidatedOptions, "parserOpts">,
190178
>),
191179
generatorOpts: (assertObject: Validator<
192180
$PropertyType<ValidatedOptions, "generatorOpts">,
193181
>),
194182
};
183+
if (!process.env.BABEL_8_BREAKING) {
184+
Object.assign(COMMON_VALIDATORS, {
185+
getModuleId: assertFunction,
186+
moduleRoot: assertString,
187+
moduleIds: assertBoolean,
188+
moduleId: assertString,
189+
});
190+
}
191+
195192
export type InputOptions = ValidatedOptions;
196193

197194
export type ValidatedOptions = {
@@ -253,12 +250,6 @@ export type ValidatedOptions = {
253250
sourceFileName?: string,
254251
sourceRoot?: string,
255252

256-
// AMD/UMD/SystemJS module naming options.
257-
getModuleId?: Function,
258-
moduleRoot?: string,
259-
moduleIds?: boolean,
260-
moduleId?: string,
261-
262253
// Deprecate top level parserOpts
263254
parserOpts?: {},
264255
// Deprecate top level generatorOpts

packages/babel-core/src/transformation/normalize-opts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export default function normalizeOptions(config: ResolvedConfig): {} {
1313
sourceType = "module",
1414
inputSourceMap,
1515
sourceMaps = !!inputSourceMap,
16-
17-
moduleRoot,
18-
sourceRoot = moduleRoot,
16+
sourceRoot = process.env.BABEL_8_BREAKING
17+
? undefined
18+
: config.options.moduleRoot,
1919

2020
sourceFileName = path.basename(filenameRelative),
2121

packages/babel-core/src/transformation/plugin-pass.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ export default class PluginPass {
4444
return this.file.addImport();
4545
}
4646

47-
getModuleName(): ?string {
48-
return this.file.getModuleName();
49-
}
50-
5147
buildCodeFrameError(node: ?NodeLocation, msg: string, Error?: typeof Error) {
5248
return this.file.buildCodeFrameError(node, msg, Error);
5349
}
5450
}
51+
52+
if (!process.env.BABEL_8_BREAKING) {
53+
// $FlowIgnore
54+
PluginPass.prototype.getModuleName = function getModuleName(): ?string {
55+
return this.file.getModuleName();
56+
};
57+
}

packages/babel-helper-module-transforms/src/get-module-name.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
1+
type RootOptions = {
2+
filename?: string;
3+
filenameRelative?: string;
4+
sourceRoot?: string;
5+
};
6+
7+
type PluginOptions = {
8+
moduleId?: string;
9+
moduleIds?: boolean;
10+
getModuleId?: (moduleName: string) => string | null | undefined;
11+
moduleRoot?: string;
12+
};
13+
14+
if (!process.env.BABEL_8_BREAKING) {
15+
const originalGetModuleName = getModuleName;
16+
17+
// @ts-expect-error TS doesn't like reassigning a function.
18+
// eslint-disable-next-line no-func-assign
19+
getModuleName = function getModuleName(
20+
rootOpts: RootOptions & PluginOptions,
21+
pluginOpts: PluginOptions,
22+
): string | null {
23+
return originalGetModuleName(rootOpts, {
24+
moduleId: pluginOpts.moduleId ?? rootOpts.moduleId,
25+
moduleIds: pluginOpts.moduleIds ?? rootOpts.moduleIds,
26+
getModuleId: pluginOpts.getModuleId ?? rootOpts.getModuleId,
27+
moduleRoot: pluginOpts.moduleRoot ?? rootOpts.moduleRoot,
28+
});
29+
};
30+
}
31+
132
export default function getModuleName(
2-
rootOpts: any,
3-
pluginOpts: any,
4-
): string | undefined | null {
33+
rootOpts: RootOptions,
34+
pluginOpts: PluginOptions,
35+
): string | null {
536
const {
637
filename,
738
filenameRelative = filename,
8-
9-
sourceRoot = pluginOpts.moduleRoot ?? rootOpts.moduleRoot,
39+
sourceRoot = pluginOpts.moduleRoot,
1040
} = rootOpts;
1141

1242
const {
13-
moduleId = rootOpts.moduleId,
14-
moduleIds = rootOpts.moduleIds ?? !!moduleId,
43+
moduleId,
44+
moduleIds = !!moduleId,
1545

16-
getModuleId = rootOpts.getModuleId,
46+
getModuleId,
1747

18-
moduleRoot = rootOpts.moduleRoot ?? sourceRoot,
48+
moduleRoot = sourceRoot,
1949
} = pluginOpts;
2050

2151
if (!moduleIds) return null;

packages/babel-plugin-transform-modules-amd/test/fixtures/amd/get-module-name-option-compat/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"BABEL_8_BREAKING": false,
23
"sourceType": "module",
34
"moduleIds": true,
45
"moduleId": "my custom module name"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"BABEL_8_BREAKING": false,
23
"sourceType": "module",
34
"moduleIds": true
45
}

packages/babel-plugin-transform-modules-amd/test/fixtures/loose/get-module-name-option-compat/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"BABEL_8_BREAKING": false,
23
"sourceType": "module",
34
"moduleIds": true,
45
"moduleId": "my custom module name",

packages/babel-plugin-transform-modules-amd/test/fixtures/loose/module-name-compat/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"BABEL_8_BREAKING": false,
23
"sourceType": "module",
34
"moduleIds": true,
45
"plugins": [["transform-modules-amd", { "loose": true }]]

0 commit comments

Comments
 (0)