Skip to content

Commit 37fc7f0

Browse files
committedApr 26, 2024
fix(@angular-devkit/build-angular): 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 29dd052)
1 parent a4362aa commit 37fc7f0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/dev-server/builder.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ export function execute(
6767
);
6868
}
6969

70-
// Warn if the initial options provided by the user enable prebundling but caching is disabled
71-
if (options.prebundle && !normalizedOptions.cacheOptions.enabled) {
72-
context.logger.warn(
73-
`Prebundling has been configured but will not be used because caching has been disabled.`,
74-
);
75-
}
76-
7770
if (options.allowedHosts?.length) {
7871
context.logger.warn(
7972
`The "allowedHosts" option will not be used because it is not supported by the "${builderName}" builder.`,
@@ -188,7 +181,7 @@ case.
188181
};
189182
}
190183

191-
function isEsbuildBased(
184+
export function isEsbuildBased(
192185
builderName: string,
193186
): builderName is
194187
| '@angular/build:application'

‎packages/angular_devkit/build_angular/src/builders/dev-server/options.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
1010
import path from 'node:path';
1111
import { normalizeCacheOptions } from '../../utils/normalize-cache';
12+
import { normalizeOptimization } from '../../utils/normalize-optimization';
13+
import { isEsbuildBased } from './builder';
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,27 @@ export async function normalizeOptions(
3840
const buildTargetSpecifier = options.buildTarget ?? options.browserTarget ?? `::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+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47+
const buildOptions = (await context.validateOptions(rawBuildOptions, browserBuilderName)) as any;
48+
const optimization = normalizeOptimization(buildOptions.optimization);
49+
50+
if (options.prebundle !== false && isEsbuildBased(browserBuilderName)) {
51+
if (!cacheOptions.enabled) {
52+
// Warn if the initial options provided by the user enable prebundling but caching is disabled
53+
logger.warn(
54+
'Prebundling has been configured but will not be used because caching has been disabled.',
55+
);
56+
} else if (optimization.scripts) {
57+
// Warn if the initial options provided by the user enable prebundling but script optimization is enabled.
58+
logger.warn(
59+
'Prebundling has been configured but will not be used because scripts optimization is enabled.',
60+
);
61+
}
62+
}
63+
4164
// Initial options to keep
4265
const {
4366
host,
@@ -86,6 +109,6 @@ export async function normalizeOptions(
86109
sslKey,
87110
forceEsbuild,
88111
// Prebundling defaults to true but requires caching to function
89-
prebundle: cacheOptions.enabled && (prebundle ?? true),
112+
prebundle: cacheOptions.enabled && !optimization.scripts && (prebundle ?? true),
90113
};
91114
}

0 commit comments

Comments
 (0)