Skip to content

Commit

Permalink
chore: resolve ssr options (#8455)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 3, 2022
1 parent e992594 commit d97e402
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
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
}
}

0 comments on commit d97e402

Please sign in to comment.