Skip to content

Commit

Permalink
Updates to cts config support (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 13, 2023
1 parent b34add1 commit 72bbe28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
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);
}
}

0 comments on commit 72bbe28

Please sign in to comment.