@babel/core
Advanced tools
Comparing version
@@ -180,3 +180,5 @@ import buildDebug from "debug"; | ||
case ".mjs": | ||
case ".ts": | ||
case ".cts": | ||
case ".mts": | ||
return readConfigCode(filepath, { | ||
@@ -183,0 +185,0 @@ envName, |
@@ -37,23 +37,32 @@ import { isAsync, waitFor } from "../../gensync-utils/async.js"; | ||
}); | ||
const SUPPORTED_EXTENSIONS = new Set([".js", ".mjs", ".cjs", ".cts"]); | ||
const SUPPORTED_EXTENSIONS = { | ||
".js": "unknown", | ||
".mjs": "esm", | ||
".cjs": "cjs", | ||
".ts": "unknown", | ||
".mts": "esm", | ||
".cts": "cjs" | ||
}; | ||
const asyncModules = new Set(); | ||
export default function* loadCodeDefault(filepath, loader, esmError, tlaError) { | ||
let async; | ||
let ext = path.extname(filepath); | ||
if (!SUPPORTED_EXTENSIONS.has(ext)) ext = ".js"; | ||
const pattern = `${loader} ${ext}`; | ||
const ext = path.extname(filepath); | ||
const isTS = ext === ".ts" || ext === ".cts" || ext === ".mts"; | ||
const type = SUPPORTED_EXTENSIONS[Object.hasOwn(SUPPORTED_EXTENSIONS, ext) ? ext : ".js"]; | ||
const pattern = `${loader} ${type}`; | ||
switch (pattern) { | ||
case "require .cjs": | ||
case "auto .cjs": | ||
{ | ||
case "require cjs": | ||
case "auto cjs": | ||
if (isTS) { | ||
return ensureTsSupport(filepath, ext, () => loadCjsDefault(filepath)); | ||
} else { | ||
return loadCjsDefault(filepath); | ||
} | ||
case "require .cts": | ||
case "auto .cts": | ||
return loadCtsDefault(filepath); | ||
case "auto .js": | ||
case "require .js": | ||
case "require .mjs": | ||
case "auto unknown": | ||
case "require unknown": | ||
case "require esm": | ||
try { | ||
{ | ||
if (isTS) { | ||
return ensureTsSupport(filepath, ext, () => loadCjsDefault(filepath)); | ||
} else { | ||
return loadCjsDefault(filepath); | ||
@@ -71,5 +80,6 @@ } | ||
} | ||
case "auto .mjs": | ||
case "auto esm": | ||
if (async ??= yield* isAsync()) { | ||
return (yield* waitFor(loadMjsFromPath(filepath))).default; | ||
const promise = isTS ? ensureTsSupport(filepath, ext, () => loadMjsFromPath(filepath)) : loadMjsFromPath(filepath); | ||
return (yield* waitFor(promise)).default; | ||
} | ||
@@ -81,45 +91,47 @@ throw new ConfigError(esmError, filepath); | ||
} | ||
function loadCtsDefault(filepath) { | ||
const ext = ".cts"; | ||
const hasTsSupport = !!(require.extensions[".ts"] || require.extensions[".cts"] || require.extensions[".mts"]); | ||
let handler; | ||
if (!hasTsSupport) { | ||
const opts = { | ||
babelrc: false, | ||
configFile: false, | ||
sourceType: "unambiguous", | ||
sourceMaps: "inline", | ||
sourceFileName: path.basename(filepath), | ||
presets: [[getTSPreset(filepath), Object.assign({ | ||
onlyRemoveTypeImports: true, | ||
optimizeConstEnums: true | ||
}, {})]] | ||
}; | ||
handler = function (m, filename) { | ||
if (handler && filename.endsWith(ext)) { | ||
try { | ||
return m._compile(transformFileSync(filename, Object.assign({}, opts, { | ||
filename | ||
})).code, filename); | ||
} catch (error) { | ||
if (!hasTsSupport) { | ||
const packageJson = require("@babel/preset-typescript/package.json"); | ||
if (semver.lt(packageJson.version, "7.21.4")) { | ||
console.error("`.cts` configuration file failed to load, please try to update `@babel/preset-typescript`."); | ||
} | ||
} | ||
throw error; | ||
function ensureTsSupport(filepath, ext, callback) { | ||
if (require.extensions[".ts"] || require.extensions[".cts"] || require.extensions[".mts"]) { | ||
return callback(); | ||
} | ||
if (ext !== ".cts") { | ||
throw new ConfigError(`\ | ||
You are using a ${ext} config file, but Babel only supports transpiling .cts configs. Either: | ||
- Use a .cts config file | ||
- Update to Node.js 23.6.0, which has native TypeScript support | ||
- Install ts-node to transpile ${ext} files on the fly\ | ||
`, filepath); | ||
} | ||
const opts = { | ||
babelrc: false, | ||
configFile: false, | ||
sourceType: "unambiguous", | ||
sourceMaps: "inline", | ||
sourceFileName: path.basename(filepath), | ||
presets: [[getTSPreset(filepath), Object.assign({ | ||
onlyRemoveTypeImports: true, | ||
optimizeConstEnums: true | ||
}, {})]] | ||
}; | ||
let handler = function (m, filename) { | ||
if (handler && filename.endsWith(".cts")) { | ||
try { | ||
return m._compile(transformFileSync(filename, Object.assign({}, opts, { | ||
filename | ||
})).code, filename); | ||
} catch (error) { | ||
const packageJson = require("@babel/preset-typescript/package.json"); | ||
if (semver.lt(packageJson.version, "7.21.4")) { | ||
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; | ||
} | ||
} | ||
return require.extensions[".js"](m, filename); | ||
}; | ||
require.extensions[ext] = handler; | ||
try { | ||
return loadCjsDefault(filepath); | ||
return callback(); | ||
} finally { | ||
if (!hasTsSupport) { | ||
if (require.extensions[ext] === handler) delete require.extensions[ext]; | ||
handler = undefined; | ||
} | ||
if (require.extensions[ext] === handler) delete require.extensions[ext]; | ||
handler = undefined; | ||
} | ||
@@ -126,0 +138,0 @@ } |
@@ -7,3 +7,3 @@ import * as _babel_traverse from '@babel/traverse'; | ||
import { SourceMapConverter } from 'convert-source-map'; | ||
import { ParserOptions, File as File$1, tokTypes } from '@babel/parser'; | ||
import { ParserOptions, ParseResult, tokTypes } from '@babel/parser'; | ||
export { tokTypes } from '@babel/parser'; | ||
@@ -459,3 +459,3 @@ import template from '@babel/template'; | ||
(err: Error, ast: null): void; | ||
(err: null, ast: File$1 | null): void; | ||
(err: null, ast: ParseResult | null): void; | ||
}; | ||
@@ -465,8 +465,8 @@ type Parse = { | ||
(code: string, opts: InputOptions | undefined | null, callback: FileParseCallback): void; | ||
(code: string, opts?: InputOptions | null): File$1 | null; | ||
(code: string, opts?: InputOptions | null): ParseResult | null; | ||
}; | ||
declare const parseRunner: gensync.Gensync<[code: string, opts: ValidatedOptions], File$1, unknown>; | ||
declare const parseRunner: gensync.Gensync<[code: string, opts: ValidatedOptions], ParseResult, unknown>; | ||
declare const parse: Parse; | ||
declare function parseSync(...args: Parameters<typeof parseRunner.sync>): File$1; | ||
declare function parseAsync(...args: Parameters<typeof parseRunner.async>): Promise<File$1>; | ||
declare function parseSync(...args: Parameters<typeof parseRunner.sync>): ParseResult; | ||
declare function parseAsync(...args: Parameters<typeof parseRunner.async>): Promise<ParseResult>; | ||
@@ -473,0 +473,0 @@ declare const version: string; |
; | ||
export const version = "8.0.0-alpha.15"; | ||
export const version = "8.0.0-alpha.16"; | ||
export { default as File } from "./transformation/file/file.js"; | ||
@@ -4,0 +4,0 @@ export { default as buildExternalHelpers } from "./tools/build-external-helpers.js"; |
@@ -11,3 +11,2 @@ import * as helpers from "@babel/helpers"; | ||
import semver from "semver"; | ||
import * as babel7 from "./babel-7-helpers.cjs"; | ||
const errorVisitor = { | ||
@@ -14,0 +13,0 @@ enter(path, state) { |
{ | ||
"name": "@babel/core", | ||
"version": "8.0.0-alpha.15", | ||
"version": "8.0.0-alpha.16", | ||
"description": "Babel compiler core.", | ||
@@ -50,10 +50,11 @@ "main": "./lib/index.js", | ||
"@ampproject/remapping": "^2.2.0", | ||
"@babel/code-frame": "^8.0.0-alpha.15", | ||
"@babel/generator": "^8.0.0-alpha.15", | ||
"@babel/helper-compilation-targets": "^8.0.0-alpha.15", | ||
"@babel/helpers": "^8.0.0-alpha.15", | ||
"@babel/parser": "^8.0.0-alpha.15", | ||
"@babel/template": "^8.0.0-alpha.15", | ||
"@babel/traverse": "^8.0.0-alpha.15", | ||
"@babel/types": "^8.0.0-alpha.15", | ||
"@babel/code-frame": "^8.0.0-alpha.16", | ||
"@babel/generator": "^8.0.0-alpha.16", | ||
"@babel/helper-compilation-targets": "^8.0.0-alpha.16", | ||
"@babel/helpers": "^8.0.0-alpha.16", | ||
"@babel/parser": "^8.0.0-alpha.16", | ||
"@babel/template": "^8.0.0-alpha.16", | ||
"@babel/traverse": "^8.0.0-alpha.16", | ||
"@babel/types": "^8.0.0-alpha.16", | ||
"@types/gensync": "^1.0.0", | ||
"convert-source-map": "^2.0.0", | ||
@@ -66,12 +67,11 @@ "debug": "^4.1.0", | ||
"devDependencies": { | ||
"@babel/helper-transform-fixture-test-runner": "^8.0.0-alpha.15", | ||
"@babel/plugin-syntax-flow": "^8.0.0-alpha.15", | ||
"@babel/plugin-transform-flow-strip-types": "^8.0.0-alpha.15", | ||
"@babel/plugin-transform-modules-commonjs": "^8.0.0-alpha.15", | ||
"@babel/preset-env": "^8.0.0-alpha.15", | ||
"@babel/preset-typescript": "^8.0.0-alpha.15", | ||
"@babel/helper-transform-fixture-test-runner": "^8.0.0-alpha.16", | ||
"@babel/plugin-syntax-flow": "^8.0.0-alpha.16", | ||
"@babel/plugin-transform-flow-strip-types": "^8.0.0-alpha.16", | ||
"@babel/plugin-transform-modules-commonjs": "^8.0.0-alpha.16", | ||
"@babel/preset-env": "^8.0.0-alpha.16", | ||
"@babel/preset-typescript": "^8.0.0-alpha.16", | ||
"@jridgewell/trace-mapping": "^0.3.25", | ||
"@types/convert-source-map": "^2.0.0", | ||
"@types/debug": "^4.1.0", | ||
"@types/gensync": "^1.0.0", | ||
"@types/resolve": "^1.3.2", | ||
@@ -78,0 +78,0 @@ "@types/semver": "^5.4.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
755324
0.24%13
-7.14%6034
0.25%16
6.67%+ Added
Updated