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

chore: resolve ssr options #8455

Merged
merged 1 commit into from Jun 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 5 additions & 23 deletions packages/vite/src/node/config.ts
Expand Up @@ -41,6 +41,8 @@ import type { PluginContainer } from './server/pluginContainer'
import { createPluginContainer } from './server/pluginContainer'
import type { PackageCache } from './packages'
import { loadEnv, resolveEnvPrefix } from './env'
import type { ResolvedSSROptions, SSROptions } from './ssr'
import { resolveSSROptions } from './ssr'

const debug = createDebugger('vite:config')

Expand Down Expand Up @@ -228,29 +230,6 @@ export interface ExperimentalOptions {
importGlobRestoreExtension?: boolean
}

export type SSRTarget = 'node' | 'webworker'

export type SSRFormat = 'esm' | 'cjs'

export interface SSROptions {
external?: string[]
noExternal?: string | RegExp | (string | RegExp)[] | true
/**
* Define the target for the ssr build. The browser field in package.json
* is ignored for node but used if webworker is the target
* Default: 'node'
*/
target?: SSRTarget
/**
* Define the format for the ssr build. Since Vite v3 the SSR build generates ESM by default.
* `'cjs'` can be selected to generate a CJS build, but it isn't recommended. This option is
* left marked as experimental to give users more time to update to ESM. CJS builds requires
* complex externalization heuristics that aren't present in the ESM format.
* @experimental
*/
format?: SSRFormat
}

export interface ResolveWorkerOptions {
format: 'es' | 'iife'
plugins: Plugin[]
Expand Down Expand Up @@ -285,6 +264,7 @@ export type ResolvedConfig = Readonly<
server: ResolvedServerOptions
build: ResolvedBuildOptions
preview: ResolvedPreviewOptions
ssr: ResolvedSSROptions | undefined
assetsInclude: (file: string) => boolean
logger: Logger
createResolver: (options?: Partial<InternalResolveOptions>) => ResolveFn
Expand Down Expand Up @@ -491,6 +471,7 @@ export async function resolveConfig(
: ''

const server = resolveServerOptions(resolvedRoot, config.server, logger)
const ssr = resolveSSROptions(config.ssr)

const optimizeDeps = config.optimizeDeps || {}

Expand All @@ -508,6 +489,7 @@ export async function resolveConfig(
cacheDir,
command,
mode,
ssr,
isWorker: false,
mainConfig: null,
isProduction,
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/index.ts
Expand Up @@ -39,6 +39,12 @@ export type {
DepsOptimizer,
ExportsData
} from './optimizer'
export type {
ResolvedSSROptions,
SSROptions,
SSRFormat,
SSRTarget
} from './ssr'
export type { Plugin } from './plugin'
export type { PackageCache, PackageData } from './packages'
export type {
Expand Down
39 changes: 39 additions & 0 deletions packages/vite/src/node/ssr/index.ts
@@ -0,0 +1,39 @@
export type SSRTarget = 'node' | 'webworker'
export type SSRFormat = 'esm' | 'cjs'

export interface SSROptions {
external?: string[]
noExternal?: string | RegExp | (string | RegExp)[] | true
/**
* Define the target for the ssr build. The browser field in package.json
* is ignored for node but used if webworker is the target
* Default: 'node'
*/
target?: SSRTarget
/**
* Define the format for the ssr build. Since Vite v3 the SSR build generates ESM by default.
* `'cjs'` can be selected to generate a CJS build, but it isn't recommended. This option is
* left marked as experimental to give users more time to update to ESM. CJS builds requires
* complex externalization heuristics that aren't present in the ESM format.
* @experimental
*/
format?: SSRFormat
}

export interface ResolvedSSROptions extends SSROptions {
target: SSRTarget
format: SSRFormat
}

export function resolveSSROptions(
ssr: SSROptions | undefined
): ResolvedSSROptions | undefined {
if (ssr === undefined) {
return undefined
}
return {
format: 'esm',
target: 'node',
...ssr
}
}