Skip to content

Commit

Permalink
Fix support for import/export in .cts files (#15478)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed Mar 30, 2023
1 parent 6e27b5f commit fad4e77
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 44 deletions.
17 changes: 8 additions & 9 deletions Makefile
Expand Up @@ -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
Expand Down
46 changes: 34 additions & 12 deletions packages/babel-core/src/config/files/module-types.ts
Expand Up @@ -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 }),
},
],
],
Expand All @@ -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];
Expand Down
20 changes: 12 additions & 8 deletions packages/babel-core/test/config-ts.js
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
}
Expand Down
@@ -1,2 +1 @@
type config = any;
module.exports = { targets: "node 12.0.0" } as config;
@@ -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;
@@ -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;
7 changes: 0 additions & 7 deletions packages/babel-helper-module-imports/src/is-module.ts
Expand Up @@ -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<t.Program>) {
const { sourceType } = path.node;
if (sourceType !== "module" && sourceType !== "script") {
throw path.buildCodeFrameError(
`Unknown sourceType "${sourceType}", cannot transform.`,
);
}

return path.node.sourceType === "module";
}
1 change: 1 addition & 0 deletions packages/babel-preset-typescript/package.json
Expand Up @@ -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": {
Expand Down
8 changes: 6 additions & 2 deletions 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";

Expand Down Expand Up @@ -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
Expand Down

This file was deleted.

@@ -0,0 +1,3 @@
"use strict";

require("x");
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -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
Expand Down

0 comments on commit fad4e77

Please sign in to comment.