Skip to content

Commit

Permalink
fix(@angular/build): disable Vite prebundling when script optimizatio…
Browse files Browse the repository at this point in the history
…ns 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)
  • Loading branch information
alan-agius4 committed Apr 26, 2024
1 parent 37fc7f0 commit 940e382
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
10 changes: 7 additions & 3 deletions goldens/circular-deps/packages.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
[
[
"packages/angular/build/src/tools/esbuild/bundler-context.ts",
"packages/angular/build/src/tools/esbuild/utils.ts"
"packages/angular_devkit/build_angular/src/builders/dev-server/builder.ts",
"packages/angular_devkit/build_angular/src/builders/dev-server/options.ts"
],
[
"packages/angular/build/src/tools/esbuild/bundler-execution-result.ts",
"packages/angular/build/src/tools/esbuild/bundler-context.ts",
"packages/angular/build/src/tools/esbuild/utils.ts"
],
[
"packages/angular/build/src/tools/esbuild/bundler-context.ts",
"packages/angular/build/src/tools/esbuild/utils.ts",
"packages/angular/build/src/tools/esbuild/bundler-execution-result.ts"
],
[
"packages/angular/build/src/tools/esbuild/bundler-execution-result.ts",
"packages/angular/build/src/tools/esbuild/utils.ts"
],
[
"packages/angular/cli/src/analytics/analytics-collector.ts",
"packages/angular/cli/src/command-builder/command-module.ts"
Expand Down
29 changes: 27 additions & 2 deletions packages/angular/build/src/builders/dev-server/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
import path from 'node:path';
import { normalizeOptimization } from '../../utils';
import { normalizeCacheOptions } from '../../utils/normalize-cache';
import { ApplicationBuilderOptions } from '../application';
import { Schema as DevServerOptions } from './schema';

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

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

// Get the application builder options.
const browserBuilderName = await context.getBuilderNameForTarget(buildTarget);
const rawBuildOptions = await context.getTargetOptions(buildTarget);
const buildOptions = (await context.validateOptions(
rawBuildOptions,
browserBuilderName,
)) as unknown as ApplicationBuilderOptions;
const optimization = normalizeOptimization(buildOptions.optimization);

if (options.prebundle) {
if (!cacheOptions.enabled) {
// Warn if the initial options provided by the user enable prebundling but caching is disabled
logger.warn(
'Prebundling has been configured but will not be used because caching has been disabled.',
);
} else if (optimization.scripts) {
// Warn if the initial options provided by the user enable prebundling but script optimization is enabled.
logger.warn(
'Prebundling has been configured but will not be used because scripts optimization is enabled.',
);
}
}

// Initial options to keep
const {
host,
Expand Down Expand Up @@ -78,6 +103,6 @@ export async function normalizeOptions(
sslCert,
sslKey,
// Prebundling defaults to true but requires caching to function
prebundle: cacheOptions.enabled && (prebundle ?? true),
prebundle: cacheOptions.enabled && !optimization.scripts && prebundle,
};
}
1 change: 1 addition & 0 deletions packages/angular/build/src/builders/dev-server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
},
"prebundle": {
"description": "Enable and control the Vite-based development server's prebundling capabilities. To enable prebundling, the Angular CLI cache must also be enabled.",
"default": true,
"oneOf": [
{ "type": "boolean" },
{
Expand Down

0 comments on commit 940e382

Please sign in to comment.