简介
vite-plugin-manifest-builder
是一个强大的 Vite 插件,用于动态构建和修改 Chrome 扩展的 manifest.json
文件,支持自动 CSS 注入、字段覆盖和版本号自增。
主要特性
- 🎯 自动 CSS 注入: 自动将 CSS 文件注入到 content scripts 中
- 🔧 动态清单构建: 在构建过程中动态构建 manifest.json
- 🎨 CSS 模式匹配: 支持自定义 CSS 文件模式匹配
- 📝 字段覆盖: 使用自定义值覆盖任何清单字段
- 🎯 定向注入: 仅将 CSS 注入到特定的 content scripts
- 🐛 调试模式: 全面的调试和日志记录
- 📦 TypeScript 支持: 完整的 TypeScript 支持和类型定义
- 🔄 模块支持: 支持 CommonJS 和 ESM
- 🔢 版本号自增: 自动递增 manifest.json 中的版本号
快速开始
import { defineConfig } from "vite";
import { manifestBuilderPlugin } from "vite-plugin-manifest-builder";
export default defineConfig({
plugins: [
manifestBuilderPlugin({
debug: true,
cssPattern: /\.(css|scss|less)$/,
targetScripts: ["content-scripts"],
manifestOverrides: {
description: "一个动态配置的 Chrome 扩展",
version: "1.0.1",
name: "我的扩展",
},
// 启用版本号自增
autoIncrementVersion: true,
versionIncrementType: 'patch',
versionIncrementStep: 1,
updateSourceFile: true // 更新源文件
}),
],
});
更多详细信息请参考英文文档部分。
vite-plugin-manifest-builder
A powerful Vite plugin for dynamically building and modifying Chrome Extension manifest.json
files with automatic CSS injection, field overrides, and version auto-increment.
✨ Features
- 🎯 Automatic CSS Injection: Automatically injects CSS files into content scripts
- 🔧 Dynamic Manifest Building: Builds manifest.json dynamically during build process
- 🎨 CSS Pattern Matching: Supports custom CSS file pattern matching
- 📝 Field Overrides: Override any manifest fields with custom values
- 🎯 Targeted Injection: Inject CSS only into specific content scripts
- 🐛 Debug Mode: Comprehensive debugging and logging
- 📦 TypeScript Support: Full TypeScript support with type definitions
- 🔄 Module Support: Supports both CommonJS and ESM
- 🔢 Version Auto-Increment: Automatically increments version numbers in manifest.json
📦 Installation
npm install vite-plugin-manifest-builder
Or with yarn:
yarn add vite-plugin-manifest-builder
🚀 Quick Start
Basic Usage
import { defineConfig } from "vite";
import { manifestBuilderPlugin } from "vite-plugin-manifest-builder";
export default defineConfig({
plugins: [manifestBuilderPlugin()],
});
Advanced Configuration
import { defineConfig } from "vite";
import { manifestBuilderPlugin } from "vite-plugin-manifest-builder";
export default defineConfig({
plugins: [
manifestBuilderPlugin({
debug: true,
cssPattern: /\.(css|scss|less)$/,
targetScripts: ["content-scripts"],
outDir: "dist",
manifestName: "manifest.json",
manifestOverrides: {
description: "A Chrome Extension with dynamic manifest",
version: "1.0.1",
name: "My Extension",
},
// Enable version auto-increment
autoIncrementVersion: true,
versionIncrementType: 'patch',
versionIncrementStep: 1,
updateSourceFile: true // Update source file
}),
],
});
📖 API Reference
Plugin Options
debug?: boolean
Enable debug logging. Default: false
manifestBuilderPlugin({
debug: true, // Enable detailed logging
});
cssPattern?: RegExp
Custom CSS file pattern matching. Default: /\.css$/
manifestBuilderPlugin({
cssPattern: /\.(css|scss|less)$/, // Match CSS, SCSS, and Less files
});
targetScripts?: string[]
Target specific content scripts for CSS injection. Default: []
(all scripts)
manifestBuilderPlugin({
targetScripts: ["content-scripts", "background"], // Only inject into these scripts
});
outDir?: string
Output directory for the built manifest. Default: 'dist'
manifestBuilderPlugin({
outDir: "build", // Custom output directory
});
manifestName?: string
Name of the output manifest file. Default: 'manifest.json'
manifestBuilderPlugin({
manifestName: "extension-manifest.json", // Custom manifest filename
});
manifestSource?: string
Source manifest file path. Default: 'manifest.json'
manifestBuilderPlugin({
manifestSource: "src/manifest.json", // Custom source manifest path
});
manifestOverrides?: Record<string, any>
Override manifest fields with custom values. Supports nested objects.
manifestBuilderPlugin({
manifestOverrides: {
description: "A powerful Chrome Extension",
version: "1.0.1",
name: "My Awesome Extension",
// Override any manifest field
permissions: ["storage", "activeTab"],
icons: {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png",
},
},
});
autoIncrementVersion?: boolean
Enable automatic version number increment. Default: false
manifestBuilderPlugin({
autoIncrementVersion: true, // Enable version auto-increment
});
versionIncrementType?: 'patch' | 'minor' | 'major'
Type of version increment. Default: 'patch'
manifestBuilderPlugin({
autoIncrementVersion: true,
versionIncrementType: 'minor', // Increment minor version
});
versionIncrementStep?: number
Step size for version increment. Default: 1
manifestBuilderPlugin({
autoIncrementVersion: true,
versionIncrementType: 'patch',
versionIncrementStep: 2, // Increment by 2
});
updateSourceFile?: boolean
Whether to update the source manifest file after version increment. Default: true
manifestBuilderPlugin({
autoIncrementVersion: true,
updateSourceFile: true, // Update source file (recommended)
});
🔄 How It Works
- Build Phase: Plugin runs during Vite's build process
- Version Processing: If enabled, automatically increments the version number
- Manifest Processing: Applies
manifestOverrides
to the source manifest - CSS Discovery: Finds all CSS files matching the pattern in the output directory
- CSS Injection: Automatically injects CSS files into content_scripts
- Output Generation: Writes the final manifest.json to the output directory
📝 Examples
Chrome Extension Development
// vite.config.ts
import { defineConfig } from "vite";
import { manifestBuilderPlugin } from "vite-plugin-manifest-builder";
export default defineConfig(({ mode }) => {
const isDev = mode === "development";
return {
plugins: [
manifestBuilderPlugin({
debug: isDev, // Enable debug in development
cssPattern: /\.css$/,
targetScripts: ["content-scripts"],
manifestOverrides: {
description: isDev ? "Development Version" : "Production Version",
version: "1.0.0",
},
// Only auto-increment version in production
autoIncrementVersion: !isDev,
versionIncrementType: 'patch',
}),
],
};
});
Multi-Environment Setup
// vite.config.ts
import { defineConfig } from "vite";
import { manifestBuilderPlugin } from "vite-plugin-manifest-builder";
export default defineConfig(({ mode }) => {
return {
plugins: [
manifestBuilderPlugin({
debug: mode === "development",
cssPattern: /\.(css|scss|less)$/,
targetScripts: ["content-scripts", "popup", "options"],
outDir: mode === "production" ? "dist" : "dev-dist",
manifestOverrides: {
name:
mode === "production"
? "Production Extension"
: "Development Extension",
version: mode === "production" ? "1.0.0" : "1.0.0-dev",
},
// Auto-increment version with different strategies
autoIncrementVersion: mode === "production",
versionIncrementType: mode === "production" ? 'minor' : 'patch',
versionIncrementStep: mode === "production" ? 1 : 1,
}),
],
};
});
Version Increment Examples
// Example 1: Patch version increment
// Input: "1.2.3" -> Output: "1.2.4"
manifestBuilderPlugin({
autoIncrementVersion: true,
versionIncrementType: 'patch',
versionIncrementStep: 1,
});
// Example 2: Minor version increment
// Input: "1.2.3" -> Output: "1.3.0"
manifestBuilderPlugin({
autoIncrementVersion: true,
versionIncrementType: 'minor',
versionIncrementStep: 1,
});
// Example 3: Major version increment
// Input: "1.2.3" -> Output: "2.0.0"
manifestBuilderPlugin({
autoIncrementVersion: true,
versionIncrementType: 'major',
versionIncrementStep: 1,
});
// Example 4: Custom step increment
// Input: "1.2.3" -> Output: "1.2.5" (patch + 2)
manifestBuilderPlugin({
autoIncrementVersion: true,
versionIncrementType: 'patch',
versionIncrementStep: 2,
});
// Example 5: Disable source file update
// Only updates output file, source file remains unchanged
manifestBuilderPlugin({
autoIncrementVersion: true,
updateSourceFile: false,
});
Version Persistence
The plugin automatically updates the source manifest file after version increment to ensure version numbers persist across builds:
// Build 1: 1.0.0 -> 1.0.1 (source file updated)
// Build 2: 1.0.1 -> 1.0.2 (source file updated)
// Build 3: 1.0.2 -> 1.0.3 (source file updated)
manifestBuilderPlugin({
autoIncrementVersion: true,
updateSourceFile: true, // Ensures version persistence
});
⚡ Performance Considerations
- Plugin Order: This plugin should run after other plugins with
enforce: 'post'
- CSS Processing: CSS files are processed after all other build steps
- File System: Minimal file system operations for optimal performance
- Bundle Hook: Uses
closeBundle
hook to ensure all files are built - Selective Injection: Use
targetScripts
to limit CSS injection scope - Version Processing: Version increment happens before manifest overrides
🐛 Troubleshooting
CSS Not Found
- Check if CSS files exist in the output directory
- Verify the
cssPattern
configuration - Enable
debug
mode for detailed logging
Manifest Issues
- Check if manifest.json has
css
field in content_scripts - Verify CSS file paths are correct
- Ensure content_scripts array exists in manifest
Build Errors
- Check output directory permissions
- Verify
outDir
andmanifestName
configuration - Ensure source manifest file exists
Override Issues
- Check
manifestOverrides
object structure - Verify field names match manifest schema
- Enable
debug
mode to see override details
Version Increment Issues
- Ensure manifest.json has a valid
version
field - Check version format (should be semantic versioning like "1.2.3")
- Verify
versionIncrementType
andversionIncrementStep
values - Enable
debug
mode to see version increment details
🛠️ Development
Setup
# Clone repository
git clone https://github.com/lizongxiao/vite-plugin-manifest-builder.git
cd vite-plugin-manifest-builder
# Install dependencies
npm install
# Start development
npm run dev
# Build
npm run build
Testing
# Run tests
npm test
# Run full test suite
npm run build && npm test
🤝 Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
📄 License
MIT License - see LICENSE file for details.
📋 Changelog
v1.0.0
- Initial release
- Automatic CSS injection
- Dynamic manifest building
- Field override support
- Full TypeScript support
- Version auto-increment feature