diff --git a/packages/vite/src/executors/dev-server/dev-server.impl.ts b/packages/vite/src/executors/dev-server/dev-server.impl.ts index bcc2a27bf6342..27123cb5d5e40 100644 --- a/packages/vite/src/executors/dev-server/dev-server.impl.ts +++ b/packages/vite/src/executors/dev-server/dev-server.impl.ts @@ -1,4 +1,8 @@ -import { ExecutorContext, joinPathFragments } from '@nx/devkit'; +import { + ExecutorContext, + joinPathFragments, + parseTargetString, +} from '@nx/devkit'; import { getNxTargetOptions, getViteServerOptions, @@ -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, @@ -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', @@ -110,7 +123,10 @@ async function runViteDevServer(server: Record): Promise { export default viteDevServerExecutor; async function getServerExtraArgs( - options: ViteDevServerExecutorOptions + options: ViteDevServerExecutorOptions, + configuration: string | undefined, + buildOptionsFromBuildTarget: Record | undefined, + otherOptionsFromBuildTarget: Record | undefined ): Promise<{ // vite ServerOptions serverOptions: Record; @@ -125,7 +141,7 @@ async function getServerExtraArgs( } } - const serverOptions = {}; + let serverOptions: Record = {}; const serverSchemaKeys = [ 'hmr', 'warmup', @@ -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]; @@ -154,6 +170,17 @@ async function getServerExtraArgs( } } + if (configuration) { + serverOptions = { + ...serverOptions, + watch: buildOptionsFromBuildTarget?.watch ?? serverOptions?.watch, + }; + otherOptions = { + ...otherOptions, + ...(otherOptionsFromBuildTarget ?? {}), + }; + } + return { serverOptions, otherOptions, diff --git a/packages/vite/src/executors/preview-server/preview-server.impl.ts b/packages/vite/src/executors/preview-server/preview-server.impl.ts index 4f6bc219fc970..ec1c5a6460c6c 100644 --- a/packages/vite/src/executors/preview-server/preview-server.impl.ts +++ b/packages/vite/src/executors/preview-server/preview-server.impl.ts @@ -45,6 +45,8 @@ export async function* vitePreviewServerExecutor( context ); + const { configuration } = parseTargetString(options.buildTarget, context); + const viteConfigPath = normalizeViteConfigFilePath( context.root, projectRoot, @@ -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', @@ -190,7 +196,9 @@ function closeServer(server?: Record): Promise { export default vitePreviewServerExecutor; async function getExtraArgs( - options: VitePreviewServerExecutorOptions + options: VitePreviewServerExecutorOptions, + configuration: string | undefined, + otherOptionsFromBuildTarget: Record | undefined ): Promise<{ // vite PreviewOptions previewOptions: Record; @@ -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]; @@ -226,6 +234,13 @@ async function getExtraArgs( } } + if (configuration) { + otherOptions = { + ...otherOptions, + ...(otherOptionsFromBuildTarget ?? {}), + }; + } + return { previewOptions, otherOptions,