Skip to content

Commit

Permalink
feat(vite): enable usage of function as library fileName (#3585)
Browse files Browse the repository at this point in the history
  • Loading branch information
gobeli committed Jun 1, 2021
1 parent 82b2bce commit 1ce0557
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/guide/build.md
Expand Up @@ -108,7 +108,8 @@ module.exports = {
build: {
lib: {
entry: path.resolve(__dirname, 'lib/main.js'),
name: 'MyLib'
name: 'MyLib',
fileName: format => `my-lib.${format}.js`
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
Expand Down
9 changes: 6 additions & 3 deletions packages/vite/src/node/build.ts
Expand Up @@ -15,7 +15,8 @@ import Rollup, {
GetModuleInfo,
WatcherOptions,
RollupWatcher,
RollupError
RollupError,
ModuleFormat
} from 'rollup'
import { buildReporterPlugin } from './plugins/reporter'
import { buildHtmlPlugin } from './plugins/html'
Expand Down Expand Up @@ -189,7 +190,7 @@ export interface LibraryOptions {
entry: string
name?: string
formats?: LibraryFormats[]
fileName?: string
fileName?: string | ((format?: ModuleFormat) => string)
}

export type LibraryFormats = 'es' | 'cjs' | 'umd' | 'iife'
Expand Down Expand Up @@ -414,7 +415,9 @@ async function doBuild(
entryFileNames: ssr
? `[name].js`
: libOptions
? `${libOptions.fileName || pkgName}.${output.format || `es`}.js`
? typeof libOptions.fileName === 'function'
? libOptions.fileName(output.format)
: `${libOptions.fileName || pkgName}.${output.format || `es`}.js`
: path.posix.join(options.assetsDir, `[name].[hash].js`),
chunkFileNames: libOptions
? `[name].js`
Expand Down

0 comments on commit 1ce0557

Please sign in to comment.