Skip to content

Commit

Permalink
chore: ssr.format
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed May 28, 2022
1 parent ad9f17e commit 773f234
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
10 changes: 9 additions & 1 deletion docs/config/ssr-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ Prevent listed dependencies from being externalized for SSR. If `true`, no depen

## ssr.target

- **Type:** `'node' | 'webworker' | 'node-cjs'`
- **Type:** `'node' | 'webworker'`
- **Default:** `node`

Build target for the SSR server.

## ssr.format

- **Type:** `'esm' | 'cjs'`
- **Default:** `esm`
- **Experimental**

Build format for the SSR server. Since Vite v3 the SSR build generates ESM by default. `'cjs'` can be selected to generate a CJS build, but it isn't recommended. The 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.
2 changes: 1 addition & 1 deletion docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { defineConfig } from 'vite'

export default defineConfig({
ssr: {
target: 'node-cjs'
format: 'cjs'
}
})
4 changes: 2 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ async function doBuild(
// In CJS, we can pass the externals to rollup as is. In ESM, we need to
// do it in the resolve plugin so we can add the resolved extension for
// deep node_modules imports
if (ssr && config.ssr?.target === 'node-cjs') {
if (ssr && config.ssr?.format === 'cjs') {
external = await cjsSsrResolveExternal(config, userExternal)
}

Expand Down Expand Up @@ -421,7 +421,7 @@ async function doBuild(

try {
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
const cjsSsrBuild = ssr && config.ssr?.target === 'node-cjs'
const cjsSsrBuild = ssr && config.ssr?.format === 'cjs'
return {
dir: outDir,
// Default format is 'es' for regular and for SSR builds
Expand Down
12 changes: 11 additions & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ export interface ExperimentalOptions {
importGlobRestoreExtension?: boolean
}

export type SSRTarget = 'node' | 'webworker' | 'node-cjs'
export type SSRTarget = 'node' | 'webworker'

export type SSRFormat = 'esm' | 'cjs'

export interface SSROptions {
external?: string[]
Expand All @@ -239,6 +241,14 @@ export interface SSROptions {
* 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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}
// skip ssr external
if (ssr) {
if (config.ssr?.target === 'node-cjs') {
if (config.ssr?.format === 'cjs') {
if (cjsShouldExternalizeForSSR(specifier, server._ssrExternals)) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function resolvePlugins(
asSrc: true,
getDepsOptimizer: () => getDepsOptimizer(config),
shouldExternalize:
isBuild && config.build.ssr && config.ssr?.target !== 'node-cjs'
isBuild && config.build.ssr && config.ssr?.format !== 'cjs'
? (id) => shouldExternalizeForSSR(id, config)
: undefined
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/ssrRequireHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function ssrRequireHookPlugin(config: ResolvedConfig): Plugin | null {
!config.build.ssr ||
!config.resolve.dedupe?.length ||
config.ssr?.noExternal === true ||
config.ssr?.target !== 'node-cjs' ||
config.ssr?.format !== 'cjs' ||
isBuildOutputEsm(config)
) {
return null
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ function createIsSsrExternal(
}
}

// When ssr.format is 'node-cjs', this function is used reverting to the Vite 2.9 era
// SSR externalize heuristics
// When ssr.format is 'cjs', this function is used reverting to the Vite 2.9
// SSR externalization heuristics
function cjsSsrCollectExternals(
root: string,
preserveSymlinks: boolean | undefined,
Expand Down

0 comments on commit 773f234

Please sign in to comment.