Skip to content

Commit

Permalink
update tsconfig generation to reference dependencies used in source c…
Browse files Browse the repository at this point in the history
…ode (#12941)

* update tsconfig generation to reference dependencies used in source code

in some cases there are devDependecnies used for type-checking, this change adds references to them when generationg tsconfig

* use regexp to extract import sources instead of parsing the code

* try reverting globby to previously installed version

* Run `generate-tsconfig` after `bootstrap-only`

* Revert "try reverting globby to previously installed version"

This reverts commit 5648962.

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
zxbodya and nicolo-ribaudo committed Mar 15, 2021
1 parent df51ba0 commit 6e1e003
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -243,8 +243,8 @@ endif
bootstrap-only: clean-all
yarn install

bootstrap: bootstrap-only generate-tsconfig
$(MAKE) build
bootstrap: bootstrap-only
$(MAKE) generate-tsconfig build

clean-lib:
$(foreach source, $(SOURCES), \
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -52,6 +52,7 @@
"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
31 changes: 28 additions & 3 deletions scripts/generators/tsconfig.js
Expand Up @@ -2,6 +2,7 @@ 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);

Expand Down Expand Up @@ -34,6 +35,27 @@ 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`);

Expand All @@ -43,9 +65,12 @@ for (const { dir } of tsPkgs) {
if (!tsconfig.generated) continue;
} catch {}

const deps = [];
if (pkg.dependencies) deps.push(...Object.keys(pkg.dependencies));
if (pkg.peerDependencies) deps.push(...Object.keys(pkg.peerDependencies));
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) {
Expand Down
9 changes: 5 additions & 4 deletions yarn.lock
Expand Up @@ -5499,6 +5499,7 @@ __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
Expand Down Expand Up @@ -8661,17 +8662,17 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"globby@npm:^11.0.1":
version: 11.0.1
resolution: "globby@npm:11.0.1"
"globby@npm:^11.0.1, globby@npm:^11.0.2":
version: 11.0.2
resolution: "globby@npm:11.0.2"
dependencies:
array-union: ^2.1.0
dir-glob: ^3.0.1
fast-glob: ^3.1.1
ignore: ^5.1.4
merge2: ^1.3.0
slash: ^3.0.0
checksum: e7239e9e468c3692aec31dc97b5efc13dd21edf38820baeda98118ade39f475c4ff9e7610859eb4a3c75277ca2616e371265fec3c626aba5db4335bc41c59ac7
checksum: d23f2a6b8897b97fb27422cde243e0fd406ebbaa821929293b27c977d169884f8112494cda4f456a51d0ec1e133e3ac703ec24bfed484e327305ea34a665eb06
languageName: node
linkType: hard

Expand Down

0 comments on commit 6e1e003

Please sign in to comment.