Skip to content

Commit 07d3fbd

Browse files
authored
feat(lib): cjs instead of umd as default format for multiple entries (#10315)
1 parent 87b48f9 commit 07d3fbd

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

docs/config/build-options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Options to pass on to [@rollup/plugin-dynamic-import-vars](https://github.com/ro
148148
- **Type:** `{ entry: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string) }`
149149
- **Related:** [Library Mode](/guide/build#library-mode)
150150
151-
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`. `fileName` is the name of the package file output, default `fileName` is the name option of package.json, it can also be defined as function taking the `format` and `entryAlias` as arguments.
151+
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`, or `['es', 'cjs']`, if multiple entries are used. `fileName` is the name of the package file output, default `fileName` is the name option of package.json, it can also be defined as function taking the `format` and `entryAlias` as arguments.
152152
153153
## build.manifest
154154

packages/vite/src/node/__tests__/build.spec.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import type { Logger } from 'vite'
34
import { describe, expect, test } from 'vitest'
45
import type { LibraryFormats, LibraryOptions } from '../build'
5-
import { resolveLibFilename } from '../build'
6+
import { resolveBuildOutputs, resolveLibFilename } from '../build'
67

78
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
89

@@ -244,3 +245,26 @@ describe('resolveLibFilename', () => {
244245
expect(fileName2).toBe('custom-filename.mjs')
245246
})
246247
})
248+
249+
describe('resolveBuildOutputs', () => {
250+
test('default format: one entry', () => {
251+
const libOptions: LibraryOptions = {
252+
entry: 'entryA.js',
253+
name: 'entryA'
254+
}
255+
256+
const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)
257+
258+
expect(outputs).toEqual([{ format: 'es' }, { format: 'umd' }])
259+
})
260+
261+
test('default format: multiple entries', () => {
262+
const libOptions: LibraryOptions = {
263+
entry: ['entryA.js', 'entryB.js']
264+
}
265+
266+
const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)
267+
268+
expect(outputs).toEqual([{ format: 'es' }, { format: 'cjs' }])
269+
})
270+
})

packages/vite/src/node/build.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -746,18 +746,21 @@ export function resolveLibFilename(
746746
return `${name}.${format}.${extension}`
747747
}
748748

749-
function resolveBuildOutputs(
749+
export function resolveBuildOutputs(
750750
outputs: OutputOptions | OutputOptions[] | undefined,
751751
libOptions: LibraryOptions | false,
752752
logger: Logger
753753
): OutputOptions | OutputOptions[] | undefined {
754754
if (libOptions) {
755-
const formats = libOptions.formats || ['es', 'umd']
755+
const hasMultipleEntries =
756+
typeof libOptions.entry !== 'string' &&
757+
Object.values(libOptions.entry).length > 1
758+
759+
const formats =
760+
libOptions.formats || (hasMultipleEntries ? ['es', 'cjs'] : ['es', 'umd'])
761+
756762
if (formats.includes('umd') || formats.includes('iife')) {
757-
if (
758-
typeof libOptions.entry !== 'string' &&
759-
Object.values(libOptions.entry).length > 1
760-
) {
763+
if (hasMultipleEntries) {
761764
throw new Error(
762765
`Multiple entry points are not supported when output formats include "umd" or "iife".`
763766
)

0 commit comments

Comments
 (0)