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

feat: new ssr.fileExtension option #8457

Closed
wants to merge 2 commits into from
Closed
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: 6 additions & 0 deletions docs/config/ssr-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ Build target for the SSR server.
- **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.

## ssr.fileExtension

- **Type:** `'js' | 'cjs' | 'mjs'`

File extension of generated bundles. When not specified, it will infer from root package.json's "type" field.
8 changes: 4 additions & 4 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import commonjsPlugin from '@rollup/plugin-commonjs'
import type { RollupCommonJSOptions } from 'types/commonjs'
import type { RollupDynamicImportVarsOptions } from 'types/dynamicImportVars'
import type { TransformOptions } from 'esbuild'
import type { InlineConfig, ResolvedConfig } from './config'
import type { InlineConfig, JsExt, ResolvedConfig } from './config'
import { isDepsOptimizerEnabled, resolveConfig } from './config'
import { buildReporterPlugin } from './plugins/reporter'
import { buildEsbuildPlugin } from './plugins/esbuild'
Expand Down Expand Up @@ -451,8 +451,10 @@ async function doBuild(
const format = output.format || (cjsSsrBuild ? 'cjs' : 'es')
const jsExt =
(ssr && config.ssr?.target !== 'webworker') || libOptions
? resolveOutputJsExtension(format, getPkgJson(config.root)?.type)
? config.ssr?.fileExtension ||
resolveOutputJsExtension(format, getPkgJson(config.root)?.type)
: 'js'

return {
dir: outDir,
// Default format is 'es' for regular and for SSR builds
Expand Down Expand Up @@ -606,8 +608,6 @@ function getPkgName(name: string) {
return name?.startsWith('@') ? name.split('/')[1] : name
}

type JsExt = 'js' | 'cjs' | 'mjs'

function resolveOutputJsExtension(
format: ModuleFormat,
type: string = 'commonjs'
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ export interface ExperimentalOptions {
importGlobRestoreExtension?: boolean
}

export type JsExt = 'js' | 'cjs' | 'mjs'

export interface ResolveWorkerOptions {
format: 'es' | 'iife'
plugins: Plugin[]
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/ssr/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { JsExt } from '..'

export type SSRTarget = 'node' | 'webworker'
export type SSRFormat = 'esm' | 'cjs'

Expand All @@ -7,7 +9,7 @@ export interface SSROptions {
/**
* 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'
* @default 'node'
*/
target?: SSRTarget
/**
Expand All @@ -18,6 +20,11 @@ export interface SSROptions {
* @experimental
*/
format?: SSRFormat
/**
* File extension of generated bundles
* Will infer from root package.json's "type" field if not specified
*/
fileExtension?: JsExt
}

export interface ResolvedSSROptions extends SSROptions {
Expand Down