#!/usr/bin/env node import { getPackage, getPackages } from "./utils/getPackages.js"; import { relative } from "path"; import { format } from "oxfmt"; import fs from "fs"; import yargs from "yargs"; import { hideBin } from "yargs/helpers"; const argv = yargs(hideBin(process.argv)).parse(); const { _: packagesToCheck } = argv; /** * This tool generates tsconfig.json and tsconfig.build.json files for all workspaces within the `packages` folder. */ function getRelativePath(a, b) { return relative(a, b).replace(/\\/g, "/"); } async function output(target, content) { const result = await format(target, content); fs.writeFileSync(target, result.code); } /** * Preserve a manually-added `exclude` from an existing generated tsconfig. The generator rewrites * these files from a fixed template, so without this any hand-added `exclude` (e.g. non-TS binaries * under `src`) would be clobbered on every run. */ function readExistingExclude(target) { try { return JSON.parse(fs.readFileSync(target, "utf8")).exclude; } catch { return undefined; } } /** * The key point: both tsconfig.json and tsconfig.build.json should reference source files during * development and build. TypeScript's project references handle building dependencies in the correct * order, so each package always compiles against the source of its dependencies, not their built * output. * * Each workspace dependency gets just two path mappings: `` -> `src` and `/*` -> `src/*`. * Subpath exports don't need their own mappings — the `/*` mapping already resolves them to the * matching source folder. */ (async () => { const workspaces = getPackages({ includes: ["/packages/"] }); for (const wpObject of workspaces) { if (!wpObject.isTs) { continue; } if (packagesToCheck.length) { if (!packagesToCheck.includes(wpObject.packageJson.name)) { continue; } } // Get package dependencies that are registered as workspaces within the repo. const dependencies = Object.keys({ ...wpObject.packageJson.dependencies, ...wpObject.packageJson.devDependencies, ...wpObject.packageJson.peerDependencies }) .filter(getPackage) .filter(name => workspaces.find(pkg => pkg.packageJson.name === name).isTs) .map(name => workspaces.find(pkg => pkg.packageJson.name === name)); // `tsconfig.build.json` compiles only `src`, never `__tests__`. Test-only packages that are // never imported from `src` — and that depend back on the very packages consuming them — // would create a circular project reference that breaks the `composite` build with TS6305, so // they are excluded from the build config. (Currently none; the former `@webiny/testing` // harness has been retired in favor of the `@webiny/api-core-testing` / // `@webiny/api-headless-cms-testing` packages.) const buildExcludedPackages = []; const buildDependencies = dependencies.filter( dep => !buildExcludedPackages.includes(dep.name) ); // Generate `tsconfig.json` const tsconfigJson = { extends: "../../tsconfig.json", include: ["src", "__tests__"], ...(readExistingExclude(wpObject.tsConfigJsonPath) && { exclude: readExistingExclude(wpObject.tsConfigJsonPath) }), references: dependencies.map(dep => ({ path: `${getRelativePath(wpObject.packageFolder, dep.packageFolder)}` })), compilerOptions: { rootDirs: ["./src", "./__tests__"], outDir: "./dist", declarationDir: "./dist", paths: { "~/*": ["./src/*"], "~tests/*": ["./__tests__/*"], ...dependencies.reduce((acc, dep) => { const relPath = getRelativePath(wpObject.packageFolder, dep.packageFolder); acc[`${dep.name}/*`] = [`${relPath}/src/*`]; acc[`${dep.name}`] = [`${relPath}/src`]; return acc; }, {}) } } }; await output(wpObject.tsConfigJsonPath, JSON.stringify(tsconfigJson)); // Generate `tsconfig.build.json` const tsconfigBuildJson = { extends: "../../tsconfig.build.json", include: ["src"], ...(readExistingExclude(wpObject.tsConfigBuildJsonPath) && { exclude: readExistingExclude(wpObject.tsConfigBuildJsonPath) }), references: buildDependencies.map(dep => ({ path: `${getRelativePath( wpObject.packageFolder, dep.packageFolder )}/tsconfig.build.json` })), compilerOptions: { rootDir: "./src", outDir: "./dist", declarationDir: "./dist", paths: { "~/*": ["./src/*"], "~tests/*": ["./__tests__/*"], ...buildDependencies.reduce((acc, dep) => { const relPath = getRelativePath(wpObject.packageFolder, dep.packageFolder); acc[`${dep.name}/*`] = [`${relPath}/src/*`]; acc[`${dep.name}`] = [`${relPath}/src`]; return acc; }, {}) } } }; await output(wpObject.tsConfigBuildJsonPath, JSON.stringify(tsconfigBuildJson)); } })();