diff --git a/docs/guide/build.md b/docs/guide/build.md index e2e18b2f5b5798..d45fe5a24168e8 100644 --- a/docs/guide/build.md +++ b/docs/guide/build.md @@ -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 diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 276f47f2fa0301..65eae08f5d5cae 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -15,7 +15,8 @@ import Rollup, { GetModuleInfo, WatcherOptions, RollupWatcher, - RollupError + RollupError, + ModuleFormat } from 'rollup' import { buildReporterPlugin } from './plugins/reporter' import { buildHtmlPlugin } from './plugins/html' @@ -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' @@ -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`