Skip to content

Add watch mode using chokidar in buildTypes script #46180

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"babel-plugin-transform-inline-environment-variables": "^0.4.4",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"chalk": "^5.4.1",
"chokidar": "^4.0.3",
"compression-webpack-plugin": "^11.1.0",
"concurrently": "^9.1.2",
"cpy-cli": "^5.0.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 49 additions & 10 deletions scripts/buildTypes.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import yargs from 'yargs';
import { $ } from 'execa';
import * as babel from '@babel/core';
import { parse } from 'jsonc-parser';
import chokidar from 'chokidar';

const $$ = $({ stdio: 'inherit' });

Expand Down Expand Up @@ -57,15 +58,10 @@ async function copyDeclarations(sourceDirectory: string, destinationDirectory: s
await fs.cp(fullSourceDirectory, fullDestinationDirectory, {
recursive: true,
filter: async (src) => {
if (src.startsWith('.')) {
// ignore dotfiles
return false;
}
// eslint-disable-next-line curly
if (src.startsWith('.')) return false;
const stats = await fs.stat(src);
if (stats.isDirectory()) {
return true;
}
return src.endsWith('.d.ts');
return stats.isDirectory() || src.endsWith('.d.ts');
},
});
}
Expand All @@ -74,9 +70,10 @@ interface HandlerArgv {
skipTsc: boolean;
copy: string[];
removeCss: boolean;
watch?: boolean;
}

async function main(argv: HandlerArgv) {
async function buildOnce(argv: HandlerArgv) {
const packageRoot = process.cwd();
const tsconfigPath = path.join(packageRoot, 'tsconfig.build.json');
const tsconfigExists = await fs.access(tsconfigPath).then(
Expand Down Expand Up @@ -118,6 +115,41 @@ async function main(argv: HandlerArgv) {
await Promise.all(tsbuildinfo.map(async (file) => fs.rm(file)));
}

async function main(argv: HandlerArgv) {
if (argv.watch) {
// eslint-disable-next-line no-console
console.log('[watch] Starting in watch mode...');

let isBuilding = false;
const triggerBuild = async () => {
// eslint-disable-next-line curly
if (isBuilding) return;
isBuilding = true;
try {
await buildOnce(argv);
// eslint-disable-next-line no-console
console.log('[watch] Build complete');
} catch (err) {
console.error('[watch] Build failed:', err);
} finally {
isBuilding = false;
}
};

await triggerBuild();

chokidar
.watch(['src/**/*.ts', 'src/**/*.d.ts'], { ignoreInitial: true })
.on('all', async (event, tmpPath) => {
// eslint-disable-next-line no-console
console.log(`[watch] ${event}: ${tmpPath}`);
await triggerBuild();
});
} else {
await buildOnce(argv);
}
}

yargs(process.argv.slice(2))
.command<HandlerArgv>(
'$0',
Expand All @@ -139,9 +171,16 @@ yargs(process.argv.slice(2))
type: 'boolean',
default: false,
describe: 'Set to `true` if you want to remove the css imports in the type definitions',
})
.option('watch', {
type: 'boolean',
default: false,
describe: 'Watch mode: rebuild when source files change',
});
},
main,
async (argv) => {
await main(argv);
},
)
.help()
.strict(true)
Expand Down
Loading