-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
498 lines (442 loc) · 16.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
BuilderContext,
createBuilder,
targetFromTargetString,
} from '@angular-devkit/architect';
import {
DevServerBuildOutput,
WebpackLoggingCallback,
runWebpackDevServer,
} from '@angular-devkit/build-webpack';
import { experimental, json, logging, tags } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { existsSync, readFileSync } from 'fs';
import * as path from 'path';
import { Observable, from } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import * as url from 'url';
import * as webpack from 'webpack';
import * as WebpackDevServer from 'webpack-dev-server';
import { checkPort } from '../angular-cli-files/utilities/check-port';
import {
BrowserConfigTransformFn,
buildBrowserWebpackConfigFromContext,
createBrowserLoggingCallback,
} from '../browser';
import { Schema as BrowserBuilderSchema } from '../browser/schema';
import { normalizeOptimization } from '../utils';
import { Schema } from './schema';
const open = require('open');
export type DevServerBuilderSchema = Schema & json.JsonObject;
export const devServerBuildOverriddenKeys: (keyof DevServerBuilderSchema)[] = [
'watch',
'optimization',
'aot',
'sourceMap',
'vendorSourceMap',
'evalSourceMap',
'vendorChunk',
'commonChunk',
'baseHref',
'progress',
'poll',
'verbose',
'deployUrl',
];
export type DevServerBuilderOutput = DevServerBuildOutput & {
baseUrl: string;
};
export type ServerConfigTransformFn = (
workspace: experimental.workspace.Workspace,
config: WebpackDevServer.Configuration,
) => Observable<WebpackDevServer.Configuration>;
/**
* Reusable implementation of the build angular webpack dev server builder.
* @param options Dev Server options.
* @param context The build context.
* @param transforms A map of transforms that can be used to hook into some logic (such as
* transforming webpack configuration before passing it to webpack).
*/
export function serveWebpackBrowser(
options: DevServerBuilderSchema,
context: BuilderContext,
transforms: {
browserConfig?: BrowserConfigTransformFn,
serverConfig?: ServerConfigTransformFn,
logging?: WebpackLoggingCallback,
} = {},
): Observable<DevServerBuilderOutput> {
const browserTarget = targetFromTargetString(options.browserTarget);
const root = context.workspaceRoot;
let first = true;
let openAddress: string;
const host = new NodeJsSyncHost();
const loggingFn = transforms.logging
|| createBrowserLoggingCallback(!!options.verbose, context.logger);
async function setup(): Promise<{
browserOptions: json.JsonObject & BrowserBuilderSchema,
webpackConfig: webpack.Configuration,
webpackDevServerConfig: WebpackDevServer.Configuration,
port: number,
}> {
// Get the browser configuration from the target name.
const rawBrowserOptions = await context.getTargetOptions(browserTarget);
// Override options we need to override, if defined.
const overrides = (Object.keys(options) as (keyof DevServerBuilderSchema)[])
.filter(key => options[key] !== undefined && devServerBuildOverriddenKeys.includes(key))
.reduce<json.JsonObject & Partial<BrowserBuilderSchema>>((previous, key) => ({
...previous,
[key]: options[key],
}), {});
// In dev server we should not have budgets because of extra libs such as socks-js
overrides.budgets = undefined;
const browserName = await context.getBuilderNameForTarget(browserTarget);
const browserOptions = await context.validateOptions<json.JsonObject & BrowserBuilderSchema>(
{ ...rawBrowserOptions, ...overrides },
browserName,
);
const webpackConfigResult = await buildBrowserWebpackConfigFromContext(
browserOptions,
context,
host,
);
// No differential loading for dev-server, hence there is just one config
let webpackConfig = webpackConfigResult.config[0];
const workspace = webpackConfigResult.workspace;
if (transforms.browserConfig) {
webpackConfig = await transforms.browserConfig(workspace, webpackConfig).toPromise();
}
const port = await checkPort(options.port || 0, options.host || 'localhost', 4200);
let webpackDevServerConfig = buildServerConfig(
root,
options,
browserOptions,
context.logger,
);
if (transforms.serverConfig) {
webpackDevServerConfig = await transforms.serverConfig(workspace, webpackConfig).toPromise();
}
return { browserOptions, webpackConfig, webpackDevServerConfig, port };
}
return from(setup()).pipe(
switchMap(({ browserOptions, webpackConfig, webpackDevServerConfig, port }) => {
options.port = port;
// Resolve public host and client address.
let clientAddress = url.parse(`${options.ssl ? 'https' : 'http'}://0.0.0.0:0`);
if (options.publicHost) {
let publicHost = options.publicHost;
if (!/^\w+:\/\//.test(publicHost)) {
publicHost = `${options.ssl ? 'https' : 'http'}://${publicHost}`;
}
clientAddress = url.parse(publicHost);
options.publicHost = clientAddress.host;
}
// Resolve serve address.
const serverAddress = url.format({
protocol: options.ssl ? 'https' : 'http',
hostname: options.host === '0.0.0.0' ? 'localhost' : options.host,
// Port cannot be undefined here since we have a step that sets it back in options above.
// tslint:disable-next-line:no-non-null-assertion
port: options.port !.toString(),
});
// Add live reload config.
if (options.liveReload) {
_addLiveReload(options, browserOptions, webpackConfig, clientAddress, context.logger);
} else if (options.hmr) {
context.logger.warn('Live reload is disabled. HMR option ignored.');
}
if (!options.watch) {
// There's no option to turn off file watching in webpack-dev-server, but
// we can override the file watcher instead.
webpackConfig.plugins = [...(webpackConfig.plugins || []), {
// tslint:disable-next-line:no-any
apply: (compiler: any) => {
compiler.hooks.afterEnvironment.tap('angular-cli', () => {
compiler.watchFileSystem = { watch: () => { } };
});
},
}];
}
const normalizedOptimization = normalizeOptimization(browserOptions.optimization);
if (normalizedOptimization.scripts || normalizedOptimization.styles) {
context.logger.error(tags.stripIndents`
****************************************************************************************
This is a simple server for use in testing or debugging Angular applications locally.
It hasn't been reviewed for security issues.
DON'T USE IT FOR PRODUCTION!
****************************************************************************************
`);
}
context.logger.info(tags.oneLine`
**
Angular Live Development Server is listening on ${options.host}:${options.port},
open your browser on ${serverAddress}${webpackDevServerConfig.publicPath}
**
`);
openAddress = serverAddress + webpackDevServerConfig.publicPath;
webpackConfig.devServer = webpackDevServerConfig;
return runWebpackDevServer(webpackConfig, context, { logging: loggingFn });
}),
map(buildEvent => {
if (first && options.open) {
first = false;
open(openAddress);
}
return { ...buildEvent, baseUrl: openAddress } as DevServerBuilderOutput;
}),
);
}
/**
* Create a webpack configuration for the dev server.
* @param workspaceRoot The root of the workspace. This comes from the context.
* @param serverOptions DevServer options, based on the dev server input schema.
* @param browserOptions Browser builder options. See the browser builder from this package.
* @param logger A generic logger to use for showing warnings.
* @returns A webpack dev-server configuration.
*/
export function buildServerConfig(
workspaceRoot: string,
serverOptions: DevServerBuilderSchema,
browserOptions: BrowserBuilderSchema,
logger: logging.LoggerApi,
): WebpackDevServer.Configuration {
if (serverOptions.host) {
// Check that the host is either localhost or prints out a message.
if (!/^127\.\d+\.\d+\.\d+/g.test(serverOptions.host) && serverOptions.host !== 'localhost') {
logger.warn(tags.stripIndent`
WARNING: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.
Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
websocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
`);
}
}
if (serverOptions.disableHostCheck) {
logger.warn(tags.oneLine`
WARNING: Running a server with --disable-host-check is a security risk.
See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
for more information.
`);
}
const servePath = buildServePath(serverOptions, browserOptions, logger);
const { styles, scripts } = normalizeOptimization(browserOptions.optimization);
const config: WebpackDevServer.Configuration = {
host: serverOptions.host,
port: serverOptions.port,
headers: { 'Access-Control-Allow-Origin': '*' },
historyApiFallback: {
index: `${servePath}/${path.basename(browserOptions.index)}`,
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
} as WebpackDevServer.HistoryApiFallbackConfig,
stats: false,
compress: styles || scripts,
watchOptions: {
poll: browserOptions.poll,
},
https: serverOptions.ssl,
overlay: {
errors: !(styles || scripts),
warnings: false,
},
public: serverOptions.publicHost,
disableHostCheck: serverOptions.disableHostCheck,
publicPath: servePath,
hot: serverOptions.hmr,
contentBase: false,
};
if (serverOptions.ssl) {
_addSslConfig(workspaceRoot, serverOptions, config);
}
if (serverOptions.proxyConfig) {
_addProxyConfig(workspaceRoot, serverOptions, config);
}
return config;
}
/**
* Resolve and build a URL _path_ that will be the root of the server. This resolved base href and
* deploy URL from the browser options and returns a path from the root.
* @param serverOptions The server options that were passed to the server builder.
* @param browserOptions The browser options that were passed to the browser builder.
* @param logger A generic logger to use for showing warnings.
*/
export function buildServePath(
serverOptions: DevServerBuilderSchema,
browserOptions: BrowserBuilderSchema,
logger: logging.LoggerApi,
): string {
let servePath = serverOptions.servePath;
if (!servePath && servePath !== '') {
const defaultPath = _findDefaultServePath(browserOptions.baseHref, browserOptions.deployUrl);
const showWarning = serverOptions.servePathDefaultWarning;
if (defaultPath == null && showWarning) {
logger.warn(tags.oneLine`
WARNING: --deploy-url and/or --base-href contain unsupported values for ng serve. Default
serve path of '/' used. Use --serve-path to override.
`);
}
servePath = defaultPath || '';
}
if (servePath.endsWith('/')) {
servePath = servePath.substr(0, servePath.length - 1);
}
if (!servePath.startsWith('/')) {
servePath = `/${servePath}`;
}
return servePath;
}
/**
* Private method to enhance a webpack config with live reload configuration.
* @private
*/
function _addLiveReload(
options: DevServerBuilderSchema,
browserOptions: BrowserBuilderSchema,
webpackConfig: webpack.Configuration,
clientAddress: url.UrlWithStringQuery,
logger: logging.LoggerApi,
) {
if (webpackConfig.plugins === undefined) {
webpackConfig.plugins = [];
}
// This allows for live reload of page when changes are made to repo.
// https://webpack.js.org/configuration/dev-server/#devserver-inline
let webpackDevServerPath;
try {
webpackDevServerPath = require.resolve('webpack-dev-server/client');
} catch {
throw new Error('The "webpack-dev-server" package could not be found.');
}
// If a custom path is provided the webpack dev server client drops the sockjs-node segment.
// This adds it back so that behavior is consistent when using a custom URL path
if (clientAddress.pathname) {
clientAddress.pathname = path.posix.join(clientAddress.pathname, 'sockjs-node');
}
const entryPoints = [`${webpackDevServerPath}?${url.format(clientAddress)}`];
if (options.hmr) {
const webpackHmrLink = 'https://webpack.js.org/guides/hot-module-replacement';
logger.warn(
tags.oneLine`NOTICE: Hot Module Replacement (HMR) is enabled for the dev server.`);
const showWarning = options.hmrWarning;
if (showWarning) {
logger.info(tags.stripIndents`
The project will still live reload when HMR is enabled,
but to take advantage of HMR additional application code is required'
(not included in an Angular CLI project by default).'
See ${webpackHmrLink}
for information on working with HMR for Webpack.`,
);
logger.warn(
tags.oneLine`To disable this warning use "hmrWarning: false" under "serve"
options in "angular.json".`,
);
}
entryPoints.push('webpack/hot/dev-server');
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
if (browserOptions.extractCss) {
logger.warn(tags.oneLine`NOTICE: (HMR) does not allow for CSS hot reload
when used together with '--extract-css'.`);
}
}
if (typeof webpackConfig.entry !== 'object' || Array.isArray(webpackConfig.entry)) {
webpackConfig.entry = {} as webpack.Entry;
}
if (!Array.isArray(webpackConfig.entry.main)) {
webpackConfig.entry.main = [];
}
webpackConfig.entry.main.unshift(...entryPoints);
}
/**
* Private method to enhance a webpack config with SSL configuration.
* @private
*/
function _addSslConfig(
root: string,
options: DevServerBuilderSchema,
config: WebpackDevServer.Configuration,
) {
let sslKey: string | undefined = undefined;
let sslCert: string | undefined = undefined;
if (options.sslKey) {
const keyPath = path.resolve(root, options.sslKey);
if (existsSync(keyPath)) {
sslKey = readFileSync(keyPath, 'utf-8');
}
}
if (options.sslCert) {
const certPath = path.resolve(root, options.sslCert);
if (existsSync(certPath)) {
sslCert = readFileSync(certPath, 'utf-8');
}
}
config.https = true;
if (sslKey != null && sslCert != null) {
config.https = {
key: sslKey,
cert: sslCert,
};
}
}
/**
* Private method to enhance a webpack config with Proxy configuration.
* @private
*/
function _addProxyConfig(
root: string,
options: DevServerBuilderSchema,
config: WebpackDevServer.Configuration,
) {
let proxyConfig = {};
const proxyPath = path.resolve(root, options.proxyConfig as string);
if (existsSync(proxyPath)) {
proxyConfig = require(proxyPath);
} else {
const message = 'Proxy config file ' + proxyPath + ' does not exist.';
throw new Error(message);
}
config.proxy = proxyConfig;
}
/**
* Find the default server path. We don't want to expose baseHref and deployUrl as arguments, only
* the browser options where needed. This method should stay private (people who want to resolve
* baseHref and deployUrl should use the buildServePath exported function.
* @private
*/
function _findDefaultServePath(baseHref?: string, deployUrl?: string): string | null {
if (!baseHref && !deployUrl) {
return '';
}
if (/^(\w+:)?\/\//.test(baseHref || '') || /^(\w+:)?\/\//.test(deployUrl || '')) {
// If baseHref or deployUrl is absolute, unsupported by ng serve
return null;
}
// normalize baseHref
// for ng serve the starting base is always `/` so a relative
// and root relative value are identical
const baseHrefParts = (baseHref || '')
.split('/')
.filter(part => part !== '');
if (baseHref && !baseHref.endsWith('/')) {
baseHrefParts.pop();
}
const normalizedBaseHref = baseHrefParts.length === 0 ? '/' : `/${baseHrefParts.join('/')}/`;
if (deployUrl && deployUrl[0] === '/') {
if (baseHref && baseHref[0] === '/' && normalizedBaseHref !== deployUrl) {
// If baseHref and deployUrl are root relative and not equivalent, unsupported by ng serve
return null;
}
return deployUrl;
}
// Join together baseHref and deployUrl
return `${normalizedBaseHref}${deployUrl || ''}`;
}
export default createBuilder<DevServerBuilderSchema, DevServerBuilderOutput>(serveWebpackBrowser);