[babel 8] Remove module-specific options from @babel/core (#12724)

This commit is contained in:
Nicolò Ribaudo 2021-03-28 01:27:37 +01:00 committed by GitHub
parent 6b39bafab6
commit 8e8954b470
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 137 additions and 86 deletions

View File

@ -98,17 +98,19 @@ commander.option(
"The root from which all sources are relative.", "The root from which all sources are relative.",
); );
// Config params for certain module output formats. if (!process.env.BABEL_8_BREAKING) {
commander.option( // Config params for certain module output formats.
"--module-root [filename]", commander.option(
// eslint-disable-next-line max-len "--module-root [filename]",
"Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.", // 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( commander.option("-M, --module-ids", "Insert an explicit id for modules.");
"--module-id [string]", commander.option(
"Specify a custom name for module ids.", "--module-id [string]",
); "Specify a custom name for module ids.",
);
}
// "babel" command specific arguments that are not passed to @babel/core. // "babel" command specific arguments that are not passed to @babel/core.
commander.option( commander.option(
@ -277,9 +279,6 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
sourceMaps: opts.sourceMaps, sourceMaps: opts.sourceMaps,
sourceFileName: opts.sourceFileName, sourceFileName: opts.sourceFileName,
sourceRoot: opts.sourceRoot, sourceRoot: opts.sourceRoot,
moduleRoot: opts.moduleRoot,
moduleIds: opts.moduleIds,
moduleId: opts.moduleId,
// Commander will default the "--no-" arguments to true, but we want to // Commander will default the "--no-" arguments to true, but we want to
// leave them undefined so that @babel/core can handle the // leave them undefined so that @babel/core can handle the
@ -289,6 +288,15 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
comments: opts.comments === true ? undefined : opts.comments, 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 // 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 // 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 // @babel/core. To avoid that, we delete undefined options, so @babel/core will only

View File

@ -50,7 +50,7 @@
"@babel/code-frame": "workspace:^7.12.13", "@babel/code-frame": "workspace:^7.12.13",
"@babel/generator": "workspace:^7.13.9", "@babel/generator": "workspace:^7.13.9",
"@babel/helper-compilation-targets": "workspace:^7.13.13", "@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/helpers": "workspace:^7.13.10",
"@babel/parser": "workspace:^7.13.13", "@babel/parser": "workspace:^7.13.13",
"@babel/template": "workspace:^7.12.13", "@babel/template": "workspace:^7.12.13",

View File

@ -173,18 +173,6 @@ const COMMON_VALIDATORS: ValidatorSet = {
sourceRoot: (assertString: Validator< sourceRoot: (assertString: Validator<
$PropertyType<ValidatedOptions, "sourceRoot">, $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< parserOpts: (assertObject: Validator<
$PropertyType<ValidatedOptions, "parserOpts">, $PropertyType<ValidatedOptions, "parserOpts">,
>), >),
@ -192,6 +180,15 @@ const COMMON_VALIDATORS: ValidatorSet = {
$PropertyType<ValidatedOptions, "generatorOpts">, $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 InputOptions = ValidatedOptions;
export type ValidatedOptions = { export type ValidatedOptions = {
@ -253,12 +250,6 @@ export type ValidatedOptions = {
sourceFileName?: string, sourceFileName?: string,
sourceRoot?: string, sourceRoot?: string,
// AMD/UMD/SystemJS module naming options.
getModuleId?: Function,
moduleRoot?: string,
moduleIds?: boolean,
moduleId?: string,
// Deprecate top level parserOpts // Deprecate top level parserOpts
parserOpts?: {}, parserOpts?: {},
// Deprecate top level generatorOpts // Deprecate top level generatorOpts

View File

@ -13,9 +13,9 @@ export default function normalizeOptions(config: ResolvedConfig): {} {
sourceType = "module", sourceType = "module",
inputSourceMap, inputSourceMap,
sourceMaps = !!inputSourceMap, sourceMaps = !!inputSourceMap,
sourceRoot = process.env.BABEL_8_BREAKING
moduleRoot, ? undefined
sourceRoot = moduleRoot, : config.options.moduleRoot,
sourceFileName = path.basename(filenameRelative), sourceFileName = path.basename(filenameRelative),

View File

@ -44,11 +44,14 @@ export default class PluginPass {
return this.file.addImport(); return this.file.addImport();
} }
getModuleName(): ?string {
return this.file.getModuleName();
}
buildCodeFrameError(node: ?NodeLocation, msg: string, Error?: typeof Error) { buildCodeFrameError(node: ?NodeLocation, msg: string, Error?: typeof Error) {
return this.file.buildCodeFrameError(node, msg, 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();
};
}

View File

@ -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( export default function getModuleName(
rootOpts: any, rootOpts: RootOptions,
pluginOpts: any, pluginOpts: PluginOptions,
): string | undefined | null { ): string | null {
const { const {
filename, filename,
filenameRelative = filename, filenameRelative = filename,
sourceRoot = pluginOpts.moduleRoot,
sourceRoot = pluginOpts.moduleRoot ?? rootOpts.moduleRoot,
} = rootOpts; } = rootOpts;
const { const {
moduleId = rootOpts.moduleId, moduleId,
moduleIds = rootOpts.moduleIds ?? !!moduleId, moduleIds = !!moduleId,
getModuleId = rootOpts.getModuleId, getModuleId,
moduleRoot = rootOpts.moduleRoot ?? sourceRoot, moduleRoot = sourceRoot,
} = pluginOpts; } = pluginOpts;
if (!moduleIds) return null; if (!moduleIds) return null;

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "moduleIds": true,
"moduleId": "my custom module name" "moduleId": "my custom module name"

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true "moduleIds": true
} }

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "moduleIds": true,
"moduleId": "my custom module name", "moduleId": "my custom module name",

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "moduleIds": true,
"plugins": [["transform-modules-amd", { "loose": true }]] "plugins": [["transform-modules-amd", { "loose": true }]]

View File

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

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "moduleIds": true,
"moduleId": "my custom module name", "moduleId": "my custom module name",

View File

@ -7,9 +7,9 @@
"my custom module name": "foo.bar" "my custom module name": "foo.bar"
}, },
"exactGlobals": true, "exactGlobals": true,
"loose": true "loose": true,
"moduleId": "my custom module name"
} }
] ]
], ]
"moduleId": "my custom module name"
} }

View File

@ -7,9 +7,9 @@
"my custom module name": "foo.bar.baz.qux" "my custom module name": "foo.bar.baz.qux"
}, },
"exactGlobals": true, "exactGlobals": true,
"loose": true "loose": true,
"moduleId": "my custom module name"
} }
] ]
], ]
"moduleId": "my custom module name"
} }

View File

@ -7,9 +7,9 @@
"my custom module name": "baz" "my custom module name": "baz"
}, },
"exactGlobals": true, "exactGlobals": true,
"loose": true "loose": true,
"moduleId": "my custom module name"
} }
] ]
], ]
"moduleId": "my custom module name"
} }

View File

@ -1,5 +1,4 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleId": "MyLib", "plugins": [["transform-modules-umd", { "loose": true, "moduleId": "MyLib" }]]
"plugins": [["transform-modules-umd", { "loose": true }]]
} }

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "moduleIds": true,
"plugins": [["transform-modules-umd", { "loose": true }]] "plugins": [["transform-modules-umd", { "loose": true }]]

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"plugins": [ "plugins": [
[ [
"transform-modules-umd", "transform-modules-umd",

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true, "moduleIds": true,
"moduleId": "my custom module name" "moduleId": "my custom module name"

View File

@ -6,9 +6,9 @@
"globals": { "globals": {
"my custom module name": "foo.bar" "my custom module name": "foo.bar"
}, },
"exactGlobals": true "exactGlobals": true,
"moduleId": "my custom module name"
} }
] ]
], ]
"moduleId": "my custom module name"
} }

View File

@ -6,9 +6,9 @@
"globals": { "globals": {
"my custom module name": "foo.bar.baz.qux" "my custom module name": "foo.bar.baz.qux"
}, },
"exactGlobals": true "exactGlobals": true,
"moduleId": "my custom module name"
} }
] ]
], ]
"moduleId": "my custom module name"
} }

View File

@ -6,9 +6,9 @@
"globals": { "globals": {
"my custom module name": "baz" "my custom module name": "baz"
}, },
"exactGlobals": true "exactGlobals": true,
"moduleId": "my custom module name"
} }
] ]
], ]
"moduleId": "my custom module name"
} }

View File

@ -1,4 +1,4 @@
{ {
"sourceType": "module", "sourceType": "module",
"moduleId": "MyLib" "plugins": [["transform-modules-umd", { "moduleId": "MyLib" }]]
} }

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"sourceType": "module", "sourceType": "module",
"moduleIds": true "moduleIds": true
} }

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"plugins": [ "plugins": [
[ [
"transform-modules-umd", "transform-modules-umd",

View File

@ -204,7 +204,7 @@ __metadata:
"@babel/code-frame": "workspace:^7.12.13" "@babel/code-frame": "workspace:^7.12.13"
"@babel/generator": "workspace:^7.13.9" "@babel/generator": "workspace:^7.13.9"
"@babel/helper-compilation-targets": "workspace:^7.13.13" "@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/helper-transform-fixture-test-runner": "workspace:*" "@babel/helper-transform-fixture-test-runner": "workspace:*"
"@babel/helpers": "workspace:^7.13.10" "@babel/helpers": "workspace:^7.13.10"
"@babel/parser": "workspace:^7.13.13" "@babel/parser": "workspace:^7.13.13"
@ -639,6 +639,30 @@ __metadata:
languageName: unknown languageName: unknown
linkType: soft linkType: soft
"@babel/helper-module-transforms-BABEL_8_BREAKING-false@npm:@babel/helper-module-transforms@workspace:^7.13.12, @babel/helper-module-transforms@workspace:^7.13.0, @babel/helper-module-transforms@workspace:packages/babel-helper-module-transforms":
version: 0.0.0-use.local
resolution: "@babel/helper-module-transforms@workspace:packages/babel-helper-module-transforms"
dependencies:
"@babel/helper-module-imports": "workspace:^7.13.12"
"@babel/helper-replace-supers": "workspace:^7.13.12"
"@babel/helper-simple-access": "workspace:^7.13.12"
"@babel/helper-split-export-declaration": "workspace:^7.12.13"
"@babel/helper-validator-identifier": "workspace:^7.12.11"
"@babel/template": "workspace:^7.12.13"
"@babel/traverse": "workspace:^7.13.0"
"@babel/types": "workspace:^7.13.12"
languageName: unknown
linkType: soft
"@babel/helper-module-transforms@condition:BABEL_8_BREAKING ? : workspace:^7.13.12":
version: 0.0.0-condition-130862
resolution: "@babel/helper-module-transforms@condition:BABEL_8_BREAKING?:workspace:^7.13.12#130862"
dependencies:
"@babel/helper-module-transforms-BABEL_8_BREAKING-false": "npm:@babel/helper-module-transforms@workspace:^7.13.12"
checksum: 099f985d9558228aa29430a51630015ea02a47f2714cc392f0105921e768c4998a70686942c51defe174cd5ed9f221ab0e4505a611f40e7a68158748c4bbfb8e
languageName: node
linkType: hard
"@babel/helper-module-transforms@npm:^7.12.1, @babel/helper-module-transforms@npm:^7.12.13, @babel/helper-module-transforms@npm:^7.13.0": "@babel/helper-module-transforms@npm:^7.12.1, @babel/helper-module-transforms@npm:^7.12.13, @babel/helper-module-transforms@npm:^7.13.0":
version: 7.13.0 version: 7.13.0
resolution: "@babel/helper-module-transforms@npm:7.13.0" resolution: "@babel/helper-module-transforms@npm:7.13.0"
@ -656,21 +680,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@babel/helper-module-transforms@workspace:^7.13.0, @babel/helper-module-transforms@workspace:^7.13.12, @babel/helper-module-transforms@workspace:packages/babel-helper-module-transforms":
version: 0.0.0-use.local
resolution: "@babel/helper-module-transforms@workspace:packages/babel-helper-module-transforms"
dependencies:
"@babel/helper-module-imports": "workspace:^7.13.12"
"@babel/helper-replace-supers": "workspace:^7.13.12"
"@babel/helper-simple-access": "workspace:^7.13.12"
"@babel/helper-split-export-declaration": "workspace:^7.12.13"
"@babel/helper-validator-identifier": "workspace:^7.12.11"
"@babel/template": "workspace:^7.12.13"
"@babel/traverse": "workspace:^7.13.0"
"@babel/types": "workspace:^7.13.12"
languageName: unknown
linkType: soft
"@babel/helper-optimise-call-expression@npm:^7.12.13": "@babel/helper-optimise-call-expression@npm:^7.12.13":
version: 7.12.13 version: 7.12.13
resolution: "@babel/helper-optimise-call-expression@npm:7.12.13" resolution: "@babel/helper-optimise-call-expression@npm:7.12.13"