|
| 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 | +import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect/src/index2'; |
| 9 | +import { getSystemPath, json, normalize, resolve } from '@angular-devkit/core'; |
| 10 | +import * as net from 'net'; |
| 11 | +import { Observable, from, of } from 'rxjs'; |
| 12 | +import { switchMap } from 'rxjs/operators'; |
| 13 | +import * as webpack from 'webpack'; |
| 14 | +import * as WebpackDevServer from 'webpack-dev-server'; |
| 15 | +import { ArchitectPlugin } from '../plugins/architect'; |
| 16 | +import { WebpackFactory, WebpackLoggingCallback } from '../webpack/index2'; |
| 17 | +import { Schema as WebpackDevServerBuilderSchema } from './schema'; |
| 18 | + |
| 19 | +const webpackMerge = require('webpack-merge'); |
| 20 | + |
| 21 | + |
| 22 | +export type DevServerBuildResult = BuilderOutput & { |
| 23 | + port: number; |
| 24 | + family: string; |
| 25 | + address: string; |
| 26 | +}; |
| 27 | + |
| 28 | +export function runWebpackDevServer( |
| 29 | + config: webpack.Configuration, |
| 30 | + context: BuilderContext, |
| 31 | + options: { |
| 32 | + devServerConfig?: WebpackDevServer.Configuration, |
| 33 | + logging?: WebpackLoggingCallback, |
| 34 | + webpackFactory?: WebpackFactory, |
| 35 | + } = {}, |
| 36 | +): Observable<BuilderOutput> { |
| 37 | + const createWebpack = options.webpackFactory || (config => of(webpack(config))); |
| 38 | + const log: WebpackLoggingCallback = options.logging |
| 39 | + || ((stats, config) => context.logger.info(stats.toString(config.stats))); |
| 40 | + |
| 41 | + config = webpackMerge(config, { |
| 42 | + plugins: [ |
| 43 | + new ArchitectPlugin(context), |
| 44 | + ], |
| 45 | + }); |
| 46 | + |
| 47 | + const devServerConfig = options.devServerConfig || config.devServer || {}; |
| 48 | + if (devServerConfig.stats) { |
| 49 | + config.stats = devServerConfig.stats as webpack.Stats.ToStringOptionsObject; |
| 50 | + } |
| 51 | + // Disable stats reporting by the devserver, we have our own logger. |
| 52 | + devServerConfig.stats = false; |
| 53 | + |
| 54 | + return createWebpack(config).pipe( |
| 55 | + switchMap(webpackCompiler => new Observable<BuilderOutput>(obs => { |
| 56 | + const server = new WebpackDevServer(webpackCompiler, devServerConfig); |
| 57 | + let result: DevServerBuildResult; |
| 58 | + |
| 59 | + webpackCompiler.hooks.done.tap('build-webpack', (stats) => { |
| 60 | + // Log stats. |
| 61 | + log(stats, config); |
| 62 | + |
| 63 | + obs.next({ |
| 64 | + ...result, |
| 65 | + success: !stats.hasErrors(), |
| 66 | + } as DevServerBuildResult); |
| 67 | + }); |
| 68 | + |
| 69 | + server.listen( |
| 70 | + devServerConfig.port === undefined ? 8080 : devServerConfig.port, |
| 71 | + devServerConfig.host === undefined ? 'localhost' : devServerConfig.host, |
| 72 | + function (this: net.Server, err) { |
| 73 | + if (err) { |
| 74 | + obs.error(err); |
| 75 | + } else { |
| 76 | + result = { |
| 77 | + success: true, |
| 78 | + port: this.address().port, |
| 79 | + family: this.address().family, |
| 80 | + address: this.address().address, |
| 81 | + }; |
| 82 | + } |
| 83 | + }, |
| 84 | + ); |
| 85 | + |
| 86 | + // Teardown logic. Close the server when unsubscribed from. |
| 87 | + return () => server.close(); |
| 88 | + })), |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +export default createBuilder< |
| 94 | + json.JsonObject & WebpackDevServerBuilderSchema, DevServerBuildResult |
| 95 | +>((options, context) => { |
| 96 | + const configPath = resolve(normalize(context.workspaceRoot), normalize(options.webpackConfig)); |
| 97 | + |
| 98 | + return from(import(getSystemPath(configPath))).pipe( |
| 99 | + switchMap((config: webpack.Configuration) => runWebpackDevServer(config, context)), |
| 100 | + ); |
| 101 | +}); |
0 commit comments