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 | */
|
---|
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 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.buildServePath = exports.getDevServerConfig = void 0;
|
---|
30 | const core_1 = require("@angular-devkit/core");
|
---|
31 | const fs_1 = require("fs");
|
---|
32 | const path_1 = require("path");
|
---|
33 | const url = __importStar(require("url"));
|
---|
34 | const utils_1 = require("../../utils");
|
---|
35 | const webpack_browser_config_1 = require("../../utils/webpack-browser-config");
|
---|
36 | const hmr_loader_1 = require("../plugins/hmr/hmr-loader");
|
---|
37 | const helpers_1 = require("../utils/helpers");
|
---|
38 | function getDevServerConfig(wco) {
|
---|
39 | var _a;
|
---|
40 | const { buildOptions: { optimization, host, port, index, headers, poll, ssl, hmr, main, disableHostCheck, liveReload, allowedHosts, watch, proxyConfig, }, logger, root, } = wco;
|
---|
41 | const servePath = buildServePath(wco.buildOptions, logger);
|
---|
42 | const { styles: stylesOptimization, scripts: scriptsOptimization } = utils_1.normalizeOptimization(optimization);
|
---|
43 | const extraPlugins = [];
|
---|
44 | // Resolve public host and client address.
|
---|
45 | let publicHost = wco.buildOptions.publicHost;
|
---|
46 | if (publicHost) {
|
---|
47 | if (!/^\w+:\/\//.test(publicHost)) {
|
---|
48 | publicHost = `${ssl ? 'https' : 'http'}://${publicHost}`;
|
---|
49 | }
|
---|
50 | const parsedHost = url.parse(publicHost);
|
---|
51 | publicHost = (_a = parsedHost.host) !== null && _a !== void 0 ? _a : undefined;
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | publicHost = '0.0.0.0:0';
|
---|
55 | }
|
---|
56 | if (!watch) {
|
---|
57 | // There's no option to turn off file watching in webpack-dev-server, but
|
---|
58 | // we can override the file watcher instead.
|
---|
59 | extraPlugins.push({
|
---|
60 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
61 | apply: (compiler) => {
|
---|
62 | compiler.hooks.afterEnvironment.tap('angular-cli', () => {
|
---|
63 | // eslint-disable-next-line @typescript-eslint/no-empty-function
|
---|
64 | compiler.watchFileSystem = { watch: () => { } };
|
---|
65 | });
|
---|
66 | },
|
---|
67 | });
|
---|
68 | }
|
---|
69 | const extraRules = [];
|
---|
70 | if (hmr) {
|
---|
71 | extraRules.push({
|
---|
72 | loader: hmr_loader_1.HmrLoader,
|
---|
73 | include: [main].map((p) => path_1.resolve(wco.root, p)),
|
---|
74 | });
|
---|
75 | }
|
---|
76 | return {
|
---|
77 | plugins: extraPlugins,
|
---|
78 | module: {
|
---|
79 | rules: extraRules,
|
---|
80 | },
|
---|
81 | devServer: {
|
---|
82 | host,
|
---|
83 | port,
|
---|
84 | headers: {
|
---|
85 | 'Access-Control-Allow-Origin': '*',
|
---|
86 | ...headers,
|
---|
87 | },
|
---|
88 | historyApiFallback: !!index && {
|
---|
89 | index: `${servePath}/${webpack_browser_config_1.getIndexOutputFile(index)}`,
|
---|
90 | disableDotRule: true,
|
---|
91 | htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
|
---|
92 | rewrites: [
|
---|
93 | {
|
---|
94 | from: new RegExp(`^(?!${servePath})/.*`),
|
---|
95 | to: (context) => url.format(context.parsedUrl),
|
---|
96 | },
|
---|
97 | ],
|
---|
98 | },
|
---|
99 | sockPath: path_1.posix.join(servePath, 'sockjs-node'),
|
---|
100 | stats: false,
|
---|
101 | compress: false,
|
---|
102 | watchOptions: helpers_1.getWatchOptions(poll),
|
---|
103 | https: getSslConfig(root, wco.buildOptions),
|
---|
104 | overlay: {
|
---|
105 | errors: !(stylesOptimization.minify || scriptsOptimization),
|
---|
106 | warnings: false,
|
---|
107 | },
|
---|
108 | public: publicHost,
|
---|
109 | allowedHosts,
|
---|
110 | disableHostCheck,
|
---|
111 | // This should always be true, but at the moment this breaks 'SuppressExtractedTextChunksWebpackPlugin'
|
---|
112 | // because it will include addition JS in the styles.js.
|
---|
113 | inline: hmr,
|
---|
114 | publicPath: servePath,
|
---|
115 | liveReload,
|
---|
116 | injectClient: liveReload,
|
---|
117 | hotOnly: hmr && !liveReload,
|
---|
118 | hot: hmr,
|
---|
119 | proxy: addProxyConfig(root, proxyConfig),
|
---|
120 | contentBase: false,
|
---|
121 | logLevel: 'error',
|
---|
122 | },
|
---|
123 | };
|
---|
124 | }
|
---|
125 | exports.getDevServerConfig = getDevServerConfig;
|
---|
126 | /**
|
---|
127 | * Resolve and build a URL _path_ that will be the root of the server. This resolved base href and
|
---|
128 | * deploy URL from the browser options and returns a path from the root.
|
---|
129 | */
|
---|
130 | function buildServePath(options, logger) {
|
---|
131 | let servePath = options.servePath;
|
---|
132 | if (servePath === undefined) {
|
---|
133 | const defaultPath = findDefaultServePath(options.baseHref, options.deployUrl);
|
---|
134 | if (defaultPath == null) {
|
---|
135 | logger.warn(core_1.tags.oneLine `
|
---|
136 | Warning: --deploy-url and/or --base-href contain unsupported values for ng serve. Default
|
---|
137 | serve path of '/' used. Use --serve-path to override.
|
---|
138 | `);
|
---|
139 | }
|
---|
140 | servePath = defaultPath || '';
|
---|
141 | }
|
---|
142 | if (servePath.endsWith('/')) {
|
---|
143 | servePath = servePath.substr(0, servePath.length - 1);
|
---|
144 | }
|
---|
145 | if (!servePath.startsWith('/')) {
|
---|
146 | servePath = `/${servePath}`;
|
---|
147 | }
|
---|
148 | return servePath;
|
---|
149 | }
|
---|
150 | exports.buildServePath = buildServePath;
|
---|
151 | /**
|
---|
152 | * Private method to enhance a webpack config with SSL configuration.
|
---|
153 | * @private
|
---|
154 | */
|
---|
155 | function getSslConfig(root, options) {
|
---|
156 | const { ssl, sslCert, sslKey } = options;
|
---|
157 | if (ssl && sslCert && sslKey) {
|
---|
158 | return {
|
---|
159 | key: fs_1.readFileSync(path_1.resolve(root, sslKey), 'utf-8'),
|
---|
160 | cert: fs_1.readFileSync(path_1.resolve(root, sslCert), 'utf-8'),
|
---|
161 | };
|
---|
162 | }
|
---|
163 | return ssl;
|
---|
164 | }
|
---|
165 | /**
|
---|
166 | * Private method to enhance a webpack config with Proxy configuration.
|
---|
167 | * @private
|
---|
168 | */
|
---|
169 | function addProxyConfig(root, proxyConfig) {
|
---|
170 | if (!proxyConfig) {
|
---|
171 | return undefined;
|
---|
172 | }
|
---|
173 | const proxyPath = path_1.resolve(root, proxyConfig);
|
---|
174 | if (fs_1.existsSync(proxyPath)) {
|
---|
175 | return require(proxyPath);
|
---|
176 | }
|
---|
177 | throw new Error('Proxy config file ' + proxyPath + ' does not exist.');
|
---|
178 | }
|
---|
179 | /**
|
---|
180 | * Find the default server path. We don't want to expose baseHref and deployUrl as arguments, only
|
---|
181 | * the browser options where needed. This method should stay private (people who want to resolve
|
---|
182 | * baseHref and deployUrl should use the buildServePath exported function.
|
---|
183 | * @private
|
---|
184 | */
|
---|
185 | function findDefaultServePath(baseHref, deployUrl) {
|
---|
186 | if (!baseHref && !deployUrl) {
|
---|
187 | return '';
|
---|
188 | }
|
---|
189 | if (/^(\w+:)?\/\//.test(baseHref || '') || /^(\w+:)?\/\//.test(deployUrl || '')) {
|
---|
190 | // If baseHref or deployUrl is absolute, unsupported by ng serve
|
---|
191 | return null;
|
---|
192 | }
|
---|
193 | // normalize baseHref
|
---|
194 | // for ng serve the starting base is always `/` so a relative
|
---|
195 | // and root relative value are identical
|
---|
196 | const baseHrefParts = (baseHref || '').split('/').filter((part) => part !== '');
|
---|
197 | if (baseHref && !baseHref.endsWith('/')) {
|
---|
198 | baseHrefParts.pop();
|
---|
199 | }
|
---|
200 | const normalizedBaseHref = baseHrefParts.length === 0 ? '/' : `/${baseHrefParts.join('/')}/`;
|
---|
201 | if (deployUrl && deployUrl[0] === '/') {
|
---|
202 | if (baseHref && baseHref[0] === '/' && normalizedBaseHref !== deployUrl) {
|
---|
203 | // If baseHref and deployUrl are root relative and not equivalent, unsupported by ng serve
|
---|
204 | return null;
|
---|
205 | }
|
---|
206 | return deployUrl;
|
---|
207 | }
|
---|
208 | // Join together baseHref and deployUrl
|
---|
209 | return `${normalizedBaseHref}${deployUrl || ''}`;
|
---|
210 | }
|
---|