|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + BuildEvent, |
| 11 | + Builder, |
| 12 | + BuilderConfiguration, |
| 13 | + BuilderContext, |
| 14 | +} from '@angular-devkit/architect'; |
| 15 | +import { Path, getSystemPath, normalize, resolve } from '@angular-devkit/core'; |
| 16 | +import { Observable, from } from 'rxjs'; |
| 17 | +import { concatMap } from 'rxjs/operators'; |
| 18 | +import * as webpack from 'webpack'; |
| 19 | +import * as WebpackDevServer from 'webpack-dev-server'; |
| 20 | +import { LoggingCallback, defaultLoggingCb } from '../webpack'; |
| 21 | +import { WebpackDevServerBuilderSchema } from './schema'; |
| 22 | + |
| 23 | + |
| 24 | +export class WebpackDevServerBuilder implements Builder<WebpackDevServerBuilderSchema> { |
| 25 | + |
| 26 | + constructor(public context: BuilderContext) { } |
| 27 | + |
| 28 | + run(builderConfig: BuilderConfiguration<WebpackDevServerBuilderSchema>): Observable<BuildEvent> { |
| 29 | + const configPath = resolve(this.context.workspace.root, |
| 30 | + normalize(builderConfig.options.webpackConfig)); |
| 31 | + |
| 32 | + return this.loadWebpackConfig(getSystemPath(configPath)).pipe( |
| 33 | + concatMap(config => this.runWebpackDevServer(config)), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public loadWebpackConfig(webpackConfigPath: string): Observable<webpack.Configuration> { |
| 38 | + return from(import(webpackConfigPath)); |
| 39 | + } |
| 40 | + |
| 41 | + public runWebpackDevServer( |
| 42 | + webpackConfig: webpack.Configuration, |
| 43 | + devServerCfg?: WebpackDevServer.Configuration, |
| 44 | + loggingCb: LoggingCallback = defaultLoggingCb, |
| 45 | + ): Observable<BuildEvent> { |
| 46 | + return new Observable(obs => { |
| 47 | + const devServerConfig = devServerCfg || webpackConfig.devServer || {}; |
| 48 | + devServerConfig.host = devServerConfig.host || 'localhost'; |
| 49 | + devServerConfig.port = devServerConfig.port || 8080; |
| 50 | + |
| 51 | + if (devServerConfig.stats) { |
| 52 | + webpackConfig.stats = devServerConfig.stats; |
| 53 | + } |
| 54 | + // Disable stats reporting by the devserver, we have our own logger. |
| 55 | + devServerConfig.stats = false; |
| 56 | + |
| 57 | + const webpackCompiler = webpack(webpackConfig); |
| 58 | + const server = new WebpackDevServer(webpackCompiler, devServerConfig); |
| 59 | + |
| 60 | + webpackCompiler.hooks.done.tap('build-webpack', (stats) => { |
| 61 | + // Log stats. |
| 62 | + loggingCb(stats, webpackConfig, this.context.logger); |
| 63 | + |
| 64 | + obs.next({ success: !stats.hasErrors() }); |
| 65 | + }); |
| 66 | + |
| 67 | + server.listen( |
| 68 | + devServerConfig.port, |
| 69 | + devServerConfig.host, |
| 70 | + (err) => { |
| 71 | + if (err) { |
| 72 | + obs.error(err); |
| 73 | + } |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + // Teardown logic. Close the server when unsubscribed from. |
| 78 | + return () => server.close(); |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +export default WebpackDevServerBuilder; |
0 commit comments