From 33e12045a3dcd81fa5d6100e4fe4177f9d8c853b Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Mon, 18 Jul 2022 23:15:59 +0800 Subject: [PATCH 1/9] ts --- packages/babel-cli/src/babel/dir.ts | 5 +- packages/babel-cli/src/babel/file.ts | 8 ++-- packages/babel-cli/src/babel/options.ts | 34 +++++++++++--- packages/babel-cli/src/babel/util.ts | 25 ++++++---- packages/babel-cli/src/babel/watcher.ts | 29 +++++++----- packages/babel-node/package.json | 1 + packages/babel-node/src/_babel-node.ts | 61 ++++++++++++------------- packages/babel-node/src/babel-node.ts | 9 ++-- scripts/generators/tsconfig.js | 16 +++++-- tsconfig.json | 8 ++++ yarn.lock | 8 ++++ 11 files changed, 131 insertions(+), 73 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.ts b/packages/babel-cli/src/babel/dir.ts index 2585951af31a..909c73643902 100644 --- a/packages/babel-cli/src/babel/dir.ts +++ b/packages/babel-cli/src/babel/dir.ts @@ -1,3 +1,4 @@ +// @ts-expect-error import slash from "slash"; import path from "path"; import fs from "fs"; @@ -129,7 +130,7 @@ export default async function ({ } let compiledFiles = 0; - let startTime = null; + let startTime: [number, number] | null = null; const logSuccess = util.debounce(function () { if (startTime === null) { @@ -178,7 +179,7 @@ export default async function ({ // when we are sure that all the files have been compiled. let processing = 0; const { filenames } = cliOptions; - let getBase; + let getBase: (filename: string) => string | null; if (filenames.length === 1) { // fast path: If there is only one filenames, we know it must be the base const base = filenames[0]; diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index 83f2912d44d3..15f755c8dfbb 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -1,5 +1,7 @@ import convertSourceMap from "convert-source-map"; import { AnyMap, encodedMap } from "@jridgewell/trace-mapping"; +import type { Section } from "@jridgewell/trace-mapping/dist/types/types"; +// @ts-expect-error import slash from "slash"; import path from "path"; import fs from "fs"; @@ -18,7 +20,7 @@ export default async function ({ babelOptions, }: CmdOptions): Promise { function buildResult(fileResults: Array): CompilationOutput { - const mapSections = []; + const mapSections: Section[] = []; let code = ""; let offset = 0; @@ -70,7 +72,7 @@ export default async function ({ } return count; } - function emptyMap() { + function emptyMap(): Section["map"] { return { version: 3, names: [], @@ -129,7 +131,7 @@ export default async function ({ } async function walk(filenames: Array): Promise { - const _filenames = []; + const _filenames: string[] = []; filenames.forEach(function (filename) { if (!fs.existsSync(filename)) return; diff --git a/packages/babel-cli/src/babel/options.ts b/packages/babel-cli/src/babel/options.ts index da35ca377b5a..38d35f9a07fa 100644 --- a/packages/babel-cli/src/babel/options.ts +++ b/packages/babel-cli/src/babel/options.ts @@ -4,6 +4,8 @@ import commander from "commander"; import { version, DEFAULT_EXTENSIONS } from "@babel/core"; import glob from "glob"; +import type { InputOptions } from "@babel/core"; + // Standard Babel input configs. commander.option( "-f, --filename [filename]", @@ -179,17 +181,35 @@ commander.usage("[options] "); commander.action(() => {}); export type CmdOptions = { - babelOptions: any; - cliOptions: any; + babelOptions: InputOptions; + cliOptions: { + filename: string; + filenames: string[]; + extensions: string[]; + keepFileExtension: boolean; + outFileExtension: string; + watch: boolean; + skipInitialBuild: boolean; + outFile: string; + outDir: string; + relative: boolean; + copyFiles: boolean; + copyIgnored: boolean; + includeDotfiles: boolean; + verbose: boolean; + quiet: boolean; + deleteDirOnStart: boolean; + sourceMapTarget: string; + }; }; export default function parseArgv(args: Array): CmdOptions | null { // commander.parse(args); - const errors = []; + const errors: string[] = []; - let filenames = commander.args.reduce(function (globbed, input) { + let filenames = commander.args.reduce(function (globbed: string[], input) { let files = glob.sync(input); if (!files.length) files = [input]; globbed.push(...files); @@ -264,7 +284,7 @@ export default function parseArgv(args: Array): CmdOptions | null { const opts = commander.opts(); - const babelOptions = { + const babelOptions: InputOptions = { presets: opts.presets, plugins: opts.plugins, rootMode: opts.rootMode, @@ -303,7 +323,9 @@ export default function parseArgv(args: Array): CmdOptions | null { // new options for @babel/core, we'll potentially get option validation errors from // @babel/core. To avoid that, we delete undefined options, so @babel/core will only // give the error if users actually pass an unsupported CLI option. - for (const key of Object.keys(babelOptions)) { + for (const key of Object.keys(babelOptions) as Array< + keyof typeof babelOptions + >) { if (babelOptions[key] === undefined) { delete babelOptions[key]; } diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index de2657ebce80..a8899a67624a 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -1,3 +1,4 @@ +// @ts-expect-error import readdirRecursive from "fs-readdir-recursive"; import * as babel from "@babel/core"; import path from "path"; @@ -20,15 +21,19 @@ export function readdir( includeDotfiles: boolean, filter?: ReaddirFilter, ): Array { - return readdirRecursive(dirname, (filename, _index, currentDirectory) => { - const stat = fs.statSync(path.join(currentDirectory, filename)); - - if (stat.isDirectory()) return true; - - return ( - (includeDotfiles || filename[0] !== ".") && (!filter || filter(filename)) - ); - }); + return readdirRecursive( + dirname, + (filename: string, index: number, currentDirectory: string) => { + const stat = fs.statSync(path.join(currentDirectory, filename)); + + if (stat.isDirectory()) return true; + + return ( + (includeDotfiles || filename[0] !== ".") && + (!filter || filter(filename)) + ); + }, + ); } export function readdirForCompilable( @@ -134,7 +139,7 @@ export function withExtension(filename: string, ext: string = ".js") { } export function debounce(fn: () => void, time: number) { - let timer; + let timer: NodeJS.Timeout; function debounced() { clearTimeout(timer); timer = setTimeout(fn, time); diff --git a/packages/babel-cli/src/babel/watcher.ts b/packages/babel-cli/src/babel/watcher.ts index be4ff00b921f..25d1389d7533 100644 --- a/packages/babel-cli/src/babel/watcher.ts +++ b/packages/babel-cli/src/babel/watcher.ts @@ -1,11 +1,12 @@ import { createRequire } from "module"; import path from "path"; +import type { WatchOptions, FSWatcher } from "chokidar"; const fileToDeps = new Map>(); const depToFiles = new Map>(); let isWatchMode = false; -let watcher; +let watcher: FSWatcher; const watchQueue = new Set(); let hasStarted = false; @@ -22,7 +23,7 @@ export function enable({ enableGlobbing }: { enableGlobbing: boolean }) { stabilityThreshold: 50, pollInterval: 10, }, - }); + } as WatchOptions); watcher.on("unlink", unwatchFile); } @@ -94,16 +95,18 @@ export function updateExternalDependencies( Array.from(dependencies, dep => path.resolve(dep)), ); - if (fileToDeps.has(absFilename)) { - for (const dep of fileToDeps.get(absFilename)) { + const deps = fileToDeps.get(absFilename); + if (deps) { + for (const dep of deps) { if (!absDependencies.has(dep)) { removeFileDependency(absFilename, dep); } } } for (const dep of absDependencies) { - if (!depToFiles.has(dep)) { - depToFiles.set(dep, new Set()); + let deps = depToFiles.get(dep); + if (!deps) { + depToFiles.set(dep, (deps = new Set())); if (!hasStarted) { watchQueue.add(dep); @@ -111,16 +114,18 @@ export function updateExternalDependencies( watcher.add(dep); } } - depToFiles.get(dep).add(absFilename); + + deps.add(absFilename); } fileToDeps.set(absFilename, absDependencies); } function removeFileDependency(filename: string, dep: string) { - depToFiles.get(dep).delete(filename); + const deps = depToFiles.get(dep) as Set; + deps.delete(filename); - if (depToFiles.get(dep).size === 0) { + if (deps.size === 0) { depToFiles.delete(dep); if (!hasStarted) { @@ -132,16 +137,16 @@ function removeFileDependency(filename: string, dep: string) { } function unwatchFile(filename: string) { - if (!fileToDeps.has(filename)) return; + const deps = fileToDeps.get(filename); + if (!deps) return; - for (const dep of fileToDeps.get(filename)) { + for (const dep of deps) { removeFileDependency(filename, dep); } fileToDeps.delete(filename); } function requireChokidar(): any { - // @ts-expect-error - TS is not configured to support import.meta. const require = createRequire(import.meta.url); try { diff --git a/packages/babel-node/package.json b/packages/babel-node/package.json index 5af679af3cd8..a55039687db2 100644 --- a/packages/babel-node/package.json +++ b/packages/babel-node/package.json @@ -37,6 +37,7 @@ "@babel/core": "workspace:^", "@babel/helper-fixtures": "workspace:^", "@babel/runtime": "workspace:^", + "@types/v8flags": "^3.1.1", "fs-readdir-recursive": "^1.0.0", "make-dir": "condition:BABEL_8_BREAKING ? : ^2.1.0", "rimraf": "^3.0.0" diff --git a/packages/babel-node/src/_babel-node.ts b/packages/babel-node/src/_babel-node.ts index ce9be2a90012..e1f2c79c5900 100644 --- a/packages/babel-node/src/_babel-node.ts +++ b/packages/babel-node/src/_babel-node.ts @@ -7,15 +7,19 @@ import * as babel from "@babel/core"; import vm from "vm"; import "core-js/stable/index"; import "regenerator-runtime/runtime"; +// @ts-expect-error @babel/register is a js module import register from "@babel/register"; import { fileURLToPath } from "url"; - import { createRequire } from "module"; + +import type { PluginAPI, PluginObject } from "@babel/core"; +import type { REPLEval } from "repl"; + const require = createRequire(import.meta.url); const program = new commander.Command("babel-node"); -function collect(value, previousValue): Array { +function collect(value: unknown, previousValue: string[]): Array { // If the user passed the option with no value, like "babel-node file.js --presets", do nothing. if (typeof value !== "string") return previousValue; @@ -91,7 +95,9 @@ const babelOptions = { babelrc: program.babelrc === true ? undefined : program.babelrc, }; -for (const key of Object.keys(babelOptions)) { +for (const key of Object.keys(babelOptions) as Array< + keyof typeof babelOptions +>) { if (babelOptions[key] === undefined) { delete babelOptions[key]; } @@ -99,7 +105,7 @@ for (const key of Object.keys(babelOptions)) { register(babelOptions); -const replPlugin = ({ types: t }) => ({ +const replPlugin = ({ types: t }: PluginAPI): PluginObject => ({ visitor: { ModuleDeclaration(path) { throw path.buildCodeFrameError("Modules aren't supported in the REPL"); @@ -126,7 +132,7 @@ const replPlugin = ({ types: t }) => ({ }, }); -const _eval = function (code, filename) { +const _eval = function (code: string, filename: string) { code = code.trim(); if (!code) return undefined; @@ -145,26 +151,18 @@ if (program.eval || program.print) { let code = program.eval; if (!code || code === true) code = program.print; - // @ts-expect-error todo(flow->ts) global.__filename = "[eval]"; - // @ts-expect-error todo(flow->ts) global.__dirname = process.cwd(); - // @ts-expect-error todo(flow->ts) const module = new Module(global.__filename); - // @ts-expect-error todo(flow->ts) module.filename = global.__filename; // @ts-expect-error todo(flow->ts) module.paths = Module._nodeModulePaths(global.__dirname); - // @ts-expect-error todo(flow->ts) global.exports = module.exports; - // @ts-expect-error todo(flow->ts) global.module = module; - // @ts-expect-error todo(flow->ts) global.require = module.require.bind(module); - // @ts-expect-error todo(flow->ts) const result = _eval(code, global.__filename); if (program.print) { const output = typeof result === "string" ? result : inspect(result); @@ -184,7 +182,7 @@ if (program.eval || program.print) { } if (arg[0] === "-") { - const parsedOption = program.options.find(option => { + const parsedOption = program.options.find((option: any) => { return option.long === arg || option.short === arg; }); if (parsedOption === undefined) { @@ -230,23 +228,7 @@ function requireArgs() { } } -function replStart() { - const replServer = repl.start({ - prompt: "babel > ", - input: process.stdin, - output: process.stdout, - eval: replEval, - useGlobal: true, - preview: true, - }); - if (process.env.BABEL_8_BREAKING) { - replServer.setupHistory(process.env.NODE_REPL_HISTORY, () => {}); - } else { - replServer.setupHistory?.(process.env.NODE_REPL_HISTORY, () => {}); - } -} - -function replEval(code, context, filename, callback) { +const replEval: REPLEval = function (code, context, filename, callback) { let err; let result; @@ -261,4 +243,21 @@ function replEval(code, context, filename, callback) { } callback(err, result); +}; + +function replStart() { + const replServer = repl.start({ + prompt: "babel > ", + input: process.stdin, + output: process.stdout, + eval: replEval, + useGlobal: true, + preview: true, + }); + const NODE_REPL_HISTORY = process.env.NODE_REPL_HISTORY as string; + if (process.env.BABEL_8_BREAKING) { + replServer.setupHistory(NODE_REPL_HISTORY, () => {}); + } else { + replServer.setupHistory?.(NODE_REPL_HISTORY, () => {}); + } } diff --git a/packages/babel-node/src/babel-node.ts b/packages/babel-node/src/babel-node.ts index 90bf8012bf56..b26af2bace41 100644 --- a/packages/babel-node/src/babel-node.ts +++ b/packages/babel-node/src/babel-node.ts @@ -13,7 +13,7 @@ const args = [ ]; let babelArgs = process.argv.slice(2); -let userArgs; +let userArgs: string[]; // separate node arguments from script arguments const argSeparator = babelArgs.indexOf("--"); @@ -27,7 +27,7 @@ if (argSeparator > -1) { * Also ensure that if the arg contains a value (e.g. --arg=true) * that only the flag is returned. */ -function getNormalizedV8Flag(arg) { +function getNormalizedV8Flag(arg: string) { // v8 uses the "no" prefix to negate boolean flags (e.g. --nolazy), // but they are not listed by v8flags const matches = arg.match(/--(?:no)?(.+)/); @@ -54,7 +54,7 @@ getV8Flags(async function (err, v8Flags) { args.push(flag); args.push(babelArgs[++i]); } else if (aliases.has(flag)) { - args.unshift(aliases.get(flag)); + args.unshift(aliases.get(flag) as string); } else if ( flag === "debug" || // node debug foo.js flag === "inspect" || @@ -73,6 +73,7 @@ getV8Flags(async function (err, v8Flags) { } try { + // @ts-expect-error const { default: kexec } = await import("kexec"); kexec(process.argv[0], args); } catch (err) { @@ -96,7 +97,7 @@ getV8Flags(async function (err, v8Flags) { if (signal) { process.kill(process.pid, signal); } else { - process.exitCode = code; + process.exitCode = code || undefined; } }); }); diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index ad9d0d203b75..a79aeac00e72 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -74,14 +74,20 @@ function getTsPkgs(subRoot) { subExports, }; }) - .filter( - ({ name, relative }) => - // @babel/register is special-cased because its entry point is a js file + .filter(({ name, relative }) => { + const ret = + // They are special-cased because them dose not have a index.ts name === "@babel/register" || + name === "@babel/cli" || + name === "@babel/node" || // @babel/compat-data is used by preset-env name === "@babel/compat-data" || - fs.existsSync(new URL(relative + "/src/index.ts", root)) - ); + fs.existsSync(new URL(relative + "/src/index.ts", root)); + if (!ret) { + console.log(`Skipping ${name} for tsconfig.json`); + } + return ret; + }); } const tsPkgs = [ diff --git a/tsconfig.json b/tsconfig.json index d2eba9b804cf..ec472ac8a040 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ { "extends": "./tsconfig.base.json", "include": [ + "./packages/babel-cli/src/**/*.ts", "./packages/babel-code-frame/src/**/*.ts", "./packages/babel-compat-data/src/**/*.ts", "./packages/babel-core/src/**/*.ts", @@ -36,6 +37,7 @@ "./packages/babel-helper-wrap-function/src/**/*.ts", "./packages/babel-helpers/src/**/*.ts", "./packages/babel-highlight/src/**/*.ts", + "./packages/babel-node/src/**/*.ts", "./packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/**/*.ts", "./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/**/*.ts", "./packages/babel-plugin-external-helpers/src/**/*.ts", @@ -150,6 +152,9 @@ ], "compilerOptions": { "paths": { + "@babel/cli": [ + "./packages/babel-cli/src" + ], "@babel/code-frame": [ "./packages/babel-code-frame/src" ], @@ -267,6 +272,9 @@ "@babel/highlight": [ "./packages/babel-highlight/src" ], + "@babel/node": [ + "./packages/babel-node/src" + ], "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression": [ "./packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src" ], diff --git a/yarn.lock b/yarn.lock index a82f687aa2e3..51ecc1c2dee9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1041,6 +1041,7 @@ __metadata: "@babel/helper-fixtures": "workspace:^" "@babel/register": "workspace:^" "@babel/runtime": "workspace:^" + "@types/v8flags": ^3.1.1 commander: ^4.0.1 core-js: ^3.22.1 fs-readdir-recursive: ^1.0.0 @@ -4528,6 +4529,13 @@ __metadata: languageName: node linkType: hard +"@types/v8flags@npm:^3.1.1": + version: 3.1.1 + resolution: "@types/v8flags@npm:3.1.1" + checksum: 5a38e85cefdf3cc6c38448425ba2c726b62da3ad454209955a780b4eb68b38f98adc95063654668c3d642418e2cf860ad3129d989994aaf7e72fbf29dca94bbc + languageName: node + linkType: hard + "@types/yargs-parser@npm:*": version: 15.0.0 resolution: "@types/yargs-parser@npm:15.0.0" From 927c6bc8dda2554151fe723c9c4adaabffb05874 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Mon, 18 Jul 2022 23:54:32 +0800 Subject: [PATCH 2/9] Update packages/babel-node/src/babel-node.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng --- packages/babel-node/src/babel-node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-node/src/babel-node.ts b/packages/babel-node/src/babel-node.ts index b26af2bace41..b89a8ed51b55 100644 --- a/packages/babel-node/src/babel-node.ts +++ b/packages/babel-node/src/babel-node.ts @@ -97,7 +97,7 @@ getV8Flags(async function (err, v8Flags) { if (signal) { process.kill(process.pid, signal); } else { - process.exitCode = code || undefined; + process.exitCode = code ?? undefined; } }); }); From 52b76374dacc6a2d2473d9d6ba2ccdcc0219f273 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 19 Jul 2022 00:04:22 +0800 Subject: [PATCH 3/9] review --- lib/fs-readdir-recursive.d.ts | 7 +++++++ lib/kexec.d.ts | 4 ++++ lib/slash.d.ts | 4 ++++ packages/babel-cli/src/babel/dir.ts | 1 - packages/babel-cli/src/babel/file.ts | 1 - packages/babel-cli/src/babel/util.ts | 23 +++++++++-------------- packages/babel-cli/src/babel/watcher.ts | 5 +++-- packages/babel-node/src/babel-node.ts | 1 - scripts/generators/tsconfig.js | 3 +++ tsconfig.json | 9 +++++++++ 10 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 lib/fs-readdir-recursive.d.ts create mode 100644 lib/kexec.d.ts create mode 100644 lib/slash.d.ts diff --git a/lib/fs-readdir-recursive.d.ts b/lib/fs-readdir-recursive.d.ts new file mode 100644 index 000000000000..f71f53c2d5d5 --- /dev/null +++ b/lib/fs-readdir-recursive.d.ts @@ -0,0 +1,7 @@ +declare module "fs-readdir-recursive" { + function read( + root: string, + filter?: (filename: string, index: number, dir: string) => boolean + ): string[]; + export = read; +} diff --git a/lib/kexec.d.ts b/lib/kexec.d.ts new file mode 100644 index 000000000000..40214d61bd53 --- /dev/null +++ b/lib/kexec.d.ts @@ -0,0 +1,4 @@ +declare module "kexec" { + function execvp(cmd: string, args: string[]): string; + export = execvp; +} diff --git a/lib/slash.d.ts b/lib/slash.d.ts new file mode 100644 index 000000000000..e2e77b0c0205 --- /dev/null +++ b/lib/slash.d.ts @@ -0,0 +1,4 @@ +declare module "slash" { + function slash(path: string): string; + export = slash; +} diff --git a/packages/babel-cli/src/babel/dir.ts b/packages/babel-cli/src/babel/dir.ts index 909c73643902..971e7a3faad6 100644 --- a/packages/babel-cli/src/babel/dir.ts +++ b/packages/babel-cli/src/babel/dir.ts @@ -1,4 +1,3 @@ -// @ts-expect-error import slash from "slash"; import path from "path"; import fs from "fs"; diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index 15f755c8dfbb..b039dcebef8f 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -1,7 +1,6 @@ import convertSourceMap from "convert-source-map"; import { AnyMap, encodedMap } from "@jridgewell/trace-mapping"; import type { Section } from "@jridgewell/trace-mapping/dist/types/types"; -// @ts-expect-error import slash from "slash"; import path from "path"; import fs from "fs"; diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index a8899a67624a..d75eb51d26ed 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -1,4 +1,3 @@ -// @ts-expect-error import readdirRecursive from "fs-readdir-recursive"; import * as babel from "@babel/core"; import path from "path"; @@ -21,19 +20,15 @@ export function readdir( includeDotfiles: boolean, filter?: ReaddirFilter, ): Array { - return readdirRecursive( - dirname, - (filename: string, index: number, currentDirectory: string) => { - const stat = fs.statSync(path.join(currentDirectory, filename)); - - if (stat.isDirectory()) return true; - - return ( - (includeDotfiles || filename[0] !== ".") && - (!filter || filter(filename)) - ); - }, - ); + return readdirRecursive(dirname, (filename, index, currentDirectory) => { + const stat = fs.statSync(path.join(currentDirectory, filename)); + + if (stat.isDirectory()) return true; + + return ( + (includeDotfiles || filename[0] !== ".") && (!filter || filter(filename)) + ); + }); } export function readdirForCompilable( diff --git a/packages/babel-cli/src/babel/watcher.ts b/packages/babel-cli/src/babel/watcher.ts index 25d1389d7533..f1f9cd643249 100644 --- a/packages/babel-cli/src/babel/watcher.ts +++ b/packages/babel-cli/src/babel/watcher.ts @@ -15,7 +15,7 @@ export function enable({ enableGlobbing }: { enableGlobbing: boolean }) { const { FSWatcher } = requireChokidar(); - watcher = new FSWatcher({ + const options: WatchOptions = { disableGlobbing: !enableGlobbing, persistent: true, ignoreInitial: true, @@ -23,7 +23,8 @@ export function enable({ enableGlobbing }: { enableGlobbing: boolean }) { stabilityThreshold: 50, pollInterval: 10, }, - } as WatchOptions); + }; + watcher = new FSWatcher(options); watcher.on("unlink", unwatchFile); } diff --git a/packages/babel-node/src/babel-node.ts b/packages/babel-node/src/babel-node.ts index b89a8ed51b55..66cada747e4f 100644 --- a/packages/babel-node/src/babel-node.ts +++ b/packages/babel-node/src/babel-node.ts @@ -73,7 +73,6 @@ getV8Flags(async function (err, v8Flags) { } try { - // @ts-expect-error const { default: kexec } = await import("kexec"); kexec(process.argv[0], args); } catch (err) { diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index a79aeac00e72..6023e5090bbd 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -129,6 +129,9 @@ fs.writeFileSync( "to-fast-properties", ["./node_modules/to-fast-properties-BABEL_8_BREAKING-true"], ], + ["slash", ["./lib/slash.d.ts"]], + ["fs-readdir-recursive", ["./lib/fs-readdir-recursive.d.ts"]], + ["kexec", ["./lib/kexec.d.ts"]], ]), }, }, diff --git a/tsconfig.json b/tsconfig.json index ec472ac8a040..64715ab978a2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -718,6 +718,15 @@ ], "to-fast-properties": [ "./node_modules/to-fast-properties-BABEL_8_BREAKING-true" + ], + "slash": [ + "./lib/slash.d.ts" + ], + "fs-readdir-recursive": [ + "./lib/fs-readdir-recursive.d.ts" + ], + "kexec": [ + "./lib/kexec.d.ts" ] } } From 34f2352752663cb3c241c95ebcba639eee782556 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 19 Jul 2022 00:09:29 +0800 Subject: [PATCH 4/9] review --- lib/slash.d.ts | 4 ---- scripts/generators/tsconfig.js | 2 +- tsconfig.json | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 lib/slash.d.ts diff --git a/lib/slash.d.ts b/lib/slash.d.ts deleted file mode 100644 index e2e77b0c0205..000000000000 --- a/lib/slash.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -declare module "slash" { - function slash(path: string): string; - export = slash; -} diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index 6023e5090bbd..20324a450925 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -129,7 +129,7 @@ fs.writeFileSync( "to-fast-properties", ["./node_modules/to-fast-properties-BABEL_8_BREAKING-true"], ], - ["slash", ["./lib/slash.d.ts"]], + ["slash", ["./node_modules/slash-BABEL_8_BREAKING-true"]], ["fs-readdir-recursive", ["./lib/fs-readdir-recursive.d.ts"]], ["kexec", ["./lib/kexec.d.ts"]], ]), diff --git a/tsconfig.json b/tsconfig.json index 64715ab978a2..c23494bc71ec 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -720,7 +720,7 @@ "./node_modules/to-fast-properties-BABEL_8_BREAKING-true" ], "slash": [ - "./lib/slash.d.ts" + "./node_modules/slash-BABEL_8_BREAKING-true" ], "fs-readdir-recursive": [ "./lib/fs-readdir-recursive.d.ts" From 90d795d915c301d2c7efba404e0ec98da9a5c2c4 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 19 Jul 2022 00:11:06 +0800 Subject: [PATCH 5/9] fix --- lib/kexec.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kexec.d.ts b/lib/kexec.d.ts index 40214d61bd53..6ab60516ce7f 100644 --- a/lib/kexec.d.ts +++ b/lib/kexec.d.ts @@ -1,4 +1,4 @@ declare module "kexec" { - function execvp(cmd: string, args: string[]): string; + function execvp(cmd: string, args?: string[]): string; export = execvp; } From 017b6dad8d8110f38d8f0c2c30619e402c1de490 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:13:30 +0800 Subject: [PATCH 6/9] Update packages/babel-node/src/_babel-node.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- packages/babel-node/src/_babel-node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-node/src/_babel-node.ts b/packages/babel-node/src/_babel-node.ts index e1f2c79c5900..2c3302b5400e 100644 --- a/packages/babel-node/src/_babel-node.ts +++ b/packages/babel-node/src/_babel-node.ts @@ -7,7 +7,7 @@ import * as babel from "@babel/core"; import vm from "vm"; import "core-js/stable/index"; import "regenerator-runtime/runtime"; -// @ts-expect-error @babel/register is a js module +// @ts-expect-error @babel/register is a CommonJS module import register from "@babel/register"; import { fileURLToPath } from "url"; import { createRequire } from "module"; From 938f92292281ee6270a677be3ef011c90c4be4a2 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:29:07 +0800 Subject: [PATCH 7/9] review --- packages/babel-cli/src/babel/file.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index b039dcebef8f..9e604b6c0912 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -1,6 +1,5 @@ import convertSourceMap from "convert-source-map"; import { AnyMap, encodedMap } from "@jridgewell/trace-mapping"; -import type { Section } from "@jridgewell/trace-mapping/dist/types/types"; import slash from "slash"; import path from "path"; import fs from "fs"; @@ -9,9 +8,17 @@ import * as util from "./util"; import type { CmdOptions } from "./options"; import * as watcher from "./watcher"; +import type { + SectionedSourceMap, + SourceMapInput, + TraceMap, +} from "@jridgewell/trace-mapping"; + +type Section = SectionedSourceMap["sections"][0]; + type CompilationOutput = { code: string; - map: any; + map: SourceMapInput; }; export default async function ({ @@ -90,7 +97,10 @@ export default async function ({ if (babelOptions.sourceMaps && babelOptions.sourceMaps !== "inline") { const mapLoc = cliOptions.outFile + ".map"; result.code = util.addSourceMappingUrl(result.code, mapLoc); - fs.writeFileSync(mapLoc, JSON.stringify(encodedMap(result.map))); + fs.writeFileSync( + mapLoc, + JSON.stringify(encodedMap(result.map as TraceMap)), + ); } fs.writeFileSync(cliOptions.outFile, result.code); From ecf822343cf9b8fd94812b46a61eb90e13d5b1b0 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:32:19 +0800 Subject: [PATCH 8/9] Apply suggestions from code review Co-authored-by: Justin Ridgewell --- packages/babel-cli/src/babel/file.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index 9e604b6c0912..37c3605027ff 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -26,7 +26,7 @@ export default async function ({ babelOptions, }: CmdOptions): Promise { function buildResult(fileResults: Array): CompilationOutput { - const mapSections: Section[] = []; + const mapSections: SectionedSourceMap["sections"] = []; let code = ""; let offset = 0; @@ -78,7 +78,7 @@ export default async function ({ } return count; } - function emptyMap(): Section["map"] { + function emptyMap(): DecodedSourceMap { return { version: 3, names: [], From 1f62b3b526e7a8292cb6c221b0fe8a8194fd6f37 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:33:33 +0800 Subject: [PATCH 9/9] review --- packages/babel-cli/src/babel/file.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index 37c3605027ff..63222479cfbd 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -12,10 +12,9 @@ import type { SectionedSourceMap, SourceMapInput, TraceMap, + DecodedSourceMap, } from "@jridgewell/trace-mapping"; -type Section = SectionedSourceMap["sections"][0]; - type CompilationOutput = { code: string; map: SourceMapInput;