Turbo + Esbuild + PNPM 管理的TS后端Monorepo中JSON文件变更无法触发Watch构建的配置咨询
Hey there! Let's break down how Turbo's watch mechanism works and fix that JSON file trigger issue you're facing.
First, let's clarify the core logic of Turbo watch:
Turbo's watch mode doesn't directly rely on your esbuild config for file watching. Instead, it uses your monorepo's dependency graph and task input/output definitions to track what files should trigger rebuilds. By default, Turbo watches common source files like .ts, .js, etc., but it doesn't automatically include all .json files unless you explicitly tell it to.
Here's how to properly configure things step by step:
1. Configure turbo.json to include JSON files as task inputs
This is the most critical step. In your root turbo.json, you need to explicitly list .json patterns in the inputs array for your build task. This tells Turbo that these files are part of the build's dependencies, so changes to them should trigger a rebuild.
Example turbo.json snippet:
{ "$schema": "https://turbo.build/schema.json", "tasks": { "build": { "inputs": [ "src/**/*.ts", "src/**/*.json", // Add this line to watch all JSON files in src "package.json", "tsconfig.json", "esbuild.config.ts" // Include your esbuild config if it affects builds too ], "outputs": ["dist/**"] } } }
Adjust the JSON pattern to match your project structure (e.g., config/**/*.json if you have config files in a separate folder).
2. Check individual package package.json source fields
If your workspace packages use a source field (common in projects using tools like tsup or esbuild for packaging), make sure it includes .json files. Turbo uses this field to infer default input files for tasks.
Example package-level package.json:
{ "name": "@your-workspace/api-service", "source": ["src/**/*.ts", "src/**/*.json"], "scripts": { "build": "esbuild src/index.ts --outdir=dist" } }
3. Ensure Esbuild is handling JSON files correctly
While Turbo manages the watch trigger, you need to make sure your esbuild setup actually processes JSON files (so the rebuild has a purpose). In your esbuild.config.ts, confirm you've included .json in resolve extensions if you're importing JSON files:
import esbuild from 'esbuild'; export default esbuild.build({ entryPoints: ['src/index.ts'], outdir: 'dist', bundle: true, platform: 'node', resolveExtensions: ['.ts', '.json'], // Allow esbuild to resolve JSON imports watch: false, // Disable esbuild's own watch—let Turbo handle global watch }).catch(() => process.exit(1));
Important: Don't run esbuild's watch mode alongside Turbo's watch—this will cause conflicts and unexpected behavior.
4. Clean Turbo cache and restart
Sometimes old cache can interfere with watch detection. Run this command to clear Turbo's cache, then restart your watch command:
pnpm turbo clean && pnpm turbo watch build
To recap Turbo's watch flow:
Turbo first builds your monorepo's dependency graph, then for each task, it tracks the inputs you define. When a file in those inputs changes, it runs the task only for the packages that depend on that file (thanks to the dependency graph). It doesn't care about esbuild's internal watch settings—it just needs to know which files are critical to your build output.
备注:内容来源于stack exchange,提问作者Janko Šalković

