From 8ceca8eacc6f1cfee83a9ca7a87a050bd3cbe00a Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 9 Mar 2023 23:40:02 +0800 Subject: [PATCH 1/6] fix --- .../src/config/files/module-types.ts | 9 +++++---- packages/babel-core/test/config-ts.js | 20 +++++++++++-------- .../invalid-cts-register/babel.config.cts | 1 - .../simple-cts-with-ts-node/babel.config.cts | 6 +++++- .../config-ts/simple-cts/babel.config.cts | 6 +++++- .../babel-helper-module-imports/src/index.ts | 2 +- .../src/is-module.ts | 17 ++++++++-------- .../src/index.ts | 6 +++--- .../src/index.ts | 4 ++-- packages/babel-preset-typescript/package.json | 1 + packages/babel-preset-typescript/src/index.ts | 9 ++++++++- .../import-in-cts/options.json | 3 --- .../node-extensions/import-in-cts/output.js | 3 +++ yarn.lock | 1 + 14 files changed, 55 insertions(+), 33 deletions(-) delete mode 100644 packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json create mode 100644 packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/output.js diff --git a/packages/babel-core/src/config/files/module-types.ts b/packages/babel-core/src/config/files/module-types.ts index 774c885c00d7..1b1ea903492e 100644 --- a/packages/babel-core/src/config/files/module-types.ts +++ b/packages/babel-core/src/config/files/module-types.ts @@ -68,16 +68,16 @@ function loadCtsDefault(filepath: string) { configFile: false, sourceType: "script", sourceMaps: "inline", + sourceFileName: path.basename(filepath), presets: [ [ getTSPreset(filepath), { - disallowAmbiguousJSXLike: true, onlyRemoveTypeImports: true, optimizeConstEnums: true, ...(process.env.BABEL_8_BREAKING - ? { ignoreExtensions: true } - : { allExtensions: true, allowDeclareFields: true }), + ? {} + : { allowDeclareFields: true }), }, ], ], @@ -100,7 +100,8 @@ function loadCtsDefault(filepath: string) { require.extensions[ext] = handler; } try { - return endHiddenCallStack(require)(filepath); + const module = endHiddenCallStack(require)(filepath); + return module?.__esModule ? module.default : module; } finally { if (!hasTsSupport) { if (require.extensions[ext] === handler) delete require.extensions[ext]; diff --git a/packages/babel-core/test/config-ts.js b/packages/babel-core/test/config-ts.js index e102d9928055..239521fc2055 100644 --- a/packages/babel-core/test/config-ts.js +++ b/packages/babel-core/test/config-ts.js @@ -42,10 +42,12 @@ const shouldSkip = semver.lt(process.version, "12.0.0") || USE_ESM; }); expect(config.options.targets).toMatchInlineSnapshot(` - Object { - "node": "12.0.0", - } - `); + Object { + "node": "12.0.0", + } + `); + + expect(config.options.sourceRoot).toMatchInlineSnapshot(`"/a/b"`); }); it("should throw with invalid .ts register", () => { @@ -89,10 +91,12 @@ const shouldSkip = semver.lt(process.version, "12.0.0") || USE_ESM; }); expect(config.options.targets).toMatchInlineSnapshot(` - Object { - "node": "12.0.0", - } - `); + Object { + "node": "12.0.0", + } + `); + + expect(config.options.sourceRoot).toMatchInlineSnapshot(`"/a/b"`); } finally { service.enabled(false); } diff --git a/packages/babel-core/test/fixtures/config-ts/invalid-cts-register/babel.config.cts b/packages/babel-core/test/fixtures/config-ts/invalid-cts-register/babel.config.cts index 9817b4f090cd..5e3ee7799500 100644 --- a/packages/babel-core/test/fixtures/config-ts/invalid-cts-register/babel.config.cts +++ b/packages/babel-core/test/fixtures/config-ts/invalid-cts-register/babel.config.cts @@ -1,2 +1 @@ type config = any; -module.exports = { targets: "node 12.0.0" } as config; diff --git a/packages/babel-core/test/fixtures/config-ts/simple-cts-with-ts-node/babel.config.cts b/packages/babel-core/test/fixtures/config-ts/simple-cts-with-ts-node/babel.config.cts index 9817b4f090cd..97a9e6dccfc3 100644 --- a/packages/babel-core/test/fixtures/config-ts/simple-cts-with-ts-node/babel.config.cts +++ b/packages/babel-core/test/fixtures/config-ts/simple-cts-with-ts-node/babel.config.cts @@ -1,2 +1,6 @@ +import path from "path"; type config = any; -module.exports = { targets: "node 12.0.0" } as config; +export default { + targets: "node 12.0.0", + sourceRoot: path.posix.join("/a", "b"), +} as config; diff --git a/packages/babel-core/test/fixtures/config-ts/simple-cts/babel.config.cts b/packages/babel-core/test/fixtures/config-ts/simple-cts/babel.config.cts index 9817b4f090cd..97a9e6dccfc3 100644 --- a/packages/babel-core/test/fixtures/config-ts/simple-cts/babel.config.cts +++ b/packages/babel-core/test/fixtures/config-ts/simple-cts/babel.config.cts @@ -1,2 +1,6 @@ +import path from "path"; type config = any; -module.exports = { targets: "node 12.0.0" } as config; +export default { + targets: "node 12.0.0", + sourceRoot: path.posix.join("/a", "b"), +} as config; diff --git a/packages/babel-helper-module-imports/src/index.ts b/packages/babel-helper-module-imports/src/index.ts index 183aaabd6fe0..f927e1afaffa 100644 --- a/packages/babel-helper-module-imports/src/index.ts +++ b/packages/babel-helper-module-imports/src/index.ts @@ -4,7 +4,7 @@ import type * as t from "@babel/types"; export { ImportInjector }; -export { default as isModule } from "./is-module"; +export { default as isModule, isModuleOrCts } from "./is-module"; export function addDefault( path: NodePath, diff --git a/packages/babel-helper-module-imports/src/is-module.ts b/packages/babel-helper-module-imports/src/is-module.ts index bfcd74802cb4..41a6d526af12 100644 --- a/packages/babel-helper-module-imports/src/is-module.ts +++ b/packages/babel-helper-module-imports/src/is-module.ts @@ -1,16 +1,17 @@ import type { NodePath } from "@babel/traverse"; -import type * as t from "@babel/types"; +import * as t from "@babel/types"; /** * A small utility to check if a file qualifies as a module. */ export default function isModule(path: NodePath) { - const { sourceType } = path.node; - if (sourceType !== "module" && sourceType !== "script") { - throw path.buildCodeFrameError( - `Unknown sourceType "${sourceType}", cannot transform.`, - ); - } - return path.node.sourceType === "module"; } + +export function isModuleOrCts(path: NodePath) { + const { node } = path; + return ( + node.sourceType === "module" || + node.body.some(node => t.isImportOrExportDeclaration(node)) + ); +} diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index 66ad47bdcc2b..e9bf715dd09a 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -17,7 +17,7 @@ import { import type * as t from "@babel/types"; import template from "@babel/template"; -import { isModule } from "@babel/helper-module-imports"; +import { isModule, isModuleOrCts } from "@babel/helper-module-imports"; import rewriteThis from "./rewrite-this"; import rewriteLiveReferences from "./rewrite-live-references"; @@ -40,7 +40,7 @@ export { buildDynamicImport, getDynamicImportSource } from "./dynamic-import"; export { default as getModuleName } from "./get-module-name"; export type { PluginOptions } from "./get-module-name"; -export { hasExports, isSideEffectImport, isModule, rewriteThis }; +export { hasExports, isSideEffectImport, isModule, isModuleOrCts, rewriteThis }; export interface RewriteModuleStatementsAndPrepareHeaderOptions { exportName?: string; @@ -86,7 +86,7 @@ export function rewriteModuleStatementsAndPrepareHeader( }: RewriteModuleStatementsAndPrepareHeaderOptions, ) { validateImportInteropOption(importInterop); - assert(isModule(path), "Cannot process module statements in a script"); + assert(isModuleOrCts(path), "Cannot process module statements in a script"); path.node.sourceType = "script"; const meta = normalizeModuleAndLoadMetadata(path, exportName, { diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.ts b/packages/babel-plugin-transform-modules-commonjs/src/index.ts index 43846666b095..8c128d3b5f0e 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.ts @@ -1,6 +1,5 @@ import { declare } from "@babel/helper-plugin-utils"; import { - isModule, rewriteModuleStatementsAndPrepareHeader, type RewriteModuleStatementsAndPrepareHeaderOptions, isSideEffectImport, @@ -8,6 +7,7 @@ import { ensureStatementsHoisted, wrapInterop, getModuleName, + isModuleOrCts, } from "@babel/helper-module-transforms"; import simplifyAccess from "@babel/helper-simple-access"; import { template, types as t } from "@babel/core"; @@ -183,7 +183,7 @@ export default declare((api, options: Options) => { Program: { exit(path, state) { - if (!isModule(path)) return; + if (!isModuleOrCts(path)) return; // Rename the bindings auto-injected into the scope so there is no // risk of conflict between the bindings. diff --git a/packages/babel-preset-typescript/package.json b/packages/babel-preset-typescript/package.json index 5de67bf0bc75..1ca43cc4bb2e 100644 --- a/packages/babel-preset-typescript/package.json +++ b/packages/babel-preset-typescript/package.json @@ -20,6 +20,7 @@ "@babel/helper-plugin-utils": "workspace:^", "@babel/helper-validator-option": "workspace:^", "@babel/plugin-syntax-jsx": "workspace:^", + "@babel/plugin-transform-modules-commonjs": "workspace:^", "@babel/plugin-transform-typescript": "workspace:^" }, "peerDependencies": { diff --git a/packages/babel-preset-typescript/src/index.ts b/packages/babel-preset-typescript/src/index.ts index c2df2778e194..0050ab0ee8b8 100644 --- a/packages/babel-preset-typescript/src/index.ts +++ b/packages/babel-preset-typescript/src/index.ts @@ -1,6 +1,7 @@ import { declarePreset } from "@babel/helper-plugin-utils"; import transformTypeScript from "@babel/plugin-transform-typescript"; import syntaxJSX from "@babel/plugin-syntax-jsx"; +import transformModulesCommonJS from "@babel/plugin-transform-modules-commonjs"; import normalizeOptions from "./normalize-options"; import type { Options } from "./normalize-options"; @@ -78,7 +79,13 @@ export default declarePreset((api, opts: Options) => { ? /\.cts$/ : filename => filename?.endsWith(".cts"), sourceType: "script", - plugins: getPlugins(false, true), + parserOpts: { + allowImportExportEverywhere: true, + }, + plugins: [ + [transformModulesCommonJS, { allowTopLevelThis: true }], + [transformTypeScript, pluginOptions(true)], + ], }, { test: !process.env.BABEL_8_BREAKING diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json deleted file mode 100644 index afbed324e92a..000000000000 --- a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)" -} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/output.js b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/output.js new file mode 100644 index 000000000000..867d1513ab8c --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/output.js @@ -0,0 +1,3 @@ +"use strict"; + +require("x"); diff --git a/yarn.lock b/yarn.lock index 53db84a84b96..668a2ac92b68 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3603,6 +3603,7 @@ __metadata: "@babel/helper-plugin-utils": "workspace:^" "@babel/helper-validator-option": "workspace:^" "@babel/plugin-syntax-jsx": "workspace:^" + "@babel/plugin-transform-modules-commonjs": "workspace:^" "@babel/plugin-transform-typescript": "workspace:^" peerDependencies: "@babel/core": ^7.0.0-0 From 682baf20a01fb6c9e966cdf26a36ae5fc76bdfb6 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 10 Mar 2023 01:36:17 +0800 Subject: [PATCH 2/6] fix --- babel.config.js | 2 +- package.json | 2 +- .../src/config/files/module-types.ts | 14 +++++++++- .../babel-helper-module-imports/src/index.ts | 2 +- .../src/is-module.ts | 10 +------ .../src/index.ts | 6 ++-- .../src/index.ts | 4 +-- yarn.lock | 28 +++++++++---------- 8 files changed, 36 insertions(+), 32 deletions(-) diff --git a/babel.config.js b/babel.config.js index 27ad7dc555b3..42b21256a46b 100644 --- a/babel.config.js +++ b/babel.config.js @@ -170,7 +170,7 @@ module.exports = function (api) { // presets are applied from right to left ["@babel/env", envOpts], [ - "@babel/preset-typescript", + "babel-preset-stable-typescript", { onlyRemoveTypeImports: true, allowDeclareFields: true, diff --git a/package.json b/package.json index 92b847b3facd..37db1ffba371 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,6 @@ "@babel/plugin-transform-modules-commonjs": "^7.21.2", "@babel/plugin-transform-runtime": "^7.21.0", "@babel/preset-env": "^7.20.2", - "@babel/preset-typescript": "^7.21.0", "@babel/runtime": "^7.21.0", "@rollup/plugin-babel": "^5.3.1", "@rollup/plugin-commonjs": "^22.0.2", @@ -42,6 +41,7 @@ "@typescript-eslint/eslint-plugin": "^5.55.0", "@typescript-eslint/parser": "^5.55.0", "babel-plugin-transform-charcodes": "^0.2.0", + "babel-preset-stable-typescript": "npm:@babel/preset-typescript@^7.21.0", "c8": "^7.12.0", "chalk": "^5.0.0", "charcodes": "^0.2.0", diff --git a/packages/babel-core/src/config/files/module-types.ts b/packages/babel-core/src/config/files/module-types.ts index 1b1ea903492e..fe65f8107e18 100644 --- a/packages/babel-core/src/config/files/module-types.ts +++ b/packages/babel-core/src/config/files/module-types.ts @@ -66,7 +66,7 @@ function loadCtsDefault(filepath: string) { const opts: InputOptions = { babelrc: false, configFile: false, - sourceType: "script", + sourceType: "unambiguous", sourceMaps: "inline", sourceFileName: path.basename(filepath), presets: [ @@ -134,6 +134,18 @@ async function loadMjsDefault(filepath: string) { function getTSPreset(filepath: string) { try { + // if ( + // semver.lte( + // // eslint-disable-next-line import/no-extraneous-dependencies + // require("@babel/preset-typescript/package.json").version, + // "7.21.0", + // ) + // ) { + // throw new ConfigError( + // "The installed version of `@babel/preset-typescript` is too old to support `.cts` configuration files.", + // filepath, + // ); + // } // eslint-disable-next-line import/no-extraneous-dependencies return require("@babel/preset-typescript"); } catch (error) { diff --git a/packages/babel-helper-module-imports/src/index.ts b/packages/babel-helper-module-imports/src/index.ts index f927e1afaffa..183aaabd6fe0 100644 --- a/packages/babel-helper-module-imports/src/index.ts +++ b/packages/babel-helper-module-imports/src/index.ts @@ -4,7 +4,7 @@ import type * as t from "@babel/types"; export { ImportInjector }; -export { default as isModule, isModuleOrCts } from "./is-module"; +export { default as isModule } from "./is-module"; export function addDefault( path: NodePath, diff --git a/packages/babel-helper-module-imports/src/is-module.ts b/packages/babel-helper-module-imports/src/is-module.ts index 41a6d526af12..c4d849447f9b 100644 --- a/packages/babel-helper-module-imports/src/is-module.ts +++ b/packages/babel-helper-module-imports/src/is-module.ts @@ -1,5 +1,5 @@ import type { NodePath } from "@babel/traverse"; -import * as t from "@babel/types"; +import type * as t from "@babel/types"; /** * A small utility to check if a file qualifies as a module. @@ -7,11 +7,3 @@ import * as t from "@babel/types"; export default function isModule(path: NodePath) { return path.node.sourceType === "module"; } - -export function isModuleOrCts(path: NodePath) { - const { node } = path; - return ( - node.sourceType === "module" || - node.body.some(node => t.isImportOrExportDeclaration(node)) - ); -} diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index e9bf715dd09a..66ad47bdcc2b 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -17,7 +17,7 @@ import { import type * as t from "@babel/types"; import template from "@babel/template"; -import { isModule, isModuleOrCts } from "@babel/helper-module-imports"; +import { isModule } from "@babel/helper-module-imports"; import rewriteThis from "./rewrite-this"; import rewriteLiveReferences from "./rewrite-live-references"; @@ -40,7 +40,7 @@ export { buildDynamicImport, getDynamicImportSource } from "./dynamic-import"; export { default as getModuleName } from "./get-module-name"; export type { PluginOptions } from "./get-module-name"; -export { hasExports, isSideEffectImport, isModule, isModuleOrCts, rewriteThis }; +export { hasExports, isSideEffectImport, isModule, rewriteThis }; export interface RewriteModuleStatementsAndPrepareHeaderOptions { exportName?: string; @@ -86,7 +86,7 @@ export function rewriteModuleStatementsAndPrepareHeader( }: RewriteModuleStatementsAndPrepareHeaderOptions, ) { validateImportInteropOption(importInterop); - assert(isModuleOrCts(path), "Cannot process module statements in a script"); + assert(isModule(path), "Cannot process module statements in a script"); path.node.sourceType = "script"; const meta = normalizeModuleAndLoadMetadata(path, exportName, { diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.ts b/packages/babel-plugin-transform-modules-commonjs/src/index.ts index 8c128d3b5f0e..43846666b095 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.ts @@ -1,5 +1,6 @@ import { declare } from "@babel/helper-plugin-utils"; import { + isModule, rewriteModuleStatementsAndPrepareHeader, type RewriteModuleStatementsAndPrepareHeaderOptions, isSideEffectImport, @@ -7,7 +8,6 @@ import { ensureStatementsHoisted, wrapInterop, getModuleName, - isModuleOrCts, } from "@babel/helper-module-transforms"; import simplifyAccess from "@babel/helper-simple-access"; import { template, types as t } from "@babel/core"; @@ -183,7 +183,7 @@ export default declare((api, options: Options) => { Program: { exit(path, state) { - if (!isModuleOrCts(path)) return; + if (!isModule(path)) return; // Rename the bindings auto-injected into the scope so there is no // risk of conflict between the bindings. diff --git a/yarn.lock b/yarn.lock index 668a2ac92b68..5f5c6d6bb701 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3581,19 +3581,6 @@ __metadata: languageName: unknown linkType: soft -"@babel/preset-typescript@npm:^7.21.0": - version: 7.21.0 - resolution: "@babel/preset-typescript@npm:7.21.0" - dependencies: - "@babel/helper-plugin-utils": ^7.20.2 - "@babel/helper-validator-option": ^7.21.0 - "@babel/plugin-transform-typescript": ^7.21.0 - peerDependencies: - "@babel/core": ^7.0.0-0 - checksum: 6e1f4d7294de2678fbaf36035e98847b2be432f40fe7a1204e5e45b8b05bcbe22902fe0d726e16af14de5bc08987fae28a7899871503fd661050d85f58755af6 - languageName: node - linkType: hard - "@babel/preset-typescript@workspace:^, @babel/preset-typescript@workspace:packages/babel-preset-typescript": version: 0.0.0-use.local resolution: "@babel/preset-typescript@workspace:packages/babel-preset-typescript" @@ -6051,6 +6038,19 @@ __metadata: languageName: node linkType: hard +"babel-preset-stable-typescript@npm:@babel/preset-typescript@^7.21.0": + version: 7.21.0 + resolution: "@babel/preset-typescript@npm:7.21.0" + dependencies: + "@babel/helper-plugin-utils": ^7.20.2 + "@babel/helper-validator-option": ^7.21.0 + "@babel/plugin-transform-typescript": ^7.21.0 + peerDependencies: + "@babel/core": ^7.0.0-0 + checksum: 6e1f4d7294de2678fbaf36035e98847b2be432f40fe7a1204e5e45b8b05bcbe22902fe0d726e16af14de5bc08987fae28a7899871503fd661050d85f58755af6 + languageName: node + linkType: hard + "babel@workspace:.": version: 0.0.0-use.local resolution: "babel@workspace:." @@ -6067,7 +6067,6 @@ __metadata: "@babel/plugin-transform-modules-commonjs": ^7.21.2 "@babel/plugin-transform-runtime": ^7.21.0 "@babel/preset-env": ^7.20.2 - "@babel/preset-typescript": ^7.21.0 "@babel/runtime": ^7.21.0 "@rollup/plugin-babel": ^5.3.1 "@rollup/plugin-commonjs": ^22.0.2 @@ -6078,6 +6077,7 @@ __metadata: "@typescript-eslint/eslint-plugin": ^5.55.0 "@typescript-eslint/parser": ^5.55.0 babel-plugin-transform-charcodes: ^0.2.0 + babel-preset-stable-typescript: "npm:@babel/preset-typescript@^7.21.0" c8: ^7.12.0 chalk: ^5.0.0 charcodes: ^0.2.0 From bb12ab0eb12c63c4a2a53885e3042774d5374df4 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 10 Mar 2023 01:48:46 +0800 Subject: [PATCH 3/6] review and fix --- babel.config.js | 2 +- package.json | 2 +- .../src/config/files/module-types.ts | 28 +++++++++++-------- yarn.lock | 28 +++++++++---------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/babel.config.js b/babel.config.js index 42b21256a46b..27ad7dc555b3 100644 --- a/babel.config.js +++ b/babel.config.js @@ -170,7 +170,7 @@ module.exports = function (api) { // presets are applied from right to left ["@babel/env", envOpts], [ - "babel-preset-stable-typescript", + "@babel/preset-typescript", { onlyRemoveTypeImports: true, allowDeclareFields: true, diff --git a/package.json b/package.json index 37db1ffba371..92b847b3facd 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "@babel/plugin-transform-modules-commonjs": "^7.21.2", "@babel/plugin-transform-runtime": "^7.21.0", "@babel/preset-env": "^7.20.2", + "@babel/preset-typescript": "^7.21.0", "@babel/runtime": "^7.21.0", "@rollup/plugin-babel": "^5.3.1", "@rollup/plugin-commonjs": "^22.0.2", @@ -41,7 +42,6 @@ "@typescript-eslint/eslint-plugin": "^5.55.0", "@typescript-eslint/parser": "^5.55.0", "babel-plugin-transform-charcodes": "^0.2.0", - "babel-preset-stable-typescript": "npm:@babel/preset-typescript@^7.21.0", "c8": "^7.12.0", "chalk": "^5.0.0", "charcodes": "^0.2.0", diff --git a/packages/babel-core/src/config/files/module-types.ts b/packages/babel-core/src/config/files/module-types.ts index fe65f8107e18..2944a61088d6 100644 --- a/packages/babel-core/src/config/files/module-types.ts +++ b/packages/babel-core/src/config/files/module-types.ts @@ -134,18 +134,22 @@ async function loadMjsDefault(filepath: string) { function getTSPreset(filepath: string) { try { - // if ( - // semver.lte( - // // eslint-disable-next-line import/no-extraneous-dependencies - // require("@babel/preset-typescript/package.json").version, - // "7.21.0", - // ) - // ) { - // throw new ConfigError( - // "The installed version of `@babel/preset-typescript` is too old to support `.cts` configuration files.", - // filepath, - // ); - // } + const packageJson = require("@babel/preset-typescript/package.json"); + + if ( + semver.lte( + // eslint-disable-next-line import/no-extraneous-dependencies + packageJson.version, + "7.21.0", + ) && + // ignore the version check if not published + !packageJson.conditions + ) { + throw new ConfigError( + "The installed version of `@babel/preset-typescript` is too old to support `.cts` configuration files.", + filepath, + ); + } // eslint-disable-next-line import/no-extraneous-dependencies return require("@babel/preset-typescript"); } catch (error) { diff --git a/yarn.lock b/yarn.lock index 5f5c6d6bb701..668a2ac92b68 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3581,6 +3581,19 @@ __metadata: languageName: unknown linkType: soft +"@babel/preset-typescript@npm:^7.21.0": + version: 7.21.0 + resolution: "@babel/preset-typescript@npm:7.21.0" + dependencies: + "@babel/helper-plugin-utils": ^7.20.2 + "@babel/helper-validator-option": ^7.21.0 + "@babel/plugin-transform-typescript": ^7.21.0 + peerDependencies: + "@babel/core": ^7.0.0-0 + checksum: 6e1f4d7294de2678fbaf36035e98847b2be432f40fe7a1204e5e45b8b05bcbe22902fe0d726e16af14de5bc08987fae28a7899871503fd661050d85f58755af6 + languageName: node + linkType: hard + "@babel/preset-typescript@workspace:^, @babel/preset-typescript@workspace:packages/babel-preset-typescript": version: 0.0.0-use.local resolution: "@babel/preset-typescript@workspace:packages/babel-preset-typescript" @@ -6038,19 +6051,6 @@ __metadata: languageName: node linkType: hard -"babel-preset-stable-typescript@npm:@babel/preset-typescript@^7.21.0": - version: 7.21.0 - resolution: "@babel/preset-typescript@npm:7.21.0" - dependencies: - "@babel/helper-plugin-utils": ^7.20.2 - "@babel/helper-validator-option": ^7.21.0 - "@babel/plugin-transform-typescript": ^7.21.0 - peerDependencies: - "@babel/core": ^7.0.0-0 - checksum: 6e1f4d7294de2678fbaf36035e98847b2be432f40fe7a1204e5e45b8b05bcbe22902fe0d726e16af14de5bc08987fae28a7899871503fd661050d85f58755af6 - languageName: node - linkType: hard - "babel@workspace:.": version: 0.0.0-use.local resolution: "babel@workspace:." @@ -6067,6 +6067,7 @@ __metadata: "@babel/plugin-transform-modules-commonjs": ^7.21.2 "@babel/plugin-transform-runtime": ^7.21.0 "@babel/preset-env": ^7.20.2 + "@babel/preset-typescript": ^7.21.0 "@babel/runtime": ^7.21.0 "@rollup/plugin-babel": ^5.3.1 "@rollup/plugin-commonjs": ^22.0.2 @@ -6077,7 +6078,6 @@ __metadata: "@typescript-eslint/eslint-plugin": ^5.55.0 "@typescript-eslint/parser": ^5.55.0 babel-plugin-transform-charcodes: ^0.2.0 - babel-preset-stable-typescript: "npm:@babel/preset-typescript@^7.21.0" c8: ^7.12.0 chalk: ^5.0.0 charcodes: ^0.2.0 From 2dd15b23a21556e7172d0c830081a3d317500d5a Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 10 Mar 2023 02:10:32 +0800 Subject: [PATCH 4/6] review --- .../src/config/files/module-types.ts | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/babel-core/src/config/files/module-types.ts b/packages/babel-core/src/config/files/module-types.ts index 2944a61088d6..12c0b2b7f4f8 100644 --- a/packages/babel-core/src/config/files/module-types.ts +++ b/packages/babel-core/src/config/files/module-types.ts @@ -86,14 +86,35 @@ function loadCtsDefault(filepath: string) { handler = function (m, filename) { // If we want to support `.ts`, `.d.ts` must be handled specially. if (handler && filename.endsWith(ext)) { - // @ts-expect-error Undocumented API - return m._compile( - transformFileSync(filename, { - ...opts, + try { + // @ts-expect-error Undocumented API + return m._compile( + transformFileSync(filename, { + ...opts, + filename, + }).code, filename, - }).code, - filename, - ); + ); + } catch (error) { + if (!hasTsSupport) { + const packageJson = require("@babel/preset-typescript/package.json"); + + if ( + semver.lte( + // eslint-disable-next-line import/no-extraneous-dependencies + packageJson.version, + "7.21.0", + ) && + // ignore the version check if not published + !packageJson.conditions + ) { + console.error( + "`.cts` configuration file failed to load, please try to update `@babel/preset-typescript`.", + ); + } + } + throw error; + } } return require.extensions[".js"](m, filename); }; @@ -134,22 +155,6 @@ async function loadMjsDefault(filepath: string) { function getTSPreset(filepath: string) { try { - const packageJson = require("@babel/preset-typescript/package.json"); - - if ( - semver.lte( - // eslint-disable-next-line import/no-extraneous-dependencies - packageJson.version, - "7.21.0", - ) && - // ignore the version check if not published - !packageJson.conditions - ) { - throw new ConfigError( - "The installed version of `@babel/preset-typescript` is too old to support `.cts` configuration files.", - filepath, - ); - } // eslint-disable-next-line import/no-extraneous-dependencies return require("@babel/preset-typescript"); } catch (error) { From 092f0adb6c77b8116b75cf012884e8978742d838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 30 Mar 2023 15:48:24 +0200 Subject: [PATCH 5/6] Fix after rebase --- packages/babel-preset-typescript/src/index.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/babel-preset-typescript/src/index.ts b/packages/babel-preset-typescript/src/index.ts index 0050ab0ee8b8..d14486cb459c 100644 --- a/packages/babel-preset-typescript/src/index.ts +++ b/packages/babel-preset-typescript/src/index.ts @@ -78,10 +78,7 @@ export default declarePreset((api, opts: Options) => { test: !process.env.BABEL_8_BREAKING ? /\.cts$/ : filename => filename?.endsWith(".cts"), - sourceType: "script", - parserOpts: { - allowImportExportEverywhere: true, - }, + sourceType: "unambiguous", plugins: [ [transformModulesCommonJS, { allowTopLevelThis: true }], [transformTypeScript, pluginOptions(true)], From c6c78ae51c7227eb06249038f1d58c10cc1b9578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 30 Mar 2023 15:50:55 +0200 Subject: [PATCH 6/6] new-version-checklist --- Makefile | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 29ac76c14e37..1c9387c04068 100644 --- a/Makefile +++ b/Makefile @@ -177,15 +177,14 @@ test-test262-update-allowlist: new-version-checklist: - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @echo "!!!!!! !!!!!!" - # @echo "!!!!!! Write any release-blocking message here, and !!!!!!" - # @echo "!!!!!! UNCOMMENT THESE LINES !!!!!!" - # @echo "!!!!!! !!!!!!" - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @exit 1 + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @echo "!!!!!! !!!!!!" + @echo "!!!!!! Update the semver.lte check in module-types.ts !!!!!!" + @echo "!!!!!! !!!!!!" + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @exit 1 new-version: $(MAKE) new-version-checklist