Skip to content

Commit 940e382

Browse files
committedApr 26, 2024·
fix(@angular/build): disable Vite prebundling when script optimizations are enabled
This change ensures that `ngDevMode` is replaced in node packages, aligning the behavior of the Vite server more closely with a production environment. (cherry picked from commit 60752fd)
1 parent 37fc7f0 commit 940e382

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed
 

Diff for: ‎goldens/circular-deps/packages.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
[
22
[
3-
"packages/angular/build/src/tools/esbuild/bundler-context.ts",
4-
"packages/angular/build/src/tools/esbuild/utils.ts"
3+
"packages/angular_devkit/build_angular/src/builders/dev-server/builder.ts",
4+
"packages/angular_devkit/build_angular/src/builders/dev-server/options.ts"
55
],
66
[
7-
"packages/angular/build/src/tools/esbuild/bundler-execution-result.ts",
7+
"packages/angular/build/src/tools/esbuild/bundler-context.ts",
88
"packages/angular/build/src/tools/esbuild/utils.ts"
99
],
1010
[
1111
"packages/angular/build/src/tools/esbuild/bundler-context.ts",
1212
"packages/angular/build/src/tools/esbuild/utils.ts",
1313
"packages/angular/build/src/tools/esbuild/bundler-execution-result.ts"
1414
],
15+
[
16+
"packages/angular/build/src/tools/esbuild/bundler-execution-result.ts",
17+
"packages/angular/build/src/tools/esbuild/utils.ts"
18+
],
1519
[
1620
"packages/angular/cli/src/analytics/analytics-collector.ts",
1721
"packages/angular/cli/src/command-builder/command-module.ts"

Diff for: ‎packages/angular/build/src/builders/dev-server/options.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
1010
import path from 'node:path';
11+
import { normalizeOptimization } from '../../utils';
1112
import { normalizeCacheOptions } from '../../utils/normalize-cache';
13+
import { ApplicationBuilderOptions } from '../application';
1214
import { Schema as DevServerOptions } from './schema';
1315

1416
export type NormalizedDevServerOptions = Awaited<ReturnType<typeof normalizeOptions>>;
@@ -28,7 +30,7 @@ export async function normalizeOptions(
2830
projectName: string,
2931
options: DevServerOptions,
3032
) {
31-
const workspaceRoot = context.workspaceRoot;
33+
const { workspaceRoot, logger } = context;
3234
const projectMetadata = await context.getProjectMetadata(projectName);
3335
const projectRoot = path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? '');
3436

@@ -38,6 +40,29 @@ export async function normalizeOptions(
3840
const buildTargetSpecifier = options.buildTarget ?? `::development`;
3941
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');
4042

43+
// Get the application builder options.
44+
const browserBuilderName = await context.getBuilderNameForTarget(buildTarget);
45+
const rawBuildOptions = await context.getTargetOptions(buildTarget);
46+
const buildOptions = (await context.validateOptions(
47+
rawBuildOptions,
48+
browserBuilderName,
49+
)) as unknown as ApplicationBuilderOptions;
50+
const optimization = normalizeOptimization(buildOptions.optimization);
51+
52+
if (options.prebundle) {
53+
if (!cacheOptions.enabled) {
54+
// Warn if the initial options provided by the user enable prebundling but caching is disabled
55+
logger.warn(
56+
'Prebundling has been configured but will not be used because caching has been disabled.',
57+
);
58+
} else if (optimization.scripts) {
59+
// Warn if the initial options provided by the user enable prebundling but script optimization is enabled.
60+
logger.warn(
61+
'Prebundling has been configured but will not be used because scripts optimization is enabled.',
62+
);
63+
}
64+
}
65+
4166
// Initial options to keep
4267
const {
4368
host,
@@ -78,6 +103,6 @@ export async function normalizeOptions(
78103
sslCert,
79104
sslKey,
80105
// Prebundling defaults to true but requires caching to function
81-
prebundle: cacheOptions.enabled && (prebundle ?? true),
106+
prebundle: cacheOptions.enabled && !optimization.scripts && prebundle,
82107
};
83108
}

Diff for: ‎packages/angular/build/src/builders/dev-server/schema.json

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
},
8282
"prebundle": {
8383
"description": "Enable and control the Vite-based development server's prebundling capabilities. To enable prebundling, the Angular CLI cache must also be enabled.",
84+
"default": true,
8485
"oneOf": [
8586
{ "type": "boolean" },
8687
{

0 commit comments

Comments
 (0)
Please sign in to comment.