-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite): support incremental builds with nxViteTsPaths (#23908)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior <!-- This is the behavior we have today --> We do not support incremental builds with vite when using inference plugin ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Support incremental builds with vite when using inference plugin ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
- Loading branch information
Showing
3 changed files
with
221 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/vite/plugins/nx-vite-build-coordination.plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { type Plugin } from 'vite'; | ||
import { BatchFunctionRunner } from 'nx/src/command-line/watch/watch'; | ||
import { exec, type ChildProcess } from 'child_process'; | ||
import { | ||
daemonClient, | ||
type UnregisterCallback, | ||
} from 'nx/src/daemon/client/client'; | ||
import { output } from 'nx/src/utils/output'; | ||
|
||
export interface NxViteBuildCoordinationPluginOptions { | ||
buildCommand: string; | ||
} | ||
export function nxViteBuildCoordinationPlugin( | ||
options: NxViteBuildCoordinationPluginOptions | ||
): Plugin { | ||
let activeBuildProcess: ChildProcess | undefined; | ||
let unregisterFileWatcher: UnregisterCallback | undefined; | ||
|
||
async function buildChangedProjects() { | ||
await new Promise<void>((res) => { | ||
activeBuildProcess = exec(options.buildCommand); | ||
activeBuildProcess.stdout.pipe(process.stdout); | ||
activeBuildProcess.stderr.pipe(process.stderr); | ||
activeBuildProcess.on('exit', () => { | ||
res(); | ||
}); | ||
activeBuildProcess.on('error', () => { | ||
res(); | ||
}); | ||
}); | ||
activeBuildProcess = undefined; | ||
} | ||
|
||
function createFileWatcher() { | ||
const runner = new BatchFunctionRunner(() => buildChangedProjects()); | ||
return daemonClient.registerFileWatcher( | ||
{ watchProjects: 'all' }, | ||
(err, { changedProjects, changedFiles }) => { | ||
if (err === 'closed') { | ||
output.error({ | ||
title: 'Watch connection closed', | ||
bodyLines: [ | ||
'The daemon had closed the connection to this watch process.', | ||
'Please restart your watch command.', | ||
], | ||
}); | ||
process.exit(1); | ||
} | ||
|
||
if (activeBuildProcess) { | ||
activeBuildProcess.kill(2); | ||
activeBuildProcess = undefined; | ||
} | ||
|
||
runner.enqueue(changedProjects, changedFiles); | ||
} | ||
); | ||
} | ||
|
||
return { | ||
name: 'nx-vite-build-coordination-plugin', | ||
async buildStart() { | ||
if (!unregisterFileWatcher) { | ||
await buildChangedProjects(); | ||
unregisterFileWatcher = await createFileWatcher(); | ||
process.on('exit', () => unregisterFileWatcher()); | ||
process.on('SIGINT', () => process.exit()); | ||
} | ||
}, | ||
}; | ||
} |