From 88875e6146e290f1e7f67cbd66f7cecae46883be Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Tue, 23 Nov 2021 23:04:58 +0800 Subject: [PATCH] feat(watch): only trigger rebuild when the changed file is actually imported --- README.md | 3 +-- src/esbuild/index.ts | 32 ++++++++++++++++++++++---------- src/index.ts | 6 ++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bc1f636c..1f03b36e 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ tsup src/index.ts src/cli.ts This will output `dist/index.js` and `dist/cli.js`. - ## Documentation For complete usages, please dive into the [docs](https://tsup.egoist.sh). @@ -49,7 +48,7 @@ For complete usages, please dive into the [docs](https://tsup.egoist.sh). ## Project Stats -![Alt](https://repobeats.axiom.co/api/embed/4ef361ec8445b33c2dab451e1d23784015834c72.svg "Repobeats analytics image") +![Alt](https://repobeats.axiom.co/api/embed/4ef361ec8445b33c2dab451e1d23784015834c72.svg 'Repobeats analytics image') ## License diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 310e527f..962b3f3e 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -38,7 +38,13 @@ export async function runEsbuild( format, css, logger, - }: { format: Format; css?: Map; logger: Logger } + buildDependencies, + }: { + format: Format + css?: Map + buildDependencies: Set + logger: Logger + } ) { const pkg = await loadPkg(process.cwd()) const deps = await getDeps(process.cwd()) @@ -174,7 +180,7 @@ export async function runEsbuild( keepNames: options.keepNames, incremental: !!options.watch, pure: typeof options.pure === 'string' ? [options.pure] : options.pure, - metafile: Boolean(options.metafile), + metafile: true, }) } catch (error) { logger.error(format, 'Build failed') @@ -270,13 +276,19 @@ export async function runEsbuild( ) } - if (options.metafile && result?.metafile) { - const outPath = path.resolve(outDir, `metafile-${format}.json`) - await fs.promises.mkdir(path.dirname(outPath), { recursive: true }) - await fs.promises.writeFile( - outPath, - JSON.stringify(result.metafile), - 'utf8' - ) + if (result.metafile) { + for (const file of Object.keys(result.metafile.inputs)) { + buildDependencies.add(file) + } + + if (options.metafile) { + const outPath = path.resolve(outDir, `metafile-${format}.json`) + await fs.promises.mkdir(path.dirname(outPath), { recursive: true }) + await fs.promises.writeFile( + outPath, + JSON.stringify(result.metafile), + 'utf8' + ) + } } } diff --git a/src/index.ts b/src/index.ts index af68344e..7733a944 100644 --- a/src/index.ts +++ b/src/index.ts @@ -138,6 +138,8 @@ export async function build(_options: Options) { if (!options.dts?.only) { let existingOnSuccess: ChildProcess | undefined + /** Files imported by the entry */ + const buildDependencies: Set = new Set() async function killPreviousProcess() { if (existingOnSuccess) { @@ -158,6 +160,7 @@ export async function build(_options: Options) { const buildAll = async () => { const killPromise = killPreviousProcess() + buildDependencies.clear() if (options.clean) { const extraPatterns = Array.isArray(options.clean) @@ -177,6 +180,7 @@ export async function build(_options: Options) { format, css: index === 0 ? css : undefined, logger, + buildDependencies, }) ), ]) @@ -238,6 +242,8 @@ export async function build(_options: Options) { ignored, }) watcher.on('all', async (type, file) => { + if (!buildDependencies.has(file)) return + logger.info('CLI', `Change detected: ${type} ${file}`) debouncedBuildAll() })