vite-plugin-build-chunk
Generate additional build artifacts (e.g., chunk files in different formats) after Vite's main build process. Ideal for scenarios requiring multi-format outputs or secondary builds.
Installation
bash
npm add vite-plugin-build-chunk
bash
pnpm add vite-plugin-build-chunk
bash
yarn add vite-plugin-build-chunk
Usage
Configure the plugin in vite.config.ts
:
typescript
import buildChunk from 'vite-plugin-build-chunk';
export default {
plugins: [
buildChunk({
build: [ // The `build` property must be configured
{
chunk: 'main.mjs', // Entry file path (relative to project root)
format: 'umd', // Build format (default: 'umd')
name: 'MyLib', // Global variable name (only applies to UMD/IIFE formats)
},
// Add more configurations here
],
}),
],
};
fileName Examples
typescript
// Default template (e.g., 'es' → 'main.mjs', 'cjs' → 'main.js', 'umd' → 'main.umd.js')
fileName: undefined,
// Custom template string (must include `[name]` and `[format]`)
fileName: '[name]',
// Dynamic generation via function
fileName: (format, entryName) => {
return `${entryName}-${format}.js`;
}
Notes
- Configuration Structure: The
build
property must be provided as an array or object. Example:typescriptbuildChunk({ build: [...] })
- Entry Path:
chunk
must be an absolute path or relative to the project root. - Multi-Task Support: Use the
build
array to configure multiple tasks, each generating independent artifacts. - Silent Mode: The plugin defaults to
logLevel: 'OFF'
to avoid cluttering the main build output. - Format Default:
format
defaults toumd
if unspecified.
Example Output
For the configuration:
typescript
{
build: [{
chunk: 'main.mjs',
name: 'MyLib',
format: 'umd'
}]
}
The generated file path might be:
dist/main.umd.js