Don't use "composite": true in tsc (until it supports cycles) (#13242)

* [ts] Don't use project references

* Commit `tsconfig.json` so it's ready-to-go

* Remove `clean-tsconfig` task

* Fix dts generation

* Add comment

* `yarn`

* Review

* Fix regexp
This commit is contained in:
Nicolò Ribaudo 2021-05-03 20:47:53 +02:00 committed by GitHub
parent 07440424d9
commit 175a51f94e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 145 additions and 132 deletions

3
.gitignore vendored
View File

@ -71,8 +71,7 @@ packages/babel-standalone/babel.min.js
!/packages/babel-eslint-plugin/LICENSE !/packages/babel-eslint-plugin/LICENSE
/.vscode /.vscode
tsconfig.json /dts
tsconfig.tsbuildinfo
/test/runtime-integration/*/output.js /test/runtime-integration/*/output.js
/test/runtime-integration/*/absolute-output.js /test/runtime-integration/*/absolute-output.js

View File

@ -2,7 +2,6 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import { createRequire } from "module"; import { createRequire } from "module";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import plumber from "gulp-plumber"; import plumber from "gulp-plumber";
import through from "through2"; import through from "through2";
import chalk from "chalk"; import chalk from "chalk";
@ -31,7 +30,6 @@ const monorepoRoot = path.dirname(fileURLToPath(import.meta.url));
const defaultPackagesGlob = "./@(codemods|packages|eslint)/*"; const defaultPackagesGlob = "./@(codemods|packages|eslint)/*";
const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`; const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`;
const defaultDtsGlob = `${defaultPackagesGlob}/lib/**/*.d.ts{,.map}`;
const babelStandalonePluginConfigGlob = const babelStandalonePluginConfigGlob =
"./packages/babel-standalone/scripts/pluginConfig.json"; "./packages/babel-standalone/scripts/pluginConfig.json";
@ -61,6 +59,13 @@ function mapSrcToLib(srcPath) {
return parts.join(path.sep); return parts.join(path.sep);
} }
function mapToDts(packageName) {
return packageName.replace(
/(?<=\\|\/|^)(packages|eslint|codemods)(?=\\|\/)/,
"dts"
);
}
function getIndexFromPackage(name) { function getIndexFromPackage(name) {
try { try {
fs.statSync(`./${name}/src/index.ts`); fs.statSync(`./${name}/src/index.ts`);
@ -193,12 +198,6 @@ export const all = {${allList}};`;
.pipe(gulp.dest(dest)); .pipe(gulp.dest(dest));
} }
function unlink() {
return through.obj(function (file, enc, callback) {
fs.unlink(file.path, () => callback());
});
}
function finish(stream) { function finish(stream) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
stream.on("end", resolve); stream.on("end", resolve);
@ -422,19 +421,20 @@ function buildRollupDts(packages) {
const sourcemap = process.env.NODE_ENV === "production"; const sourcemap = process.env.NODE_ENV === "production";
return Promise.all( return Promise.all(
packages.map(async packageName => { packages.map(async packageName => {
const input = `${packageName}/lib/index.d.ts`; const input = `${mapToDts(packageName)}/src/index.d.ts`;
fancyLog(`Bundling '${chalk.cyan(input)}' with rollup ...`); const output = `${packageName}/lib/index.d.ts`;
fancyLog(`Bundling '${chalk.cyan(output)}' with rollup ...`);
const bundle = await rollup({ const bundle = await rollup({
input, input,
plugins: [rollupDts()], 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({ await bundle.write({
file: `${packageName}/lib/index.d.ts`, file: output,
format: "es", format: "es",
sourcemap: sourcemap, sourcemap: sourcemap,
exports: "named", exports: "named",
@ -443,13 +443,9 @@ function buildRollupDts(packages) {
); );
} }
function removeDts(exclude) {
return getFiles(defaultDtsGlob, { exclude }).pipe(unlink());
}
function copyDts(packages) { function copyDts(packages) {
return getFiles(`${defaultPackagesGlob}/src/**/*.d.ts`, { include: 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)); .pipe(gulp.dest(monorepoRoot));
} }
@ -516,7 +512,6 @@ gulp.task(
"bundle-dts", "bundle-dts",
gulp.series("copy-dts", () => buildRollupDts(dtsBundles)) gulp.series("copy-dts", () => buildRollupDts(dtsBundles))
); );
gulp.task("clean-dts", () => removeDts(/* exclude */ dtsBundles));
gulp.task("build-babel", () => buildBabel(/* exclude */ libBundles)); gulp.task("build-babel", () => buildBabel(/* exclude */ libBundles));

View File

@ -76,6 +76,7 @@ flowcheck-ci:
code-quality: tscheck flow lint code-quality: tscheck flow lint
tscheck: generate-tsconfig tscheck: generate-tsconfig
rm -rf dts
$(YARN) tsc -b . $(YARN) tsc -b .
flow: build-flow-typings flow: build-flow-typings
@ -109,13 +110,6 @@ clean: test-clean
rm -rf packages/*/npm-debug* rm -rf packages/*/npm-debug*
rm -rf node_modules/.cache 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: test-clean:
$(foreach source, $(SOURCES), \ $(foreach source, $(SOURCES), \
$(call clean-source-test, $(source))) $(call clean-source-test, $(source)))
@ -182,12 +176,9 @@ prepublish-build: clean-lib clean-runtime-helpers
STRIP_BABEL_8_FLAG=true $(MAKE) prepublish-build-standalone clone-license prepublish-prepare-dts STRIP_BABEL_8_FLAG=true $(MAKE) prepublish-build-standalone clone-license prepublish-prepare-dts
prepublish-prepare-dts: prepublish-prepare-dts:
$(MAKE) clean-tsconfig
$(MAKE) tscheck $(MAKE) tscheck
$(YARN) gulp bundle-dts $(YARN) gulp bundle-dts
$(YARN) gulp clean-dts
$(MAKE) build-typescript-legacy-typings $(MAKE) build-typescript-legacy-typings
$(MAKE) clean-tsconfig
prepublish: prepublish:
$(MAKE) check-yarn-bug-1882 $(MAKE) check-yarn-bug-1882
@ -259,7 +250,7 @@ clean-runtime-helpers:
rm -f packages/babel-runtime-corejs3/helpers/**/*.mjs rm -f packages/babel-runtime-corejs3/helpers/**/*.mjs
rm -rf packages/babel-runtime-corejs2/core-js rm -rf packages/babel-runtime-corejs2/core-js
clean-all: clean-tsconfig clean-all:
rm -rf node_modules rm -rf node_modules
rm -rf package-lock.json rm -rf package-lock.json
rm -rf .changelog rm -rf .changelog

View File

@ -52,7 +52,6 @@
"eslint-plugin-prettier": "^3.1.2", "eslint-plugin-prettier": "^3.1.2",
"fancy-log": "^1.3.3", "fancy-log": "^1.3.3",
"flow-bin": "^0.123.0", "flow-bin": "^0.123.0",
"globby": "^11.0.2",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"gulp-babel": "^8.0.0", "gulp-babel": "^8.0.0",
"gulp-filter": "^5.1.0", "gulp-filter": "^5.1.0",

View File

@ -2,16 +2,5 @@
"extends": "../../tsconfig.base.json", "extends": "../../tsconfig.base.json",
"include": [ "include": [
"./typings" "./typings"
],
"references": [
{
"path": "../babel-code-frame"
},
{
"path": "../babel-helper-fixtures"
},
{
"path": "../babel-helper-validator-identifier"
}
] ]
} }

View File

@ -1,10 +1,6 @@
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
import { createRequire } from "module";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import globby from "globby";
const require = createRequire(import.meta.url);
const root = path.resolve( const root = path.resolve(
path.dirname(fileURLToPath(import.meta.url)), path.dirname(fileURLToPath(import.meta.url)),
@ -18,15 +14,9 @@ function getTsPkgs(subRoot) {
.map(name => ({ .map(name => ({
name: name.replace(/^babel-/, "@babel/"), name: name.replace(/^babel-/, "@babel/"),
dir: path.resolve(root, subRoot, name), dir: path.resolve(root, subRoot, name),
relative: `./${subRoot}/${name}`,
})) }))
.filter(({ dir }) => { .filter(({ dir }) => fs.existsSync(path.join(dir, "src", "index.ts")));
try {
fs.statSync(path.join(dir, "src", "index.ts"));
return true;
} catch {
return false;
}
});
} }
const tsPkgs = [ const tsPkgs = [
@ -35,81 +25,18 @@ const tsPkgs = [
...getTsPkgs("codemods"), ...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( fs.writeFileSync(
path.resolve(root, `tsconfig.json`), path.resolve(root, `tsconfig.json`),
"/* This file is automatically generated by scripts/generators/tconfig.js */\n" +
JSON.stringify( JSON.stringify(
{ {
files: [], extends: "./tsconfig.base.json",
references: tsPkgs.map(({ dir }) => ({ include: tsPkgs.map(({ relative }) => `${relative}/src/**/*.ts`),
path: path.relative(root, dir), compilerOptions: {
})), paths: Object.fromEntries(
tsPkgs.map(({ name, relative }) => [name, [`${relative}/src`]])
),
},
}, },
null, null,
2 2

View File

@ -8,7 +8,7 @@
"declaration": true, "declaration": true,
"declarationMap": true, "declarationMap": true,
"emitDeclarationOnly": true, "emitDeclarationOnly": true,
"composite": true, "declarationDir": "./dts",
"moduleResolution": "node", "moduleResolution": "node",
"esModuleInterop": true, "esModuleInterop": true,
"isolatedModules": true, "isolatedModules": true,

114
tsconfig.json Normal file
View File

@ -0,0 +1,114 @@
/* This file is automatically generated by scripts/generators/tconfig.js */
{
"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"
]
}
}
}

View File

@ -5632,7 +5632,6 @@ __metadata:
eslint-plugin-prettier: ^3.1.2 eslint-plugin-prettier: ^3.1.2
fancy-log: ^1.3.3 fancy-log: ^1.3.3
flow-bin: ^0.123.0 flow-bin: ^0.123.0
globby: ^11.0.2
gulp: ^4.0.2 gulp: ^4.0.2
gulp-babel: ^8.0.0 gulp-babel: ^8.0.0
gulp-filter: ^5.1.0 gulp-filter: ^5.1.0
@ -8804,7 +8803,7 @@ fsevents@^1.2.7:
languageName: node languageName: node
linkType: hard linkType: hard
"globby@npm:^11.0.1, globby@npm:^11.0.2": "globby@npm:^11.0.1":
version: 11.0.2 version: 11.0.2
resolution: "globby@npm:11.0.2" resolution: "globby@npm:11.0.2"
dependencies: dependencies: