From 5345bf6b5694cea3f81a6cb96cfe9d391c143577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 30 Apr 2021 23:55:17 +0200 Subject: [PATCH 1/8] [ts] Don't use project references --- package.json | 1 - packages/babel-parser/tsconfig.json | 11 ---- scripts/generators/tsconfig.js | 92 +++-------------------------- tsconfig.base.json | 2 +- 4 files changed, 10 insertions(+), 96 deletions(-) diff --git a/package.json b/package.json index 450f57af838e..8ab17d2558e1 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "eslint-plugin-prettier": "^3.1.2", "fancy-log": "^1.3.3", "flow-bin": "^0.123.0", - "globby": "^11.0.2", "gulp": "^4.0.2", "gulp-babel": "^8.0.0", "gulp-filter": "^5.1.0", diff --git a/packages/babel-parser/tsconfig.json b/packages/babel-parser/tsconfig.json index d1a436e76874..2fa7583ce92b 100644 --- a/packages/babel-parser/tsconfig.json +++ b/packages/babel-parser/tsconfig.json @@ -2,16 +2,5 @@ "extends": "../../tsconfig.base.json", "include": [ "./typings" - ], - "references": [ - { - "path": "../babel-code-frame" - }, - { - "path": "../babel-helper-fixtures" - }, - { - "path": "../babel-helper-validator-identifier" - } ] } diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index 7b4b4846e4d0..b05836e321e6 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -1,10 +1,6 @@ import path from "path"; import fs from "fs"; -import { createRequire } from "module"; import { fileURLToPath } from "url"; -import globby from "globby"; - -const require = createRequire(import.meta.url); const root = path.resolve( path.dirname(fileURLToPath(import.meta.url)), @@ -18,15 +14,9 @@ function getTsPkgs(subRoot) { .map(name => ({ name: name.replace(/^babel-/, "@babel/"), dir: path.resolve(root, subRoot, name), + relative: `./${subRoot}/${name}`, })) - .filter(({ dir }) => { - try { - fs.statSync(path.join(dir, "src", "index.ts")); - return true; - } catch { - return false; - } - }); + .filter(({ dir }) => fs.existsSync(path.join(dir, "src", "index.ts"))); } const tsPkgs = [ @@ -35,81 +25,17 @@ const tsPkgs = [ ...getTsPkgs("codemods"), ]; -function sourceDeps(packageDir) { - const files = globby.sync(`src/**/*.ts`, { - cwd: packageDir, - onlyFiles: true, - dot: true, - ignore: ["**/node_modules/**"], - }); - const result = new Set(); - for (const file of files) { - const filename = path.join(packageDir, file); - const source = fs.readFileSync(filename, { encoding: "utf8" }); - - for (const [importSource] of source.matchAll( - /(?<=from\s*")@babel\/[^"/]+/g - )) { - result.add(importSource); - } - } - return result; -} - -for (const { dir } of tsPkgs) { - const pkg = require(`${dir}/package.json`); - - try { - const tsconfig = require(`${dir}/tsconfig.json`); - // Don't overwrite manually written configs - if (!tsconfig.generated) continue; - } catch {} - - const deps = new Set([ - ...(pkg.dependencies ? Object.keys(pkg.dependencies) : []), - ...(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []), - // todo(flow->ts): update dependencies in package.json if dependency declared incorrectly - ...sourceDeps(dir), - ]); - - const references = []; - for (const dep of deps) { - if (!dep.startsWith("@babel/")) continue; - for (const { name, dir: depDir } of tsPkgs) { - if (name === dep) { - references.push({ path: path.relative(dir, depDir) }); - break; - } - } - } - - fs.writeFileSync( - path.resolve(dir, "tsconfig.json"), - JSON.stringify( - { - generated: true, - extends: "../../tsconfig.base.json", - compilerOptions: { - outDir: "./lib", - rootDir: "./src", - }, - include: ["./src/**/*"], - references, - }, - null, - 2 - ) - ); -} - fs.writeFileSync( path.resolve(root, `tsconfig.json`), JSON.stringify( { - files: [], - references: tsPkgs.map(({ dir }) => ({ - path: path.relative(root, dir), - })), + extends: "./tsconfig.base.json", + include: tsPkgs.map(({ relative }) => `${relative}/src/**/*.ts`), + compilerOptions: { + paths: Object.fromEntries( + tsPkgs.map(({ name, relative }) => [name, [`${relative}/src`]]) + ), + }, }, null, 2 diff --git a/tsconfig.base.json b/tsconfig.base.json index 9ab38ed2a7a1..065969895b11 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -7,7 +7,7 @@ ], "declaration": true, "declarationMap": true, - "emitDeclarationOnly": true, + "noEmit": true, "composite": true, "moduleResolution": "node", "esModuleInterop": true, From 6cbdc8c7af84f356637f4ff76d37eb63218f13c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 30 Apr 2021 23:57:21 +0200 Subject: [PATCH 2/8] Commit `tsconfig.json` so it's ready-to-go --- tsconfig.json | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tsconfig.json diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000000..a1c4068e1b77 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,113 @@ +{ + "extends": "./tsconfig.base.json", + "include": [ + "./packages/babel-code-frame/src/**/*.ts", + "./packages/babel-core/src/**/*.ts", + "./packages/babel-generator/src/**/*.ts", + "./packages/babel-helper-annotate-as-pure/src/**/*.ts", + "./packages/babel-helper-explode-assignable-expression/src/**/*.ts", + "./packages/babel-helper-fixtures/src/**/*.ts", + "./packages/babel-helper-function-name/src/**/*.ts", + "./packages/babel-helper-get-function-arity/src/**/*.ts", + "./packages/babel-helper-hoist-variables/src/**/*.ts", + "./packages/babel-helper-member-expression-to-functions/src/**/*.ts", + "./packages/babel-helper-module-imports/src/**/*.ts", + "./packages/babel-helper-module-transforms/src/**/*.ts", + "./packages/babel-helper-optimise-call-expression/src/**/*.ts", + "./packages/babel-helper-replace-supers/src/**/*.ts", + "./packages/babel-helper-simple-access/src/**/*.ts", + "./packages/babel-helper-split-export-declaration/src/**/*.ts", + "./packages/babel-helper-transform-fixture-test-runner/src/**/*.ts", + "./packages/babel-helper-validator-identifier/src/**/*.ts", + "./packages/babel-helper-validator-option/src/**/*.ts", + "./packages/babel-highlight/src/**/*.ts", + "./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/**/*.ts", + "./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts", + "./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts", + "./packages/babel-template/src/**/*.ts", + "./packages/babel-traverse/src/**/*.ts", + "./packages/babel-types/src/**/*.ts" + ], + "compilerOptions": { + "paths": { + "@babel/code-frame": [ + "./packages/babel-code-frame/src" + ], + "@babel/core": [ + "./packages/babel-core/src" + ], + "@babel/generator": [ + "./packages/babel-generator/src" + ], + "@babel/helper-annotate-as-pure": [ + "./packages/babel-helper-annotate-as-pure/src" + ], + "@babel/helper-explode-assignable-expression": [ + "./packages/babel-helper-explode-assignable-expression/src" + ], + "@babel/helper-fixtures": [ + "./packages/babel-helper-fixtures/src" + ], + "@babel/helper-function-name": [ + "./packages/babel-helper-function-name/src" + ], + "@babel/helper-get-function-arity": [ + "./packages/babel-helper-get-function-arity/src" + ], + "@babel/helper-hoist-variables": [ + "./packages/babel-helper-hoist-variables/src" + ], + "@babel/helper-member-expression-to-functions": [ + "./packages/babel-helper-member-expression-to-functions/src" + ], + "@babel/helper-module-imports": [ + "./packages/babel-helper-module-imports/src" + ], + "@babel/helper-module-transforms": [ + "./packages/babel-helper-module-transforms/src" + ], + "@babel/helper-optimise-call-expression": [ + "./packages/babel-helper-optimise-call-expression/src" + ], + "@babel/helper-replace-supers": [ + "./packages/babel-helper-replace-supers/src" + ], + "@babel/helper-simple-access": [ + "./packages/babel-helper-simple-access/src" + ], + "@babel/helper-split-export-declaration": [ + "./packages/babel-helper-split-export-declaration/src" + ], + "@babel/helper-transform-fixture-test-runner": [ + "./packages/babel-helper-transform-fixture-test-runner/src" + ], + "@babel/helper-validator-identifier": [ + "./packages/babel-helper-validator-identifier/src" + ], + "@babel/helper-validator-option": [ + "./packages/babel-helper-validator-option/src" + ], + "@babel/highlight": [ + "./packages/babel-highlight/src" + ], + "@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining": [ + "./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src" + ], + "@babel/plugin-proposal-async-do-expressions": [ + "./packages/babel-plugin-proposal-async-do-expressions/src" + ], + "@babel/plugin-syntax-async-do-expressions": [ + "./packages/babel-plugin-syntax-async-do-expressions/src" + ], + "@babel/template": [ + "./packages/babel-template/src" + ], + "@babel/traverse": [ + "./packages/babel-traverse/src" + ], + "@babel/types": [ + "./packages/babel-types/src" + ] + } + } +} \ No newline at end of file From c264b346994894aecf147944a261cc5db37a2763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 1 May 2021 00:01:08 +0200 Subject: [PATCH 3/8] Remove `clean-tsconfig` task --- Makefile | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 93c20f49ee9d..9a535fd5c779 100644 --- a/Makefile +++ b/Makefile @@ -109,13 +109,6 @@ clean: test-clean rm -rf packages/*/npm-debug* rm -rf node_modules/.cache -clean-tsconfig: - rm -f tsconfig.json - git clean packages/*/tsconfig.json -xfq - git clean codemods/*/tsconfig.json -xfq - git clean eslint/*/tsconfig.json -xfq - rm -f */*/tsconfig.tsbuildinfo - test-clean: $(foreach source, $(SOURCES), \ $(call clean-source-test, $(source))) @@ -182,12 +175,10 @@ prepublish-build: clean-lib clean-runtime-helpers STRIP_BABEL_8_FLAG=true $(MAKE) prepublish-build-standalone clone-license prepublish-prepare-dts prepublish-prepare-dts: - $(MAKE) clean-tsconfig $(MAKE) tscheck $(YARN) gulp bundle-dts $(YARN) gulp clean-dts $(MAKE) build-typescript-legacy-typings - $(MAKE) clean-tsconfig prepublish: $(MAKE) check-yarn-bug-1882 @@ -259,7 +250,7 @@ clean-runtime-helpers: rm -f packages/babel-runtime-corejs3/helpers/**/*.mjs rm -rf packages/babel-runtime-corejs2/core-js -clean-all: clean-tsconfig +clean-all: rm -rf node_modules rm -rf package-lock.json rm -rf .changelog From 62eb4748e32bd7fc47e16b4f01eaca6c303d97df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 1 May 2021 00:25:29 +0200 Subject: [PATCH 4/8] Fix dts generation --- .gitignore | 3 +-- Gulpfile.mjs | 34 +++++++++++++--------------------- Makefile | 2 +- tsconfig.base.json | 4 ++-- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index e50ea7f7f6a4..45afd8fef7f0 100644 --- a/.gitignore +++ b/.gitignore @@ -71,8 +71,7 @@ packages/babel-standalone/babel.min.js !/packages/babel-eslint-plugin/LICENSE /.vscode -tsconfig.json -tsconfig.tsbuildinfo +/dts /test/runtime-integration/*/output.js /test/runtime-integration/*/absolute-output.js diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 52fd643f50b3..2dbd729f050d 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -2,7 +2,6 @@ import path from "path"; import fs from "fs"; import { createRequire } from "module"; import { fileURLToPath } from "url"; - import plumber from "gulp-plumber"; import through from "through2"; import chalk from "chalk"; @@ -31,7 +30,6 @@ const monorepoRoot = path.dirname(fileURLToPath(import.meta.url)); const defaultPackagesGlob = "./@(codemods|packages|eslint)/*"; const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`; -const defaultDtsGlob = `${defaultPackagesGlob}/lib/**/*.d.ts{,.map}`; const babelStandalonePluginConfigGlob = "./packages/babel-standalone/scripts/pluginConfig.json"; @@ -61,6 +59,10 @@ function mapSrcToLib(srcPath) { return parts.join(path.sep); } +function mapToDts(packageName) { + return packageName.replace(/\bpackages\b/, "dts"); +} + function getIndexFromPackage(name) { try { fs.statSync(`./${name}/src/index.ts`); @@ -193,12 +195,6 @@ export const all = {${allList}};`; .pipe(gulp.dest(dest)); } -function unlink() { - return through.obj(function (file, enc, callback) { - fs.unlink(file.path, () => callback()); - }); -} - function finish(stream) { return new Promise((resolve, reject) => { stream.on("end", resolve); @@ -422,19 +418,20 @@ function buildRollupDts(packages) { const sourcemap = process.env.NODE_ENV === "production"; return Promise.all( packages.map(async packageName => { - const input = `${packageName}/lib/index.d.ts`; - fancyLog(`Bundling '${chalk.cyan(input)}' with rollup ...`); + const input = `${mapToDts(packageName)}/src/index.d.ts`; + const output = `${packageName}/lib/index.d.ts`; + fancyLog(`Bundling '${chalk.cyan(output)}' with rollup ...`); + const bundle = await rollup({ input, plugins: [rollupDts()], + onwarn(warning, warn) { + if (warning.code !== "CIRCULAR_DEPENDENCY") warn(warning); + }, }); - await finish( - gulp.src(`${packageName}/lib/**/*.d.ts{,.map}`).pipe(unlink()) - ); - await bundle.write({ - file: `${packageName}/lib/index.d.ts`, + file: output, format: "es", sourcemap: sourcemap, exports: "named", @@ -443,13 +440,9 @@ function buildRollupDts(packages) { ); } -function removeDts(exclude) { - return getFiles(defaultDtsGlob, { exclude }).pipe(unlink()); -} - function copyDts(packages) { return getFiles(`${defaultPackagesGlob}/src/**/*.d.ts`, { include: packages }) - .pipe(rename(file => path.resolve(file.base, mapSrcToLib(file.relative)))) + .pipe(rename(file => path.resolve(file.base, mapToDts(file.relative)))) .pipe(gulp.dest(monorepoRoot)); } @@ -516,7 +509,6 @@ gulp.task( "bundle-dts", gulp.series("copy-dts", () => buildRollupDts(dtsBundles)) ); -gulp.task("clean-dts", () => removeDts(/* exclude */ dtsBundles)); gulp.task("build-babel", () => buildBabel(/* exclude */ libBundles)); diff --git a/Makefile b/Makefile index 9a535fd5c779..1e933e4aaf52 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,7 @@ flowcheck-ci: code-quality: tscheck flow lint tscheck: generate-tsconfig + rm -rf dts $(YARN) tsc -b . flow: build-flow-typings @@ -177,7 +178,6 @@ prepublish-build: clean-lib clean-runtime-helpers prepublish-prepare-dts: $(MAKE) tscheck $(YARN) gulp bundle-dts - $(YARN) gulp clean-dts $(MAKE) build-typescript-legacy-typings prepublish: diff --git a/tsconfig.base.json b/tsconfig.base.json index 065969895b11..1e62a17667a5 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -7,8 +7,8 @@ ], "declaration": true, "declarationMap": true, - "noEmit": true, - "composite": true, + "emitDeclarationOnly": true, + "declarationDir": "./dts", "moduleResolution": "node", "esModuleInterop": true, "isolatedModules": true, From 0e1e6b10d49bf80a0336125ba67d0b8109bc3e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 1 May 2021 00:26:45 +0200 Subject: [PATCH 5/8] Add comment --- scripts/generators/tsconfig.js | 25 +++++++++++++------------ tsconfig.json | 1 + 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index b05836e321e6..98b2f94c7ada 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -27,17 +27,18 @@ const tsPkgs = [ fs.writeFileSync( path.resolve(root, `tsconfig.json`), - JSON.stringify( - { - extends: "./tsconfig.base.json", - include: tsPkgs.map(({ relative }) => `${relative}/src/**/*.ts`), - compilerOptions: { - paths: Object.fromEntries( - tsPkgs.map(({ name, relative }) => [name, [`${relative}/src`]]) - ), + "/* This file is automatically generated by scripts/generators/tconfig.js */\n" + + JSON.stringify( + { + extends: "./tsconfig.base.json", + include: tsPkgs.map(({ relative }) => `${relative}/src/**/*.ts`), + compilerOptions: { + paths: Object.fromEntries( + tsPkgs.map(({ name, relative }) => [name, [`${relative}/src`]]) + ), + }, }, - }, - null, - 2 - ) + null, + 2 + ) ); diff --git a/tsconfig.json b/tsconfig.json index a1c4068e1b77..8ac0e03d54b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,4 @@ +/* This file is automatically generated by scripts/generators/tconfig.js */ { "extends": "./tsconfig.base.json", "include": [ From ddef34a34b9c5d019e4264ce2490cfeb7537d8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 1 May 2021 00:55:06 +0200 Subject: [PATCH 6/8] `yarn` --- yarn.lock | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/yarn.lock b/yarn.lock index cfc5106ad69d..bd5f4dce00d9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5643,7 +5643,6 @@ __metadata: eslint-plugin-prettier: ^3.1.2 fancy-log: ^1.3.3 flow-bin: ^0.123.0 - globby: ^11.0.2 gulp: ^4.0.2 gulp-babel: ^8.0.0 gulp-filter: ^5.1.0 @@ -8815,7 +8814,7 @@ fsevents@^1.2.7: languageName: node linkType: hard -"globby@npm:^11.0.1, globby@npm:^11.0.2": +"globby@npm:^11.0.1": version: 11.0.2 resolution: "globby@npm:11.0.2" dependencies: From 092483818e8a8894f25b5fa7823cd4ede0b50939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 1 May 2021 18:45:40 +0200 Subject: [PATCH 7/8] Review --- Gulpfile.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 2dbd729f050d..7240855cfdf7 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -60,7 +60,10 @@ function mapSrcToLib(srcPath) { } function mapToDts(packageName) { - return packageName.replace(/\bpackages\b/, "dts"); + return packageName.replace( + /(?<\\|\/)(packages|eslint|codemods)(?=\\|\/)/, + "dts" + ); } function getIndexFromPackage(name) { From c7c0b2bf55db4b6b35b34a4cf1a67391f393f3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 1 May 2021 23:52:51 +0200 Subject: [PATCH 8/8] Fix regexp --- Gulpfile.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 7240855cfdf7..ba6bde9b1786 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -61,7 +61,7 @@ function mapSrcToLib(srcPath) { function mapToDts(packageName) { return packageName.replace( - /(?<\\|\/)(packages|eslint|codemods)(?=\\|\/)/, + /(?<=\\|\/|^)(packages|eslint|codemods)(?=\\|\/)/, "dts" ); }