Skip to content

Commit

Permalink
cleanup(web): reexport WebpackNxBuildCoordinationPlugin from `@nrwl/w…
Browse files Browse the repository at this point in the history
…ebpack` (nrwl#12315)
  • Loading branch information
Phillip9587 committed Sep 29, 2022
1 parent 776b5a0 commit a592af1
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 86 deletions.
2 changes: 1 addition & 1 deletion docs/shared/incremental-builds.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ If you are only planning to use incremental builds to speed up your CI, then the

## Custom Serve Target

If you are implementing a custom serve command, you can use `WebpackNxBuildCoordinationPlugin` provided by `@nrwl/web`. It's a webpack plugin you can use to coordinate the compiling of the libs and the webpack linking.
If you are implementing a custom serve command, you can use `WebpackNxBuildCoordinationPlugin` provided by `@nrwl/webpack`. It's a webpack plugin you can use to coordinate the compiling of the libs and the webpack linking.

## Using Webpack Module Federation to implement incremental builds

Expand Down
85 changes: 1 addition & 84 deletions packages/web/src/plugins/webpack-nx-build-coordination-plugin.ts
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';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { workspaceLayout } from '@nrwl/devkit';
import { joinPathFragments } from '@nrwl/devkit';
import ignore from 'ignore';
import { readFileSync } from 'fs';
import type { Compiler } from 'webpack';

export class WebpackNxBuildCoordinationPlugin {
private currentlyRunning: 'none' | 'nx-build' | 'webpack-build' = 'none';
Expand All @@ -13,7 +14,7 @@ export class WebpackNxBuildCoordinationPlugin {
this.startWatchingBuildableLibs();
}

apply(compiler) {
apply(compiler: Compiler) {
compiler.hooks.beforeCompile.tapPromise(
'IncrementalDevServerPlugin',
async () => {
Expand Down

0 comments on commit a592af1

Please sign in to comment.