From 9b0eedb308ff808a45736e258a1731bdcd4ef238 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Thu, 14 Dec 2023 20:49:43 +0200 Subject: [PATCH] fix(vite): only dynamically import vite --- .../vite/src/executors/build/build.impl.ts | 6 ++--- .../executors/dev-server/dev-server.impl.ts | 22 +++++++------------ .../preview-server/preview-server.impl.ts | 14 +++++++----- packages/vite/src/utils/options-utils.ts | 8 +++---- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/packages/vite/src/executors/build/build.impl.ts b/packages/vite/src/executors/build/build.impl.ts index 6e73ae39b76a46..5db2776d64915d 100644 --- a/packages/vite/src/executors/build/build.impl.ts +++ b/packages/vite/src/executors/build/build.impl.ts @@ -25,7 +25,6 @@ import { createBuildableTsConfig, validateTypes, } from '../../utils/executor-utils'; -import { BuildOptions } from 'vite'; export async function* viteBuildExecutor( options: Record & ViteBuildExecutorOptions, @@ -192,7 +191,8 @@ export async function* viteBuildExecutor( export async function getBuildExtraArgs( options: ViteBuildExecutorOptions ): Promise<{ - buildOptions: BuildOptions; + // vite BuildOptions + buildOptions: Record; otherOptions: Record; }> { // support passing extra args to vite cli @@ -204,7 +204,7 @@ export async function getBuildExtraArgs( } } - const buildOptions = {} as BuildOptions; + const buildOptions = {}; const buildSchemaKeys = [ 'target', 'polyfillModulePreload', 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 6dcae0d056c06a..75f9ac9c033c5a 100644 --- a/packages/vite/src/executors/dev-server/dev-server.impl.ts +++ b/packages/vite/src/executors/dev-server/dev-server.impl.ts @@ -1,17 +1,9 @@ import { ExecutorContext, joinPathFragments } from '@nx/devkit'; -import { - loadConfigFromFile, - type InlineConfig, - type ViteDevServer, - ServerOptions, -} from 'vite'; - import { getNxTargetOptions, getViteServerOptions, normalizeViteConfigFilePath, } from '../../utils/options-utils'; - import { ViteDevServerExecutorOptions } from './schema'; import { ViteBuildExecutorOptions } from '../build/schema'; import { createBuildableTsConfig } from '../../utils/executor-utils'; @@ -23,7 +15,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 } = await (Function( + const { mergeConfig, createServer, loadConfigFromFile } = await (Function( 'return import("vite")' )() as Promise); @@ -54,7 +46,8 @@ export async function* viteDevServerExecutor( viteConfigPath ); - const serverConfig: InlineConfig = mergeConfig( + // vite InlineConfig + const serverConfig = 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 @@ -96,8 +89,8 @@ export async function* viteDevServerExecutor( process.once('exit', () => resolve()); }); } - -async function runViteDevServer(server: ViteDevServer): Promise { +// vite ViteDevServer +async function runViteDevServer(server: Record): Promise { await server.listen(); server.printUrls(); @@ -116,7 +109,8 @@ export default viteDevServerExecutor; async function getServerExtraArgs( options: ViteDevServerExecutorOptions ): Promise<{ - serverOptions: ServerOptions; + // vite ServerOptions + serverOptions: Record; otherOptions: Record; }> { // support passing extra args to vite cli @@ -128,7 +122,7 @@ async function getServerExtraArgs( } } - const serverOptions = {} as ServerOptions; + const serverOptions = {}; const serverSchemaKeys = [ 'hmr', 'warmup', 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 8fd9b6915ab0ce..085b2d212c10ff 100644 --- a/packages/vite/src/executors/preview-server/preview-server.impl.ts +++ b/packages/vite/src/executors/preview-server/preview-server.impl.ts @@ -5,7 +5,6 @@ import { parseTargetString, runExecutor, } from '@nx/devkit'; -import type { InlineConfig, PreviewOptions, PreviewServer } from 'vite'; import { getNxTargetOptions, getProxyConfig, @@ -98,7 +97,8 @@ export async function* vitePreviewServerExecutor( }, }; - const serverConfig: InlineConfig = mergeConfig( + // vite InlineConfig + const serverConfig = 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 @@ -114,7 +114,8 @@ export async function* vitePreviewServerExecutor( console.warn('WARNING: preview is not meant to be run in production!'); } - let server: PreviewServer | undefined; + // vite PreviewServer + let server: Record | undefined; const processOnExit = async () => { await closeServer(server); @@ -168,7 +169,7 @@ export async function* vitePreviewServerExecutor( }); } -function closeServer(server?: PreviewServer): Promise { +function closeServer(server?: Record): Promise { return new Promise((resolve) => { if (!server) { resolve(); @@ -191,7 +192,8 @@ export default vitePreviewServerExecutor; async function getExtraArgs( options: VitePreviewServerExecutorOptions ): Promise<{ - previewOptions: PreviewOptions; + // vite PreviewOptions + previewOptions: Record; otherOptions: Record; }> { // support passing extra args to vite cli @@ -203,7 +205,7 @@ async function getExtraArgs( } } - const previewOptions = {} as PreviewOptions; + const previewOptions = {}; const previewSchemaKeys = [ 'port', 'strictPort', diff --git a/packages/vite/src/utils/options-utils.ts b/packages/vite/src/utils/options-utils.ts index 8302f36413afae..eec41fbab86268 100644 --- a/packages/vite/src/utils/options-utils.ts +++ b/packages/vite/src/utils/options-utils.ts @@ -6,7 +6,6 @@ import { readTargetOptions, } from '@nx/devkit'; import { existsSync } from 'fs'; -import { ProxyOptions, ServerOptions } from 'vite'; import { ViteDevServerExecutorOptions } from '../executors/dev-server/schema'; /** @@ -89,14 +88,15 @@ export function getViteServerProxyConfigPath( export async function getViteServerOptions( options: ViteDevServerExecutorOptions, context: ExecutorContext -): Promise { +): Promise> { + // returns vite ServerOptions // Allows ESM to be required in CJS modules. Vite will be published as ESM in the future. const { searchForWorkspaceRoot } = await (Function( 'return import("vite")' )() as Promise); const projectRoot = context.projectsConfigurations.projects[context.projectName].root; - const serverOptions: ServerOptions = { + const serverOptions: Record = { fs: { allow: [ searchForWorkspaceRoot(joinPathFragments(projectRoot)), @@ -120,7 +120,7 @@ export async function getViteServerOptions( export function getProxyConfig( context: ExecutorContext, proxyConfig?: string -): Record | undefined { +): Record | undefined { const proxyConfigPath = getViteServerProxyConfigPath(proxyConfig, context); if (proxyConfigPath) { logger.info(`Loading proxy configuration from: ${proxyConfigPath}`);