|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { BuilderContext, BuilderOutput } from '@angular-devkit/architect'; |
| 10 | +import { EMPTY, Observable, defer, switchMap } from 'rxjs'; |
| 11 | +import { ExecutionTransformer } from '../../transforms'; |
| 12 | +import { checkPort } from '../../utils/check-port'; |
| 13 | +import { IndexHtmlTransform } from '../../utils/index-file/index-html-generator'; |
| 14 | +import { purgeStaleBuildCache } from '../../utils/purge-cache'; |
| 15 | +import { normalizeOptions } from './options'; |
| 16 | +import { Schema as DevServerBuilderOptions } from './schema'; |
| 17 | +import { DevServerBuilderOutput, serveWebpackBrowser } from './webpack-server'; |
| 18 | + |
| 19 | +/** |
| 20 | + * A Builder that executes a development server based on the provided browser target option. |
| 21 | + * @param options Dev Server options. |
| 22 | + * @param context The build context. |
| 23 | + * @param transforms A map of transforms that can be used to hook into some logic (such as |
| 24 | + * transforming webpack configuration before passing it to webpack). |
| 25 | + * |
| 26 | + * @experimental Direct usage of this function is considered experimental. |
| 27 | + */ |
| 28 | +export function execute( |
| 29 | + options: DevServerBuilderOptions, |
| 30 | + context: BuilderContext, |
| 31 | + transforms: { |
| 32 | + webpackConfiguration?: ExecutionTransformer<import('webpack').Configuration>; |
| 33 | + logging?: import('@angular-devkit/build-webpack').WebpackLoggingCallback; |
| 34 | + indexHtml?: IndexHtmlTransform; |
| 35 | + } = {}, |
| 36 | +): Observable<DevServerBuilderOutput> { |
| 37 | + // Determine project name from builder context target |
| 38 | + const projectName = context.target?.project; |
| 39 | + if (!projectName) { |
| 40 | + context.logger.error(`The 'dev-server' builder requires a target to be specified.`); |
| 41 | + |
| 42 | + return EMPTY; |
| 43 | + } |
| 44 | + |
| 45 | + return defer(() => initialize(options, projectName, context)).pipe( |
| 46 | + switchMap(({ builderName, normalizedOptions }) => { |
| 47 | + // Issue a warning that the dev-server does not currently support the experimental esbuild- |
| 48 | + // based builder and will use Webpack. |
| 49 | + if (builderName === '@angular-devkit/build-angular:browser-esbuild') { |
| 50 | + context.logger.warn( |
| 51 | + 'WARNING: The experimental esbuild-based builder is not currently supported ' + |
| 52 | + 'by the dev-server. The stable Webpack-based builder will be used instead.', |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return serveWebpackBrowser(normalizedOptions, builderName, context, transforms); |
| 57 | + }), |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +async function initialize( |
| 62 | + initialOptions: DevServerBuilderOptions, |
| 63 | + projectName: string, |
| 64 | + context: BuilderContext, |
| 65 | +) { |
| 66 | + // Purge old build disk cache. |
| 67 | + await purgeStaleBuildCache(context); |
| 68 | + |
| 69 | + const normalizedOptions = await normalizeOptions(context, projectName, initialOptions); |
| 70 | + const builderName = await context.getBuilderNameForTarget(normalizedOptions.browserTarget); |
| 71 | + |
| 72 | + if ( |
| 73 | + !normalizedOptions.disableHostCheck && |
| 74 | + !/^127\.\d+\.\d+\.\d+/g.test(normalizedOptions.host) && |
| 75 | + normalizedOptions.host !== 'localhost' |
| 76 | + ) { |
| 77 | + context.logger.warn(` |
| 78 | +Warning: This is a simple server for use in testing or debugging Angular applications |
| 79 | +locally. It hasn't been reviewed for security issues. |
| 80 | +
|
| 81 | +Binding this server to an open connection can result in compromising your application or |
| 82 | +computer. Using a different host than the one passed to the "--host" flag might result in |
| 83 | +websocket connection issues. You might need to use "--disable-host-check" if that's the |
| 84 | +case. |
| 85 | + `); |
| 86 | + } |
| 87 | + |
| 88 | + if (normalizedOptions.disableHostCheck) { |
| 89 | + context.logger.warn( |
| 90 | + 'Warning: Running a server with --disable-host-check is a security risk. ' + |
| 91 | + 'See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.', |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + normalizedOptions.port = await checkPort(normalizedOptions.port, normalizedOptions.host); |
| 96 | + |
| 97 | + return { builderName, normalizedOptions }; |
| 98 | +} |
0 commit comments