From 1ce0557a8feb749922a9858862bebc1b8f06e708 Mon Sep 17 00:00:00 2001 From: Etienne Gobeli Date: Tue, 1 Jun 2021 15:40:29 +0200 Subject: [PATCH] feat(vite): enable usage of function as library fileName (#3585) --- docs/guide/build.md | 3 ++- packages/vite/src/node/build.ts | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) 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`