Skip to content

Commit

Permalink
feat: add option to customize asset file names
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
  • Loading branch information
ShGKme authored and susnux committed Oct 18, 2023
1 parent 63c6365 commit 0f61f77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
11 changes: 10 additions & 1 deletion lib/baseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
*/
Expand Down
8 changes: 8 additions & 0 deletions lib/libConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 0f61f77

Please sign in to comment.