Skip to content

Commit

Permalink
feat(lib): cjs instead of umd as default format for multiple entries (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar authored Oct 10, 2022
1 parent 87b48f9 commit 07d3fbd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Options to pass on to [@rollup/plugin-dynamic-import-vars](https://github.com/ro
- **Type:** `{ entry: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string) }`
- **Related:** [Library Mode](/guide/build#library-mode)
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.
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.
## build.manifest
Expand Down
26 changes: 25 additions & 1 deletion packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { Logger } from 'vite'
import { describe, expect, test } from 'vitest'
import type { LibraryFormats, LibraryOptions } from '../build'
import { resolveLibFilename } from '../build'
import { resolveBuildOutputs, resolveLibFilename } from '../build'

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

Expand Down Expand Up @@ -244,3 +245,26 @@ describe('resolveLibFilename', () => {
expect(fileName2).toBe('custom-filename.mjs')
})
})

describe('resolveBuildOutputs', () => {
test('default format: one entry', () => {
const libOptions: LibraryOptions = {
entry: 'entryA.js',
name: 'entryA'
}

const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)

expect(outputs).toEqual([{ format: 'es' }, { format: 'umd' }])
})

test('default format: multiple entries', () => {
const libOptions: LibraryOptions = {
entry: ['entryA.js', 'entryB.js']
}

const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)

expect(outputs).toEqual([{ format: 'es' }, { format: 'cjs' }])
})
})
15 changes: 9 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,18 +746,21 @@ export function resolveLibFilename(
return `${name}.${format}.${extension}`
}

function resolveBuildOutputs(
export function resolveBuildOutputs(
outputs: OutputOptions | OutputOptions[] | undefined,
libOptions: LibraryOptions | false,
logger: Logger
): OutputOptions | OutputOptions[] | undefined {
if (libOptions) {
const formats = libOptions.formats || ['es', 'umd']
const hasMultipleEntries =
typeof libOptions.entry !== 'string' &&
Object.values(libOptions.entry).length > 1

const formats =
libOptions.formats || (hasMultipleEntries ? ['es', 'cjs'] : ['es', 'umd'])

if (formats.includes('umd') || formats.includes('iife')) {
if (
typeof libOptions.entry !== 'string' &&
Object.values(libOptions.entry).length > 1
) {
if (hasMultipleEntries) {
throw new Error(
`Multiple entry points are not supported when output formats include "umd" or "iife".`
)
Expand Down

0 comments on commit 07d3fbd

Please sign in to comment.