Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for import/export in .cts files #15478

Merged
merged 6 commits into from Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this before merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before releasing, right? Could you add a notice in new-version-check in the makefile?

) &&
// 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.`,
);
}

Comment on lines -8 to -14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now sourceType must be one of the two, unless one uses babal-7-beta and writes a new bug to trigger this.

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