Skip to content

Commit

Permalink
feat(watch): only trigger rebuild when the changed file is actually i…
Browse files Browse the repository at this point in the history
…mported
  • Loading branch information
egoist committed Nov 23, 2021
1 parent 2d4f6b0 commit 88875e6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -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).
Expand All @@ -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

Expand Down
32 changes: 22 additions & 10 deletions src/esbuild/index.ts
Expand Up @@ -38,7 +38,13 @@ export async function runEsbuild(
format,
css,
logger,
}: { format: Format; css?: Map<string, string>; logger: Logger }
buildDependencies,
}: {
format: Format
css?: Map<string, string>
buildDependencies: Set<string>
logger: Logger
}
) {
const pkg = await loadPkg(process.cwd())
const deps = await getDeps(process.cwd())
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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'
)
}
}
}
6 changes: 6 additions & 0 deletions src/index.ts
Expand Up @@ -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<string> = new Set()

async function killPreviousProcess() {
if (existingOnSuccess) {
Expand All @@ -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)
Expand All @@ -177,6 +180,7 @@ export async function build(_options: Options) {
format,
css: index === 0 ? css : undefined,
logger,
buildDependencies,
})
),
])
Expand Down Expand Up @@ -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()
})
Expand Down

1 comment on commit 88875e6

@vercel
Copy link

@vercel vercel bot commented on 88875e6 Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.