From e492b498ff0caa112abce944af190ac9201f88dc Mon Sep 17 00:00:00 2001 From: Fabian Wiles Date: Tue, 8 May 2018 19:42:28 +1200 Subject: [PATCH] feat(@angular-devkit/build-angular): add watch mode to server --- .../build_angular/src/server/index.ts | 9 ++++++++- .../build_angular/src/server/schema.d.ts | 8 ++++++++ .../build_angular/src/server/schema.json | 11 ++++++++++- .../build_angular/test/server/base_spec_large.ts | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/server/index.ts b/packages/angular_devkit/build_angular/src/server/index.ts index 5c56717e91..0e656c4e9c 100644 --- a/packages/angular_devkit/build_angular/src/server/index.ts +++ b/packages/angular_devkit/build_angular/src/server/index.ts @@ -85,7 +85,14 @@ export class ServerBuilder implements Builder { }; try { - webpackCompiler.run(callback); + if (options.watch) { + const watching = webpackCompiler.watch({ poll: options.poll }, callback); + + // Teardown logic. Close the watcher when unsubscribed from. + return () => watching.close(() => { }); + } else { + webpackCompiler.run(callback); + } } catch (err) { if (err) { this.context.logger.error( diff --git a/packages/angular_devkit/build_angular/src/server/schema.d.ts b/packages/angular_devkit/build_angular/src/server/schema.d.ts index 94d92beee1..8e05b9e62c 100644 --- a/packages/angular_devkit/build_angular/src/server/schema.d.ts +++ b/packages/angular_devkit/build_angular/src/server/schema.d.ts @@ -117,6 +117,14 @@ export interface BuildWebpackServerSchema { * Use file name for lazy loaded chunks. */ namedChunks?: boolean; + /** + * Run build when files change. + */ + watch?: boolean; + /** + * Enable and define the file watching poll time period in milliseconds. + */ + poll?: number; } /** diff --git a/packages/angular_devkit/build_angular/src/server/schema.json b/packages/angular_devkit/build_angular/src/server/schema.json index 6d7ec1a56d..e15be2bcc9 100644 --- a/packages/angular_devkit/build_angular/src/server/schema.json +++ b/packages/angular_devkit/build_angular/src/server/schema.json @@ -157,6 +157,15 @@ "type": "string" }, "default": [] + }, + "watch": { + "type": "boolean", + "description": "Run build when files change.", + "default": false + }, + "poll": { + "type": "number", + "description": "Enable and define the file watching poll time period in milliseconds." } }, "additionalProperties": false, @@ -202,6 +211,6 @@ } ] } - + } } \ No newline at end of file diff --git a/packages/angular_devkit/build_angular/test/server/base_spec_large.ts b/packages/angular_devkit/build_angular/test/server/base_spec_large.ts index 05dff2d9e5..2d877ef4cd 100644 --- a/packages/angular_devkit/build_angular/test/server/base_spec_large.ts +++ b/packages/angular_devkit/build_angular/test/server/base_spec_large.ts @@ -45,4 +45,18 @@ describe('Server Builder', () => { }), ).toPromise().then(done, done.fail); }, Timeout.Standard); + + it('runs watch mode', (done) => { + const overrides = { watch: true }; + + runTargetSpec(host, { project: 'app', target: 'server' }, overrides).pipe( + tap((buildEvent) => { + expect(buildEvent.success).toBe(true); + + const fileName = join(outputPath, 'main.js'); + const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName))); + expect(content).toMatch(/AppServerModuleNgFactory/); + }), + ).subscribe(undefined, done.fail, done); + }, Timeout.Standard); });