1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var __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 | }));
|
16 | var __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 | });
|
21 | var __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 | };
|
28 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
29 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
30 | };
|
31 | Object.defineProperty(exports, "__esModule", { value: true });
|
32 | exports.runWebpackDevServer = void 0;
|
33 | const architect_1 = require("@angular-devkit/architect");
|
34 | const path_1 = require("path");
|
35 | const rxjs_1 = require("rxjs");
|
36 | const operators_1 = require("rxjs/operators");
|
37 | const webpack_1 = __importDefault(require("webpack"));
|
38 | const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
|
39 | const utils_1 = require("../utils");
|
40 | function runWebpackDevServer(config, context, options = {}) {
|
41 | const createWebpack = (c) => {
|
42 | if (options.webpackFactory) {
|
43 | const result = options.webpackFactory(c);
|
44 | if ((0, rxjs_1.isObservable)(result)) {
|
45 | return result;
|
46 | }
|
47 | else {
|
48 | return (0, rxjs_1.of)(result);
|
49 | }
|
50 | }
|
51 | else {
|
52 | return (0, rxjs_1.of)((0, webpack_1.default)(c));
|
53 | }
|
54 | };
|
55 | const createWebpackDevServer = (webpack, config) => {
|
56 | if (options.webpackDevServerFactory) {
|
57 | return new options.webpackDevServerFactory(config, webpack);
|
58 | }
|
59 | return new webpack_dev_server_1.default(config, webpack);
|
60 | };
|
61 | const log = options.logging || ((stats, config) => context.logger.info(stats.toString(config.stats)));
|
62 | return createWebpack({ ...config, watch: false }).pipe((0, operators_1.switchMap)((webpackCompiler) => new rxjs_1.Observable((obs) => {
|
63 | var _a;
|
64 | const devServerConfig = options.devServerConfig || config.devServer || {};
|
65 | (_a = devServerConfig.host) !== null && _a !== void 0 ? _a : (devServerConfig.host = 'localhost');
|
66 | let result;
|
67 | webpackCompiler.hooks.done.tap('build-webpack', (stats) => {
|
68 |
|
69 | log(stats, config);
|
70 | obs.next({
|
71 | ...result,
|
72 | emittedFiles: (0, utils_1.getEmittedFiles)(stats.compilation),
|
73 | success: !stats.hasErrors(),
|
74 | outputPath: stats.compilation.outputOptions.path,
|
75 | });
|
76 | });
|
77 | const devServer = createWebpackDevServer(webpackCompiler, devServerConfig);
|
78 | devServer.startCallback(() => {
|
79 | const address = devServer.server.address();
|
80 | if (!address) {
|
81 | obs.error(new Error(`Dev-server address info is not defined.`));
|
82 | return;
|
83 | }
|
84 | result = {
|
85 | success: true,
|
86 | port: typeof address === 'string' ? 0 : address.port,
|
87 | family: typeof address === 'string' ? '' : address.family,
|
88 | address: typeof address === 'string' ? address : address.address,
|
89 | };
|
90 | });
|
91 |
|
92 | return () => {
|
93 | devServer.stopCallback(() => { });
|
94 | webpackCompiler.close(() => { });
|
95 | };
|
96 | })));
|
97 | }
|
98 | exports.runWebpackDevServer = runWebpackDevServer;
|
99 | exports.default = (0, architect_1.createBuilder)((options, context) => {
|
100 | const configPath = (0, path_1.resolve)(context.workspaceRoot, options.webpackConfig);
|
101 | return (0, rxjs_1.from)(Promise.resolve().then(() => __importStar(require(configPath)))).pipe((0, operators_1.switchMap)(({ default: config }) => runWebpackDevServer(config, context)));
|
102 | });
|