Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ export async function* serveWithVite(
extensions?.middleware,
transformers?.indexHtml,
thirdPartySourcemaps,
browserOptions.conditions,
);

server = await createServer(serverConfiguration);
Expand Down
7 changes: 7 additions & 0 deletions packages/angular/build/src/builders/dev-server/vite/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ function createSsrConfig(
thirdPartySourcemaps: boolean,
define: ApplicationBuilderInternalOptions['define'],
target: string[],
conditions: string[] | undefined,
): Vite.SSROptions {
return {
// Prebundle with the same conditions as the server build.
resolve: conditions ? { conditions: ['node', ...conditions] } : undefined,
Comment thread
damienwebdev marked this conversation as resolved.
Resolved
// Note: `true` and `/.*/` have different sematics. When true, the `external` option is ignored.
noExternal: /.*/,
// Exclude any Node.js built in module and provided dependencies (currently build defined externals)
Expand Down Expand Up @@ -147,6 +150,7 @@ export async function setupServer(
extensionMiddleware?: Vite.Connect.NextHandleFunction[],
indexHtmlTransformer?: (content: string) => Promise<string>,
thirdPartySourcemaps = false,
conditions?: string[],
): Promise<Vite.InlineConfig> {
await initializeHash();
const { normalizePath } = (await import('vite' as string)) as typeof Vite;
Expand Down Expand Up @@ -196,6 +200,8 @@ export async function setupServer(
base: serverOptions.servePath,
resolve: {
mainFields: ['es2020', 'browser', 'module', 'main'],
// Prebundle with the same conditions as the browser build.
conditions: conditions ? ['browser', ...conditions] : undefined,
Comment thread
damienwebdev marked this conversation as resolved.
Resolved
preserveSymlinks,
},
dev: {
Expand All @@ -222,6 +228,7 @@ export async function setupServer(
thirdPartySourcemaps,
define,
target,
conditions,
),
plugins: [
createAngularSetupMiddlewaresPlugin({
Expand Down
78 changes: 78 additions & 0 deletions packages/angular/build/src/builders/dev-server/vite/server_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import type { JavaScriptTransformer } from '../internal';
import { ServerSsrMode } from '../../../tools/vite/plugins';
import type { NormalizedDevServerOptions } from '../options';
import { setupServer } from './server';

describe('setupServer', () => {
const serverOptions = {
workspaceRoot: '/workspace',
projectRoot: '/workspace',
buildTarget: { project: 'app', target: 'build' },
cacheOptions: { path: '/workspace/.angular/cache', enabled: true },
servePath: '/',
port: 4200,
liveReload: true,
} as unknown as NormalizedDevServerOptions;

const externalMetadata = {
implicitBrowser: ['cond-pkg'],
implicitServer: ['cond-pkg'],
explicitBrowser: [],
explicitServer: [],
};

function createConfig(ssrMode: ServerSsrMode, conditions?: string[]) {
return setupServer(
serverOptions,
new Map(),
new Map(),
undefined,
externalMetadata,
ssrMode,
{} as JavaScriptTransformer,
['es2022'],
new Map(),
new Map(),
undefined,
undefined,
undefined,
undefined,
false,
conditions,
);
}

it('should not set resolve conditions when the build has no conditions', async () => {
const config = await createConfig(ServerSsrMode.InternalSsrMiddleware);

expect(config.resolve?.conditions).toBeUndefined();
expect(config.ssr?.resolve?.conditions).toBeUndefined();
});
Comment thread
damienwebdev marked this conversation as resolved.
Resolved

it('should add the build conditions to the browser resolve conditions', async () => {
const config = await createConfig(ServerSsrMode.NoSsr, ['my-condition']);

expect(config.resolve?.conditions).toEqual(['browser', 'my-condition']);
});

it('should add the build conditions to the SSR resolve conditions', async () => {
const config = await createConfig(ServerSsrMode.InternalSsrMiddleware, ['my-condition']);

expect(config.ssr?.resolve?.conditions).toEqual(['node', 'my-condition']);
});

it('should keep an empty conditions list instead of using the Vite defaults', async () => {
const config = await createConfig(ServerSsrMode.InternalSsrMiddleware, []);

expect(config.resolve?.conditions).toEqual(['browser']);
expect(config.ssr?.resolve?.conditions).toEqual(['node']);
});
});