Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): more properly resolve arguments from configurations #20825

Merged
Merged
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
43 changes: 35 additions & 8 deletions packages/vite/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ExecutorContext, joinPathFragments } from '@nx/devkit';
import {
ExecutorContext,
joinPathFragments,
parseTargetString,
} from '@nx/devkit';
import {
getNxTargetOptions,
getViteServerOptions,
Expand All @@ -8,6 +12,7 @@ import { ViteDevServerExecutorOptions } from './schema';
import { ViteBuildExecutorOptions } from '../build/schema';
import { createBuildableTsConfig } from '../../utils/executor-utils';
import { relative } from 'path';
import { getBuildExtraArgs } from '../build/build.impl';

export async function* viteDevServerExecutor(
options: ViteDevServerExecutorOptions,
Expand All @@ -32,15 +37,23 @@ export async function* viteDevServerExecutor(
options.buildTarget,
context
);

const { configuration } = parseTargetString(options.buildTarget, context);

const { buildOptions, otherOptions: otherOptionsFromBuild } =
await getBuildExtraArgs(buildTargetOptions);

const viteConfigPath = normalizeViteConfigFilePath(
context.root,
projectRoot,
buildTargetOptions.configFile
);
const { serverOptions, otherOptions } = await getServerExtraArgs({
...options,
...buildTargetOptions,
});
const { serverOptions, otherOptions } = await getServerExtraArgs(
options,
configuration,
buildOptions,
otherOptionsFromBuild
);
const resolved = await loadConfigFromFile(
{
mode: otherOptions?.mode ?? buildTargetOptions?.['mode'] ?? 'development',
Expand Down Expand Up @@ -110,7 +123,10 @@ async function runViteDevServer(server: Record<string, any>): Promise<void> {
export default viteDevServerExecutor;

async function getServerExtraArgs(
options: ViteDevServerExecutorOptions
options: ViteDevServerExecutorOptions,
configuration: string | undefined,
buildOptionsFromBuildTarget: Record<string, unknown> | undefined,
otherOptionsFromBuildTarget: Record<string, unknown> | undefined
): Promise<{
// vite ServerOptions
serverOptions: Record<string, unknown>;
Expand All @@ -125,7 +141,7 @@ async function getServerExtraArgs(
}
}

const serverOptions = {};
let serverOptions: Record<string, unknown> = {};
const serverSchemaKeys = [
'hmr',
'warmup',
Expand All @@ -145,7 +161,7 @@ async function getServerExtraArgs(
'headers',
];

const otherOptions = {};
let otherOptions = {};
for (const key of Object.keys(extraArgs)) {
if (serverSchemaKeys.includes(key)) {
serverOptions[key] = extraArgs[key];
Expand All @@ -154,6 +170,17 @@ async function getServerExtraArgs(
}
}

if (configuration) {
serverOptions = {
...serverOptions,
watch: buildOptionsFromBuildTarget?.watch ?? serverOptions?.watch,
};
otherOptions = {
...otherOptions,
...(otherOptionsFromBuildTarget ?? {}),
};
}

return {
serverOptions,
otherOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export async function* vitePreviewServerExecutor(
context
);

const { configuration } = parseTargetString(options.buildTarget, context);

const viteConfigPath = normalizeViteConfigFilePath(
context.root,
projectRoot,
Expand All @@ -54,7 +56,11 @@ export async function* vitePreviewServerExecutor(
const { buildOptions, otherOptions: otherOptionsFromBuild } =
await getBuildExtraArgs(buildTargetOptions);

const { previewOptions, otherOptions } = await getExtraArgs(options);
const { previewOptions, otherOptions } = await getExtraArgs(
options,
configuration,
otherOptionsFromBuild
);
const resolved = await loadConfigFromFile(
{
mode: otherOptions?.mode ?? otherOptionsFromBuild?.mode ?? 'production',
Expand Down Expand Up @@ -190,7 +196,9 @@ function closeServer(server?: Record<string, any>): Promise<void> {
export default vitePreviewServerExecutor;

async function getExtraArgs(
options: VitePreviewServerExecutorOptions
options: VitePreviewServerExecutorOptions,
configuration: string | undefined,
otherOptionsFromBuildTarget: Record<string, unknown> | undefined
): Promise<{
// vite PreviewOptions
previewOptions: Record<string, any>;
Expand All @@ -217,7 +225,7 @@ async function getExtraArgs(
'headers',
];

const otherOptions = {};
let otherOptions = {};
for (const key of Object.keys(extraArgs)) {
if (previewSchemaKeys.includes(key)) {
previewOptions[key] = extraArgs[key];
Expand All @@ -226,6 +234,13 @@ async function getExtraArgs(
}
}

if (configuration) {
otherOptions = {
...otherOptions,
...(otherOptionsFromBuildTarget ?? {}),
};
}

return {
previewOptions,
otherOptions,
Expand Down