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
feat: Support .cts
as configuration file
#15283
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
350ba10
feat
liuxingbaoyu 40b5f2b
yarn dedupe
liuxingbaoyu 25d0fac
fix test
liuxingbaoyu 0aae8bd
review
liuxingbaoyu 8459ad7
yarn.lock
liuxingbaoyu e395515
fix
liuxingbaoyu 8a71976
fix
liuxingbaoyu e90dd47
fix test
liuxingbaoyu f768926
review
liuxingbaoyu b76ec12
support `process.env.BABEL_8_BREAKING && x`
liuxingbaoyu b34add1
Update packages/babel-core/src/config/files/module-types.ts
liuxingbaoyu 72bbe28
Updates to cts config support (#4)
nicolo-ribaudo 9f79e04
lockfile
nicolo-ribaudo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ import semver from "semver"; | |
import { endHiddenCallStack } from "../../errors/rewrite-stack-trace"; | ||
import ConfigError from "../../errors/config-error"; | ||
|
||
import type { InputOptions } from ".."; | ||
import { transformFileSync } from "../../transform-file"; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
let import_: ((specifier: string | URL) => any) | undefined; | ||
|
@@ -23,38 +26,87 @@ export const supportsESM = semver.satisfies( | |
"^12.17 || >=13.2", | ||
); | ||
|
||
export default function* loadCjsOrMjsDefault( | ||
export default function* loadCodeDefault( | ||
filepath: string, | ||
asyncError: string, | ||
// TODO(Babel 8): Remove this | ||
fallbackToTranspiledModule: boolean = false, | ||
): Handler<unknown> { | ||
switch (guessJSModuleType(filepath)) { | ||
case "cjs": | ||
switch (path.extname(filepath)) { | ||
case ".cjs": | ||
return loadCjsDefault(filepath, fallbackToTranspiledModule); | ||
case "unknown": | ||
case ".mjs": | ||
break; | ||
case ".cts": | ||
return loadCtsDefault(filepath); | ||
default: | ||
try { | ||
return loadCjsDefault(filepath, fallbackToTranspiledModule); | ||
} catch (e) { | ||
if (e.code !== "ERR_REQUIRE_ESM") throw e; | ||
} | ||
// fall through | ||
case "mjs": | ||
if (yield* isAsync()) { | ||
return yield* waitFor(loadMjsDefault(filepath)); | ||
} | ||
throw new ConfigError(asyncError, filepath); | ||
} | ||
if (yield* isAsync()) { | ||
return yield* waitFor(loadMjsDefault(filepath)); | ||
} | ||
throw new ConfigError(asyncError, filepath); | ||
} | ||
|
||
function guessJSModuleType(filename: string): "cjs" | "mjs" | "unknown" { | ||
switch (path.extname(filename)) { | ||
case ".cjs": | ||
return "cjs"; | ||
case ".mjs": | ||
return "mjs"; | ||
default: | ||
return "unknown"; | ||
function loadCtsDefault(filepath: string) { | ||
const ext = ".cts"; | ||
const hasTsSupport = !!( | ||
require.extensions[".ts"] || | ||
require.extensions[".cts"] || | ||
require.extensions[".mts"] | ||
); | ||
|
||
let handler: NodeJS.RequireExtensions[""]; | ||
|
||
if (!hasTsSupport) { | ||
const opts: InputOptions = { | ||
babelrc: false, | ||
configFile: false, | ||
sourceType: "script", | ||
sourceMaps: "inline", | ||
presets: [ | ||
[ | ||
getTSPreset(filepath), | ||
{ | ||
disallowAmbiguousJSXLike: true, | ||
allExtensions: true, | ||
onlyRemoveTypeImports: true, | ||
optimizeConstEnums: true, | ||
...(!process.env.BABEL_8_BREAKING && { | ||
allowDeclareFields: true, | ||
}), | ||
}, | ||
], | ||
], | ||
}; | ||
|
||
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, | ||
filename, | ||
}).code, | ||
filename, | ||
); | ||
} | ||
return require.extensions[".js"](m, filename); | ||
}; | ||
require.extensions[ext] = handler; | ||
} | ||
try { | ||
return endHiddenCallStack(require)(filepath); | ||
} finally { | ||
if (!hasTsSupport) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user config could register a new handler for TS extensions. We should also check if |
||
if (require.extensions[ext] === handler) delete require.extensions[ext]; | ||
handler = undefined; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -69,8 +121,7 @@ function loadCjsDefault(filepath: string, fallbackToTranspiledModule: boolean) { | |
async function loadMjsDefault(filepath: string) { | ||
if (!import_) { | ||
throw new ConfigError( | ||
"Internal error: Native ECMAScript modules aren't supported" + | ||
" by this platform.\n", | ||
"Internal error: Native ECMAScript modules aren't supported by this platform.\n", | ||
filepath, | ||
); | ||
} | ||
|
@@ -80,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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { injcectVirtualStackFrame, expectedError } from "./rewrite-stack-trace"; | ||
import { injectVirtualStackFrame, expectedError } from "./rewrite-stack-trace"; | ||
|
||
export default class ConfigError extends Error { | ||
constructor(message: string, filename?: string) { | ||
super(message); | ||
expectedError(this); | ||
if (filename) injcectVirtualStackFrame(this, filename); | ||
if (filename) injectVirtualStackFrame(this, filename); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the plan to eventually support
.ts
and.mts
? Can we throw an error when we findbabel.config.ts
andbabel.config.mts
, instead of silently ignoring them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I want to support this, but it may be difficult, and the current nodejs support for this is not very complete.