UNPKG

4.07 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __importDefault = (this && this.__importDefault) || function (mod) {
10 return (mod && mod.__esModule) ? mod : { "default": mod };
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.runWebpackDevServer = void 0;
14const architect_1 = require("@angular-devkit/architect");
15const path_1 = require("path");
16const rxjs_1 = require("rxjs");
17const operators_1 = require("rxjs/operators");
18const webpack_1 = __importDefault(require("webpack"));
19const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
20const utils_1 = require("../utils");
21function runWebpackDevServer(config, context, options = {}) {
22 var _a;
23 const createWebpack = (c) => {
24 if (options.webpackFactory) {
25 const result = options.webpackFactory(c);
26 if ((0, rxjs_1.isObservable)(result)) {
27 return result;
28 }
29 else {
30 return (0, rxjs_1.of)(result);
31 }
32 }
33 else {
34 return (0, rxjs_1.of)((0, webpack_1.default)(c));
35 }
36 };
37 const createWebpackDevServer = (webpack, config) => {
38 if (options.webpackDevServerFactory) {
39 return new options.webpackDevServerFactory(config, webpack);
40 }
41 return new webpack_dev_server_1.default(config, webpack);
42 };
43 const log = options.logging || ((stats, config) => context.logger.info(stats.toString(config.stats)));
44 const shouldProvideStats = (_a = options.shouldProvideStats) !== null && _a !== void 0 ? _a : true;
45 return createWebpack({ ...config, watch: false }).pipe((0, operators_1.switchMap)((webpackCompiler) => new rxjs_1.Observable((obs) => {
46 var _a;
47 const devServerConfig = options.devServerConfig || config.devServer || {};
48 (_a = devServerConfig.host) !== null && _a !== void 0 ? _a : (devServerConfig.host = 'localhost');
49 let result;
50 const statsOptions = typeof config.stats === 'boolean' ? undefined : config.stats;
51 webpackCompiler.hooks.done.tap('build-webpack', (stats) => {
52 // Log stats.
53 log(stats, config);
54 obs.next({
55 ...result,
56 webpackStats: shouldProvideStats ? stats.toJson(statsOptions) : undefined,
57 emittedFiles: (0, utils_1.getEmittedFiles)(stats.compilation),
58 success: !stats.hasErrors(),
59 outputPath: stats.compilation.outputOptions.path,
60 });
61 });
62 const devServer = createWebpackDevServer(webpackCompiler, devServerConfig);
63 devServer.startCallback((err) => {
64 var _a;
65 if (err) {
66 obs.error(err);
67 return;
68 }
69 const address = (_a = devServer.server) === null || _a === void 0 ? void 0 : _a.address();
70 if (!address) {
71 obs.error(new Error(`Dev-server address info is not defined.`));
72 return;
73 }
74 result = {
75 success: true,
76 port: typeof address === 'string' ? 0 : address.port,
77 family: typeof address === 'string' ? '' : address.family,
78 address: typeof address === 'string' ? address : address.address,
79 };
80 });
81 // Teardown logic. Close the server when unsubscribed from.
82 return () => {
83 devServer.stopCallback(() => { });
84 webpackCompiler.close(() => { });
85 };
86 })));
87}
88exports.runWebpackDevServer = runWebpackDevServer;
89exports.default = (0, architect_1.createBuilder)((options, context) => {
90 const configPath = (0, path_1.resolve)(context.workspaceRoot, options.webpackConfig);
91 return (0, rxjs_1.from)((0, utils_1.getWebpackConfig)(configPath)).pipe((0, operators_1.switchMap)((config) => runWebpackDevServer(config, context)));
92});