From f9f3de06d0668d94b0c787af27a4a66922075742 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 17 Oct 2024 17:26:34 +0100 Subject: [PATCH] fix(vite): use resolveConfig instead of loadConfigFromFile to ensure node env set #27627 (#28444) ## Current Behavior In the Vite Build Executor, we're using `loadConfigFromFile` from Vite to get the config options. The issue with this is that vite will not attempt to set `NODE_ENV` which may be required by both the config file that is being loaded, and other plugins. ## Expected Behavior Use resolveConfig which does set NODE_ENV correctly ## Related Issue(s) Fixes #27627 --- packages/vite/src/executors/build/build.impl.ts | 16 ++++++++-------- .../src/executors/dev-server/dev-server.impl.ts | 12 +++++++----- .../preview-server/preview-server.impl.ts | 14 +++++++------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/vite/src/executors/build/build.impl.ts b/packages/vite/src/executors/build/build.impl.ts index 277371a623914..5854b02923b51 100644 --- a/packages/vite/src/executors/build/build.impl.ts +++ b/packages/vite/src/executors/build/build.impl.ts @@ -34,8 +34,7 @@ export async function* viteBuildExecutor( ) { process.env.VITE_CJS_IGNORE_WARNING = 'true'; // Allows ESM to be required in CJS modules. Vite will be published as ESM in the future. - const { mergeConfig, build, loadConfigFromFile } = - await loadViteDynamicImport(); + const { mergeConfig, build, resolveConfig } = await loadViteDynamicImport(); const projectRoot = context.projectsConfigurations.projects[context.projectName].root; const tsConfigForBuild = createBuildableTsConfig( @@ -56,23 +55,24 @@ export async function* viteBuildExecutor( const { buildOptions, otherOptions } = await getBuildExtraArgs(options); - const resolved = await loadConfigFromFile( + const resolved = await resolveConfig( { + configFile: viteConfigPath, mode: otherOptions?.mode ?? 'production', - command: 'build', }, - viteConfigPath + 'build', + otherOptions?.mode ?? 'production' ); const outDir = joinPathFragments(offsetFromRoot(projectRoot), options.outputPath) ?? - resolved?.config?.build?.outDir; + resolved?.build?.outDir; const buildConfig = mergeConfig( { // This should not be needed as it's going to be set in vite.config.ts // but leaving it here in case someone did not migrate correctly - root: resolved.config.root ?? root, + root: resolved.root ?? root, configFile: viteConfigPath, }, { @@ -89,7 +89,7 @@ export async function* viteBuildExecutor( workspaceRoot: context.root, tsconfig: tsConfigForBuild, isVueProject: Boolean( - resolved.config.plugins?.find( + resolved.plugins?.find( (plugin: Plugin) => typeof plugin === 'object' && plugin?.name === 'vite:vue' ) 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 042aacd371665..ea88362972fad 100644 --- a/packages/vite/src/executors/dev-server/dev-server.impl.ts +++ b/packages/vite/src/executors/dev-server/dev-server.impl.ts @@ -23,7 +23,7 @@ export async function* viteDevServerExecutor( ): AsyncGenerator<{ success: boolean; baseUrl: string }> { process.env.VITE_CJS_IGNORE_WARNING = 'true'; // Allows ESM to be required in CJS modules. Vite will be published as ESM in the future. - const { mergeConfig, createServer, loadConfigFromFile } = + const { mergeConfig, createServer, resolveConfig } = await loadViteDynamicImport(); const projectRoot = @@ -56,12 +56,13 @@ export async function* viteDevServerExecutor( buildOptions, otherOptionsFromBuild ); - const resolved = await loadConfigFromFile( + const resolved = await resolveConfig( { + configFile: viteConfigPath, mode: otherOptions?.mode ?? buildTargetOptions?.['mode'] ?? 'development', - command: 'serve', }, - viteConfigPath + 'serve', + otherOptions?.mode ?? buildTargetOptions?.['mode'] ?? 'development' ); // vite InlineConfig @@ -69,7 +70,7 @@ export async function* viteDevServerExecutor( { // This should not be needed as it's going to be set in vite.config.ts // but leaving it here in case someone did not migrate correctly - root: resolved.config.root ?? root, + root: resolved.root ?? root, configFile: viteConfigPath, }, { @@ -107,6 +108,7 @@ export async function* viteDevServerExecutor( process.once('exit', () => resolve()); }); } + // vite ViteDevServer async function runViteDevServer(server: Record): Promise { await server.listen(); 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 c6957cf47196a..efcafc857ca04 100644 --- a/packages/vite/src/executors/preview-server/preview-server.impl.ts +++ b/packages/vite/src/executors/preview-server/preview-server.impl.ts @@ -22,8 +22,7 @@ export async function* vitePreviewServerExecutor( ) { process.env.VITE_CJS_IGNORE_WARNING = 'true'; // Allows ESM to be required in CJS modules. Vite will be published as ESM in the future. - const { mergeConfig, preview, loadConfigFromFile } = - await loadViteDynamicImport(); + const { mergeConfig, preview, resolveConfig } = await loadViteDynamicImport(); const projectRoot = context.projectsConfigurations.projects[context.projectName].root; const target = parseTargetString(options.buildTarget, context); @@ -61,12 +60,13 @@ export async function* vitePreviewServerExecutor( configuration, otherOptionsFromBuild ); - const resolved = await loadConfigFromFile( + const resolved = await resolveConfig( { + configFile: viteConfigPath, mode: otherOptions?.mode ?? otherOptionsFromBuild?.mode ?? 'production', - command: 'build', }, - viteConfigPath + 'build', + otherOptions?.mode ?? otherOptionsFromBuild?.mode ?? 'production' ); const outDir = @@ -75,7 +75,7 @@ export async function* vitePreviewServerExecutor( offsetFromRoot(projectRoot), buildTargetOptions.outputPath ) ?? - resolved?.config?.build?.outDir; + resolved?.build?.outDir; if (!outDir) { throw new Error( @@ -108,7 +108,7 @@ export async function* vitePreviewServerExecutor( { // This should not be needed as it's going to be set in vite.config.ts // but leaving it here in case someone did not migrate correctly - root: resolved.config.root ?? root, + root: resolved.root ?? root, configFile: viteConfigPath, }, {