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): only dynamically import vite #20774

Merged
merged 1 commit into from
Dec 15, 2023
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
6 changes: 3 additions & 3 deletions packages/vite/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
createBuildableTsConfig,
validateTypes,
} from '../../utils/executor-utils';
import { BuildOptions } from 'vite';

export async function* viteBuildExecutor(
options: Record<string, any> & ViteBuildExecutorOptions,
Expand Down Expand Up @@ -192,7 +191,8 @@ export async function* viteBuildExecutor(
export async function getBuildExtraArgs(
options: ViteBuildExecutorOptions
): Promise<{
buildOptions: BuildOptions;
// vite BuildOptions
buildOptions: Record<string, unknown>;
otherOptions: Record<string, any>;
}> {
// support passing extra args to vite cli
Expand All @@ -204,7 +204,7 @@ export async function getBuildExtraArgs(
}
}

const buildOptions = {} as BuildOptions;
const buildOptions = {};
const buildSchemaKeys = [
'target',
'polyfillModulePreload',
Expand Down
22 changes: 8 additions & 14 deletions packages/vite/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<typeof import('vite')>);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -96,8 +89,8 @@ export async function* viteDevServerExecutor(
process.once('exit', () => resolve());
});
}

async function runViteDevServer(server: ViteDevServer): Promise<void> {
// vite ViteDevServer
async function runViteDevServer(server: Record<string, any>): Promise<void> {
mandarini marked this conversation as resolved.
Show resolved Hide resolved
await server.listen();

server.printUrls();
Expand All @@ -116,7 +109,8 @@ export default viteDevServerExecutor;
async function getServerExtraArgs(
options: ViteDevServerExecutorOptions
): Promise<{
serverOptions: ServerOptions;
// vite ServerOptions
serverOptions: Record<string, unknown>;
otherOptions: Record<string, any>;
}> {
// support passing extra args to vite cli
Expand All @@ -128,7 +122,7 @@ async function getServerExtraArgs(
}
}

const serverOptions = {} as ServerOptions;
const serverOptions = {};
const serverSchemaKeys = [
'hmr',
'warmup',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
parseTargetString,
runExecutor,
} from '@nx/devkit';
import type { InlineConfig, PreviewOptions, PreviewServer } from 'vite';
import {
getNxTargetOptions,
getProxyConfig,
Expand Down Expand Up @@ -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
Expand All @@ -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<string, any> | undefined;

const processOnExit = async () => {
await closeServer(server);
Expand Down Expand Up @@ -168,7 +169,7 @@ export async function* vitePreviewServerExecutor(
});
}

function closeServer(server?: PreviewServer): Promise<void> {
function closeServer(server?: Record<string, any>): Promise<void> {
mandarini marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve) => {
if (!server) {
resolve();
Expand All @@ -191,7 +192,8 @@ export default vitePreviewServerExecutor;
async function getExtraArgs(
options: VitePreviewServerExecutorOptions
): Promise<{
previewOptions: PreviewOptions;
// vite PreviewOptions
previewOptions: Record<string, any>;
otherOptions: Record<string, any>;
}> {
// support passing extra args to vite cli
Expand All @@ -203,7 +205,7 @@ async function getExtraArgs(
}
}

const previewOptions = {} as PreviewOptions;
const previewOptions = {};
const previewSchemaKeys = [
'port',
'strictPort',
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/utils/options-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -89,14 +88,15 @@ export function getViteServerProxyConfigPath(
export async function getViteServerOptions(
options: ViteDevServerExecutorOptions,
context: ExecutorContext
): Promise<ServerOptions> {
): Promise<Record<string, unknown>> {
// 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<typeof import('vite')>);
const projectRoot =
context.projectsConfigurations.projects[context.projectName].root;
const serverOptions: ServerOptions = {
const serverOptions: Record<string, unknown> = {
fs: {
allow: [
searchForWorkspaceRoot(joinPathFragments(projectRoot)),
Expand All @@ -120,7 +120,7 @@ export async function getViteServerOptions(
export function getProxyConfig(
context: ExecutorContext,
proxyConfig?: string
): Record<string, string | ProxyOptions> | undefined {
): Record<string, string | unknown> | undefined {
const proxyConfigPath = getViteServerProxyConfigPath(proxyConfig, context);
if (proxyConfigPath) {
logger.info(`Loading proxy configuration from: ${proxyConfigPath}`);
Expand Down