|
| 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 type { BuilderContext } from '@angular-devkit/architect'; |
| 10 | +import type { Plugin } from 'esbuild'; |
| 11 | +import type http from 'node:http'; |
| 12 | +import { checkPort } from '../../utils/check-port'; |
| 13 | +import { |
| 14 | + type IndexHtmlTransform, |
| 15 | + buildApplicationInternal, |
| 16 | + purgeStaleBuildCache, |
| 17 | +} from './internal'; |
| 18 | +import { normalizeOptions } from './options'; |
| 19 | +import type { DevServerBuilderOutput } from './output'; |
| 20 | +import type { Schema as DevServerBuilderOptions } from './schema'; |
| 21 | +import { serveWithVite } from './vite-server'; |
| 22 | + |
| 23 | +/** |
| 24 | + * A Builder that executes a development server based on the provided browser target option. |
| 25 | + * |
| 26 | + * Usage of the `transforms` and/or `extensions` parameters is NOT supported and may cause |
| 27 | + * unexpected build output or build failures. |
| 28 | + * |
| 29 | + * @param options Dev Server options. |
| 30 | + * @param context The build context. |
| 31 | + * @param extensions An optional object containing an array of build plugins (esbuild-based) |
| 32 | + * and/or HTTP request middleware. |
| 33 | + * |
| 34 | + * @experimental Direct usage of this function is considered experimental. |
| 35 | + */ |
| 36 | +export async function* execute( |
| 37 | + options: DevServerBuilderOptions, |
| 38 | + context: BuilderContext, |
| 39 | + extensions?: { |
| 40 | + buildPlugins?: Plugin[]; |
| 41 | + middleware?: (( |
| 42 | + req: http.IncomingMessage, |
| 43 | + res: http.ServerResponse, |
| 44 | + next: (err?: unknown) => void, |
| 45 | + ) => void)[]; |
| 46 | + indexHtmlTransformer?: IndexHtmlTransform; |
| 47 | + }, |
| 48 | +): AsyncIterable<DevServerBuilderOutput> { |
| 49 | + // Determine project name from builder context target |
| 50 | + const projectName = context.target?.project; |
| 51 | + if (!projectName) { |
| 52 | + context.logger.error(`The "dev-server" builder requires a target to be specified.`); |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const { builderName, normalizedOptions } = await initialize(options, projectName, context); |
| 58 | + |
| 59 | + // Warn if the initial options provided by the user enable prebundling but caching is disabled |
| 60 | + if (options.prebundle && !normalizedOptions.cacheOptions.enabled) { |
| 61 | + context.logger.warn( |
| 62 | + `Prebundling has been configured but will not be used because caching has been disabled.`, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + yield* serveWithVite( |
| 67 | + normalizedOptions, |
| 68 | + builderName, |
| 69 | + (options, context, plugins) => |
| 70 | + buildApplicationInternal(options, context, { write: false }, { codePlugins: plugins }), |
| 71 | + context, |
| 72 | + { indexHtml: extensions?.indexHtmlTransformer }, |
| 73 | + extensions, |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +async function initialize( |
| 78 | + initialOptions: DevServerBuilderOptions, |
| 79 | + projectName: string, |
| 80 | + context: BuilderContext, |
| 81 | +) { |
| 82 | + // Purge old build disk cache. |
| 83 | + await purgeStaleBuildCache(context); |
| 84 | + |
| 85 | + const normalizedOptions = await normalizeOptions(context, projectName, initialOptions); |
| 86 | + const builderName = await context.getBuilderNameForTarget(normalizedOptions.buildTarget); |
| 87 | + |
| 88 | + if ( |
| 89 | + !normalizedOptions.disableHostCheck && |
| 90 | + !/^127\.\d+\.\d+\.\d+/g.test(normalizedOptions.host) && |
| 91 | + normalizedOptions.host !== 'localhost' |
| 92 | + ) { |
| 93 | + context.logger.warn(` |
| 94 | +Warning: This is a simple server for use in testing or debugging Angular applications |
| 95 | +locally. It hasn't been reviewed for security issues. |
| 96 | +
|
| 97 | +Binding this server to an open connection can result in compromising your application or |
| 98 | +computer. Using a different host than the one passed to the "--host" flag might result in |
| 99 | +websocket connection issues. You might need to use "--disable-host-check" if that's the |
| 100 | +case. |
| 101 | + `); |
| 102 | + } |
| 103 | + |
| 104 | + if (normalizedOptions.disableHostCheck) { |
| 105 | + context.logger.warn( |
| 106 | + 'Warning: Running a server with --disable-host-check is a security risk. ' + |
| 107 | + 'See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.', |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + normalizedOptions.port = await checkPort(normalizedOptions.port, normalizedOptions.host); |
| 112 | + |
| 113 | + return { |
| 114 | + builderName, |
| 115 | + normalizedOptions, |
| 116 | + }; |
| 117 | +} |
0 commit comments