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 diff --git a/packages/babel-core/src/config/files/module-types.ts b/packages/babel-core/src/config/files/module-types.ts index 774c885c00d7..12c0b2b7f4f8 100644 --- a/packages/babel-core/src/config/files/module-types.ts +++ b/packages/babel-core/src/config/files/module-types.ts @@ -66,18 +66,18 @@ function loadCtsDefault(filepath: string) { const opts: InputOptions = { babelrc: false, configFile: false, - sourceType: "script", + sourceType: "unambiguous", 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 }), }, ], ], @@ -86,21 +86,43 @@ 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); }; 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/is-module.ts b/packages/babel-helper-module-imports/src/is-module.ts index bfcd74802cb4..c4d849447f9b 100644 --- a/packages/babel-helper-module-imports/src/is-module.ts +++ b/packages/babel-helper-module-imports/src/is-module.ts @@ -5,12 +5,5 @@ import type * 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"; } 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..d14486cb459c 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"; @@ -77,8 +78,11 @@ export default declarePreset((api, opts: Options) => { test: !process.env.BABEL_8_BREAKING ? /\.cts$/ : filename => filename?.endsWith(".cts"), - sourceType: "script", - plugins: getPlugins(false, true), + sourceType: "unambiguous", + 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