/
index.ts
72 lines (67 loc) · 1.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { DepOptimizationConfig } from '../optimizer'
export type SSRTarget = 'node' | 'webworker'
export type SSRFormat = 'esm' | 'cjs'
export type SsrDepOptimizationOptions = DepOptimizationConfig
export interface SSROptions {
noExternal?: string | RegExp | (string | RegExp)[] | true
external?: string[]
/**
* 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
/**
* Control over which dependencies are optimized during SSR and esbuild options
* During build:
* no external CJS dependencies are optimized by default
* During dev:
* explicit no external CJS dependencies are optimized by default
* @experimental
*/
optimizeDeps?: SsrDepOptimizationOptions
}
export interface ResolvedSSROptions extends SSROptions {
target: SSRTarget
format: SSRFormat
optimizeDeps: SsrDepOptimizationOptions
}
export function resolveSSROptions(
ssr: SSROptions | undefined,
buildSsrCjsExternalHeuristics?: boolean,
preserveSymlinks?: boolean
): ResolvedSSROptions {
ssr ??= {}
const optimizeDeps = ssr.optimizeDeps ?? {}
let format: SSRFormat = 'esm'
let target: SSRTarget = 'node'
if (buildSsrCjsExternalHeuristics) {
if (ssr) {
format = 'cjs'
} else {
target = 'node'
format = 'cjs'
}
}
return {
format,
target,
...ssr,
optimizeDeps: {
disabled: true,
...optimizeDeps,
esbuildOptions: {
preserveSymlinks,
...optimizeDeps.esbuildOptions
}
}
}
}