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

Updates to cts config support #4

Merged
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
8 changes: 0 additions & 8 deletions packages/babel-core/package.json
Expand Up @@ -77,14 +77,6 @@
"rimraf": "^3.0.0",
"ts-node": "^10.9.1"
},
"peerDependencies": {
"@babel/preset-typescript": "^7.0.0"
},
"peerDependenciesMeta": {
"@babel/preset-typescript": {
"optional": true
}
},
"conditions": {
"BABEL_8_BREAKING": [
null,
Expand Down
40 changes: 30 additions & 10 deletions packages/babel-core/src/config/files/module-types.ts
Expand Up @@ -63,23 +63,14 @@ function loadCtsDefault(filepath: string) {
let handler: NodeJS.RequireExtensions[""];

if (!hasTsSupport) {
const preset = require("@babel/preset-typescript");

if (!preset) {
throw new ConfigError(
"You appear to be using a .cts file as Babel configuration, but the `@babel/preset-typescript` package was not found, please install it!",
filepath,
);
}

const opts: InputOptions = {
babelrc: false,
configFile: false,
sourceType: "script",
sourceMaps: "inline",
presets: [
[
preset,
getTSPreset(filepath),
{
disallowAmbiguousJSXLike: true,
allExtensions: true,
Expand Down Expand Up @@ -140,3 +131,32 @@ async function loadMjsDefault(filepath: string) {
const module = await endHiddenCallStack(import_)(pathToFileURL(filepath));
return module.default;
}

function getTSPreset(filepath: string) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
return require("@babel/preset-typescript");
} catch (error) {
if (error.code !== "MODULE_NOT_FOUND") throw error;

let message =
"You appear to be using a .cts file as Babel configuration, but the `@babel/preset-typescript` package was not found: please install it!";

if (process.versions.pnp) {
// Using Yarn PnP, which doesn't allow requiring packages that are not
// explicitly specified as dependencies.
// TODO(Babel 8): Explicitly add `@babel/preset-typescript` as an
// optional peer dependency of `@babel/core`.
message += `
If you are using Yarn Plug'n'Play, you may also need to add the following configuration to your .yarnrc.yml file:

packageExtensions:
\t"@babel/core@*":
\t\tpeerDependencies:
\t\t\t"@babel/preset-typescript": "*"
`;
}

throw new ConfigError(message, filepath);
}
}