Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): disable Vite prebundling when scr…
Browse files Browse the repository at this point in the history
…ipt 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)
  • Loading branch information
alan-agius4 committed Apr 26, 2024
1 parent a4362aa commit 37fc7f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ export function execute(
);
}

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

if (options.allowedHosts?.length) {
context.logger.warn(
`The "allowedHosts" option will not be used because it is not supported by the "${builderName}" builder.`,
Expand Down Expand Up @@ -188,7 +181,7 @@ case.
};
}

function isEsbuildBased(
export function isEsbuildBased(
builderName: string,
): builderName is
| '@angular/build:application'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
import path from 'node:path';
import { normalizeCacheOptions } from '../../utils/normalize-cache';
import { normalizeOptimization } from '../../utils/normalize-optimization';
import { isEsbuildBased } from './builder';
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,27 @@ export async function normalizeOptions(
const buildTargetSpecifier = options.buildTarget ?? options.browserTarget ?? `::development`;
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');

// Get the application builder options.
const browserBuilderName = await context.getBuilderNameForTarget(buildTarget);
const rawBuildOptions = await context.getTargetOptions(buildTarget);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const buildOptions = (await context.validateOptions(rawBuildOptions, browserBuilderName)) as any;
const optimization = normalizeOptimization(buildOptions.optimization);

if (options.prebundle !== false && isEsbuildBased(browserBuilderName)) {
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 @@ -86,6 +109,6 @@ export async function normalizeOptions(
sslKey,
forceEsbuild,
// Prebundling defaults to true but requires caching to function
prebundle: cacheOptions.enabled && (prebundle ?? true),
prebundle: cacheOptions.enabled && !optimization.scripts && (prebundle ?? true),
};
}

0 comments on commit 37fc7f0

Please sign in to comment.