Skip to content
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

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

Merged
merged 8 commits into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -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
37 changes: 16 additions & 21 deletions Gulpfile.mjs
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -61,6 +59,13 @@ function mapSrcToLib(srcPath) {
return parts.join(path.sep);
}

function mapToDts(packageName) {
return packageName.replace(
/(?<=\\|\/|^)(packages|eslint|codemods)(?=\\|\/)/,
"dts"
);
}

function getIndexFromPackage(name) {
try {
fs.statSync(`./${name}/src/index.ts`);
Expand Down Expand Up @@ -193,12 +198,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);
Expand Down Expand Up @@ -422,19 +421,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why there is a warning about it? Can it cause issue when publishing d.ts files?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't cause issues because types are hoisted. It's only a problem when two modules depend on each other and they export/import non-hoistable declarations:

// a.js
import { b } from "b";
export let a = b;
import { a } from "a";
export let b = a;

},
});

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",
Expand All @@ -443,13 +443,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));
}

Expand Down Expand Up @@ -516,7 +512,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));

Expand Down
13 changes: 2 additions & 11 deletions Makefile
Expand Up @@ -76,6 +76,7 @@ flowcheck-ci:
code-quality: tscheck flow lint

tscheck: generate-tsconfig
rm -rf dts
$(YARN) tsc -b .

flow: build-flow-typings
Expand Down Expand Up @@ -109,13 +110,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)))
Expand Down Expand Up @@ -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

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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
11 changes: 0 additions & 11 deletions packages/babel-parser/tsconfig.json
Expand Up @@ -2,16 +2,5 @@
"extends": "../../tsconfig.base.json",
"include": [
"./typings"
],
"references": [
{
"path": "../babel-code-frame"
},
{
"path": "../babel-helper-fixtures"
},
{
"path": "../babel-helper-validator-identifier"
}
]
}
93 changes: 10 additions & 83 deletions 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)),
Expand All @@ -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 = [
Expand All @@ -35,83 +25,20 @@ 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"),
fs.writeFileSync(
path.resolve(root, `tsconfig.json`),
"/* This file is automatically generated by scripts/generators/tconfig.js */\n" +
JSON.stringify(
{
generated: true,
extends: "../../tsconfig.base.json",
extends: "./tsconfig.base.json",
include: tsPkgs.map(({ relative }) => `${relative}/src/**/*.ts`),
compilerOptions: {
outDir: "./lib",
rootDir: "./src",
paths: Object.fromEntries(
tsPkgs.map(({ name, relative }) => [name, [`${relative}/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),
})),
},
null,
2
)
);
2 changes: 1 addition & 1 deletion tsconfig.base.json
Expand Up @@ -8,7 +8,7 @@
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"composite": true,
"declarationDir": "./dts",
"moduleResolution": "node",
"esModuleInterop": true,
"isolatedModules": true,
Expand Down