From 040ca9f2fdfb88c50107186bd93d4deb32f251c7 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 3 Jun 2022 21:39:58 +0800 Subject: [PATCH] feat: new `ssr.fileExtension` option --- packages/vite/src/node/build.ts | 8 ++++---- packages/vite/src/node/config.ts | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 705465400ca5ca..9f22e54d9d5b61 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -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' @@ -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 @@ -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' diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index d80f5a60583abc..67875e068f44e7 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -228,6 +228,8 @@ export interface ExperimentalOptions { importGlobRestoreExtension?: boolean } +export type JsExt = 'js' | 'cjs' | 'mjs' + export type SSRTarget = 'node' | 'webworker' export type SSRFormat = 'esm' | 'cjs' @@ -238,7 +240,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 /** @@ -249,6 +251,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 ResolveWorkerOptions {