UNPKG

5.22 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28var __importDefault = (this && this.__importDefault) || function (mod) {
29 return (mod && mod.__esModule) ? mod : { "default": mod };
30};
31Object.defineProperty(exports, "__esModule", { value: true });
32exports.runWebpackDevServer = void 0;
33const architect_1 = require("@angular-devkit/architect");
34const path_1 = require("path");
35const rxjs_1 = require("rxjs");
36const operators_1 = require("rxjs/operators");
37const webpack_1 = __importDefault(require("webpack"));
38const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
39const utils_1 = require("../utils");
40function runWebpackDevServer(config, context, options = {}) {
41 const createWebpack = (c) => {
42 if (options.webpackFactory) {
43 const result = options.webpackFactory(c);
44 if (rxjs_1.isObservable(result)) {
45 return result;
46 }
47 else {
48 return rxjs_1.of(result);
49 }
50 }
51 else {
52 return rxjs_1.of(webpack_1.default(c));
53 }
54 };
55 const createWebpackDevServer = (webpack, config) => {
56 if (options.webpackDevServerFactory) {
57 // webpack-dev-server types currently do not support Webpack 5
58 // eslint-disable-next-line @typescript-eslint/no-explicit-any
59 return new options.webpackDevServerFactory(webpack, config);
60 }
61 // webpack-dev-server types currently do not support Webpack 5
62 // eslint-disable-next-line @typescript-eslint/no-explicit-any
63 return new webpack_dev_server_1.default(webpack, config);
64 };
65 const log = options.logging || ((stats, config) => context.logger.info(stats.toString(config.stats)));
66 // eslint-disable-next-line @typescript-eslint/no-explicit-any
67 const devServerConfig = options.devServerConfig || config.devServer || {};
68 if (devServerConfig.stats) {
69 config.stats = devServerConfig.stats;
70 }
71 // Disable stats reporting by the devserver, we have our own logger.
72 devServerConfig.stats = false;
73 return createWebpack({ ...config, watch: false }).pipe(operators_1.switchMap((webpackCompiler) => new rxjs_1.Observable((obs) => {
74 const server = createWebpackDevServer(webpackCompiler, devServerConfig);
75 let result;
76 webpackCompiler.hooks.done.tap('build-webpack', (stats) => {
77 // Log stats.
78 log(stats, config);
79 obs.next({
80 ...result,
81 emittedFiles: utils_1.getEmittedFiles(stats.compilation),
82 success: !stats.hasErrors(),
83 outputPath: stats.compilation.outputOptions.path,
84 });
85 });
86 server.listen(devServerConfig.port === undefined ? 8080 : devServerConfig.port, devServerConfig.host === undefined ? 'localhost' : devServerConfig.host, function (err) {
87 if (err) {
88 obs.error(err);
89 }
90 else {
91 const address = this.address();
92 if (!address) {
93 obs.error(new Error(`Dev-server address info is not defined.`));
94 return;
95 }
96 result = {
97 success: true,
98 port: typeof address === 'string' ? 0 : address.port,
99 family: typeof address === 'string' ? '' : address.family,
100 address: typeof address === 'string' ? address : address.address,
101 };
102 }
103 });
104 // Teardown logic. Close the server when unsubscribed from.
105 return () => {
106 var _a;
107 server.close();
108 (_a = webpackCompiler.close) === null || _a === void 0 ? void 0 : _a.call(webpackCompiler, () => { });
109 };
110 })));
111}
112exports.runWebpackDevServer = runWebpackDevServer;
113exports.default = architect_1.createBuilder((options, context) => {
114 const configPath = path_1.resolve(context.workspaceRoot, options.webpackConfig);
115 return rxjs_1.from(Promise.resolve().then(() => __importStar(require(configPath)))).pipe(operators_1.switchMap(({ default: config }) => runWebpackDevServer(config, context)));
116});