Skip to content

Commit

Permalink
fix(vite): more properly resolve arguments from configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Dec 18, 2023
1 parent 626605e commit a12e898
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
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
21 changes: 18 additions & 3 deletions packages/vite/src/executors/preview-server/preview-server.impl.ts
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

0 comments on commit a12e898

Please sign in to comment.