From 0f61f77d69c49552668bb5299a6cd04f5a00664a Mon Sep 17 00:00:00 2001 From: "Grigorii K. Shartsev" Date: Mon, 9 Oct 2023 14:10:03 +0200 Subject: [PATCH] feat: add option to customize asset file names Signed-off-by: Grigorii K. Shartsev --- lib/appConfig.ts | 8 ++++++++ lib/baseConfig.ts | 11 ++++++++++- lib/libConfig.ts | 8 ++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/appConfig.ts b/lib/appConfig.ts index 15488bf..b07648d 100644 --- a/lib/appConfig.ts +++ b/lib/appConfig.ts @@ -124,6 +124,14 @@ export const createAppConfig = (entries: { [entryAlias: string]: string }, optio // global variables for appName and appVersion intro: `const appName = ${JSON.stringify(appName)}; const appVersion = ${JSON.stringify(appVersion)};`, assetFileNames: (assetInfo) => { + // Allow to customize the asset file names + if (options.assetFileNames) { + const customName = options.assetFileNames(assetInfo) + if (customName) { + return customName + } + } + const extType = assetInfo.name.split('.').pop() if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) { return 'img/[name][extname]' diff --git a/lib/baseConfig.ts b/lib/baseConfig.ts index 5837139..0283df3 100644 --- a/lib/baseConfig.ts +++ b/lib/baseConfig.ts @@ -8,7 +8,7 @@ import { readFileSync } from 'node:fs' import { type CoreJSPluginOptions, corejsPlugin } from 'rollup-plugin-corejs' import { minify as minifyPlugin } from 'rollup-plugin-esbuild-minify/lib/index.js' import { nodePolyfills } from 'vite-plugin-node-polyfills' -import { defineConfig, mergeConfig, type UserConfigExport, type UserConfigFn } from 'vite' +import { defineConfig, mergeConfig, type UserConfigExport, type UserConfigFn, type Rollup } from 'vite' import { RemoveEnsureWatchPlugin } from './plugins/RemoveEnsureWatch.js' import replace from '@rollup/plugin-replace' @@ -49,6 +49,15 @@ export interface BaseOptions { * @default 'dist/vendor.LICENSE.txt' */ thirdPartyLicense?: false | string + /** + * Customize the asset file names. + * Similar to `output.assetFileNames` in rollup config, + * but if returns undefined, then this config defaults is be used. + * + * @example Move CSS styles to `styles/style.css` instead of the default `css/[entrypoint-name].css`: + * (chunkInfo) => chunkInfo.name.endsWith('.css') ? 'styles/style.css' : undefined + */ + assetFileNames?: (chunkInfo: Rollup.PreRenderedAsset) => 'string' | undefined, /** * Vite config to override or extend the base config */ diff --git a/lib/libConfig.ts b/lib/libConfig.ts index 1db3446..bbd17b0 100644 --- a/lib/libConfig.ts +++ b/lib/libConfig.ts @@ -90,6 +90,14 @@ export const createLibConfig = (entries: { [entryAlias: string]: string }, optio const userConfig = await Promise.resolve(typeof options.config === 'function' ? options.config(env) : options.config) const assetFileNames = (assetInfo) => { + // Allow to customize the asset file names + if (options.assetFileNames) { + const customName = options.assetFileNames(assetInfo) + if (customName) { + return customName + } + } + const extType = assetInfo.name.split('.').pop() if (!options.inlineCSS && /css/i.test(extType)) { return '[name].css'