From 554fda50b49479467b95f7be50cb6f06a800556a Mon Sep 17 00:00:00 2001 From: Etienne Gobeli Date: Tue, 1 Jun 2021 15:40:29 +0200 Subject: [PATCH 1/5] 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 c39ef603fa861e..fcf3fcbea9b9b4 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' @@ -198,7 +199,7 @@ export interface LibraryOptions { entry: string name?: string formats?: LibraryFormats[] - fileName?: string + fileName?: string | ((format?: ModuleFormat) => string) } export type LibraryFormats = 'es' | 'cjs' | 'umd' | 'iife' @@ -425,7 +426,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` From cec700e1ae7ad4487f7c7073c1688e642b08d46c Mon Sep 17 00:00:00 2001 From: Etienne Gobeli Date: Wed, 2 Jun 2021 08:41:28 +0200 Subject: [PATCH 2/5] feat(vite): rewrite lib test to use custom filename --- packages/playground/lib/index.dist.html | 5 ++--- packages/playground/lib/vite.config.js | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/playground/lib/index.dist.html b/packages/playground/lib/index.dist.html index 3416c1f3952fa9..eddf3006a66aec 100644 --- a/packages/playground/lib/index.dist.html +++ b/packages/playground/lib/index.dist.html @@ -1,15 +1,14 @@ -
- + diff --git a/packages/playground/lib/vite.config.js b/packages/playground/lib/vite.config.js index a80800f6ad3d58..2545d488e165ad 100644 --- a/packages/playground/lib/vite.config.js +++ b/packages/playground/lib/vite.config.js @@ -8,7 +8,8 @@ module.exports = { build: { lib: { entry: path.resolve(__dirname, 'src/main.js'), - name: 'MyLib' + name: 'MyLib', + fileName: (format) => `my-lib-custom-filename.${format}.js` } }, plugins: [ From fdf067faae323a05981bd3b146f17f7a3d27632c Mon Sep 17 00:00:00 2001 From: Etienne Gobeli Date: Wed, 2 Jun 2021 13:09:58 +0200 Subject: [PATCH 3/5] feat(vite): refactor and add unit test for resolveLibFilename --- .../vite/src/node/__tests__/build.spec.ts | 32 +++++++++++++++++++ packages/vite/src/node/build.ts | 14 ++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 packages/vite/src/node/__tests__/build.spec.ts diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts new file mode 100644 index 00000000000000..15bca18c91bfc4 --- /dev/null +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -0,0 +1,32 @@ +import { resolveLibFilename } from '../build' + +describe('resolveLibFilename', () => { + test('custom filename function', () => { + const filename = resolveLibFilename( + { + fileName: (format) => `custom-filename-function.${format}.js`, + entry: 'mylib.js' + }, + 'es', + 'mylib' + ) + + expect(filename).toBe('custom-filename-function.es.js') + }) + + test('custom filename string', () => { + const filename = resolveLibFilename( + { fileName: 'custom-filename', entry: 'mylib.js' }, + 'es', + 'mylib' + ) + + expect(filename).toBe('custom-filename.es.js') + }) + + test('package name as filename', () => { + const filename = resolveLibFilename({ entry: 'mylib.js' }, 'es', 'mylib') + + expect(filename).toBe('mylib.es.js') + }) +}) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index fcf3fcbea9b9b4..7bad8ffb7d9fdf 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -426,9 +426,7 @@ async function doBuild( entryFileNames: ssr ? `[name].js` : libOptions - ? typeof libOptions.fileName === 'function' - ? libOptions.fileName(output.format) - : `${libOptions.fileName || pkgName}.${output.format || `es`}.js` + ? resolveLibFilename(libOptions, output.format, pkgName) : path.posix.join(options.assetsDir, `[name].[hash].js`), chunkFileNames: libOptions ? `[name].js` @@ -624,6 +622,16 @@ function staticImportedByEntry( return someImporterIs } +export function resolveLibFilename( + libOptions: LibraryOptions, + format: ModuleFormat | undefined, + pkgName: string +): string { + return typeof libOptions.fileName === 'function' + ? libOptions.fileName(format) + : `${libOptions.fileName || pkgName}.${format || `es`}.js` +} + function resolveBuildOutputs( outputs: OutputOptions | OutputOptions[] | undefined, libOptions: LibraryOptions | false, From 5f525ab50f3d3cfe7a36fdeb4e6970c6f3202499 Mon Sep 17 00:00:00 2001 From: Etienne Gobeli Date: Mon, 14 Jun 2021 08:35:45 +0200 Subject: [PATCH 4/5] feat(vite): update LibraryOptions and add docs --- docs/config/index.md | 4 ++-- packages/vite/src/node/build.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 356e9b37831e32..e551c7d562b2be 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -621,10 +621,10 @@ createServer() ### build.lib -- **Type:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string }` +- **Type:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat) => 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 + 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` as an argument. ### build.manifest diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 7bad8ffb7d9fdf..16a3322de13ddb 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -199,7 +199,7 @@ export interface LibraryOptions { entry: string name?: string formats?: LibraryFormats[] - fileName?: string | ((format?: ModuleFormat) => string) + fileName?: string | ((format: ModuleFormat) => string) } export type LibraryFormats = 'es' | 'cjs' | 'umd' | 'iife' @@ -627,7 +627,7 @@ export function resolveLibFilename( format: ModuleFormat | undefined, pkgName: string ): string { - return typeof libOptions.fileName === 'function' + return typeof libOptions.fileName === 'function' && format ? libOptions.fileName(format) : `${libOptions.fileName || pkgName}.${format || `es`}.js` } From 7ed0f5df92b210ec9a993af44088360bd50d09b2 Mon Sep 17 00:00:00 2001 From: Etienne Gobeli Date: Tue, 29 Jun 2021 19:47:53 +0200 Subject: [PATCH 5/5] feat(vite): set default format in buildOutputOptions --- packages/vite/src/node/build.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 16a3322de13ddb..f19192c0bc0404 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -426,7 +426,7 @@ async function doBuild( entryFileNames: ssr ? `[name].js` : libOptions - ? resolveLibFilename(libOptions, output.format, pkgName) + ? resolveLibFilename(libOptions, output.format || 'es', pkgName) : path.posix.join(options.assetsDir, `[name].[hash].js`), chunkFileNames: libOptions ? `[name].js` @@ -624,12 +624,12 @@ function staticImportedByEntry( export function resolveLibFilename( libOptions: LibraryOptions, - format: ModuleFormat | undefined, + format: ModuleFormat, pkgName: string ): string { - return typeof libOptions.fileName === 'function' && format + return typeof libOptions.fileName === 'function' ? libOptions.fileName(format) - : `${libOptions.fileName || pkgName}.${format || `es`}.js` + : `${libOptions.fileName || pkgName}.${format}.js` } function resolveBuildOutputs(