forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup(web): reexport WebpackNxBuildCoordinationPlugin from `@nrwl/w…
…ebpack` (nrwl#12315)
- Loading branch information
1 parent
776b5a0
commit a592af1
Showing
3 changed files
with
4 additions
and
86 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
85 changes: 1 addition & 84 deletions
85
packages/web/src/plugins/webpack-nx-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 |
---|---|---|
@@ -1,84 +1 @@ | ||
import { watch } from 'chokidar'; | ||
import { execSync } from 'child_process'; | ||
import { workspaceLayout } from '@nrwl/devkit'; | ||
import { joinPathFragments } from '@nrwl/devkit'; | ||
import ignore from 'ignore'; | ||
import { readFileSync } from 'fs'; | ||
|
||
export class WebpackNxBuildCoordinationPlugin { | ||
private currentlyRunning: 'none' | 'nx-build' | 'webpack-build' = 'none'; | ||
|
||
constructor(private readonly buildCmd: string) { | ||
this.buildChangedProjects(); | ||
this.startWatchingBuildableLibs(); | ||
} | ||
|
||
apply(compiler) { | ||
compiler.hooks.beforeCompile.tapPromise( | ||
'IncrementalDevServerPlugin', | ||
async () => { | ||
while (this.currentlyRunning === 'nx-build') { | ||
await sleep(50); | ||
} | ||
this.currentlyRunning = 'webpack-build'; | ||
} | ||
); | ||
compiler.hooks.done.tapPromise('IncrementalDevServerPlugin', async () => { | ||
this.currentlyRunning = 'none'; | ||
}); | ||
} | ||
|
||
startWatchingBuildableLibs() { | ||
createFileWatcher(process.cwd(), () => { | ||
this.buildChangedProjects(); | ||
}); | ||
} | ||
|
||
async buildChangedProjects() { | ||
while (this.currentlyRunning === 'webpack-build') { | ||
await sleep(50); | ||
} | ||
this.currentlyRunning = 'nx-build'; | ||
try { | ||
execSync(this.buildCmd, { stdio: [0, 1, 2] }); | ||
// eslint-disable-next-line no-empty | ||
} catch (e) {} | ||
this.currentlyRunning = 'none'; | ||
} | ||
} | ||
|
||
function sleep(time: number) { | ||
return new Promise((resolve) => setTimeout(resolve, time)); | ||
} | ||
|
||
function getIgnoredGlobs(root: string) { | ||
const ig = ignore(); | ||
try { | ||
ig.add(readFileSync(`${root}/.gitignore`, 'utf-8')); | ||
} catch {} | ||
try { | ||
ig.add(readFileSync(`${root}/.nxignore`, 'utf-8')); | ||
} catch {} | ||
return ig; | ||
} | ||
|
||
function createFileWatcher(root: string, changeHandler: () => void) { | ||
const ignoredGlobs = getIgnoredGlobs(root); | ||
const layout = workspaceLayout(); | ||
|
||
const watcher = watch( | ||
[ | ||
joinPathFragments(layout.appsDir, '**'), | ||
joinPathFragments(layout.libsDir, '**'), | ||
], | ||
{ | ||
cwd: root, | ||
ignoreInitial: true, | ||
} | ||
); | ||
watcher.on('all', (_event: string, path: string) => { | ||
if (ignoredGlobs.ignores(path)) return; | ||
changeHandler(); | ||
}); | ||
return { close: () => watcher.close() }; | ||
} | ||
export { WebpackNxBuildCoordinationPlugin } from '@nrwl/webpack/src/plugins/webpack-nx-build-coordination-plugin'; |
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