package | version | downloads | license |
---|---|---|---|
vite-plugin-replace2 | |||
vite-plugin-replace |
vite-plugin-replace has quite a few downloads, but the author no longer maintains it, so I forked the original author's code and made some improvements.
- Support both string and RegExp patterns
- Support function replacements
- Exclude files/directories from processing
- TypeScript support
- Compatible with Vite 2.x
npm i -D vite-plugin-replace2
# or
pnpm add -D vite-plugin-replace2
# or
yarn add -D vite-plugin-replace2
import { replaceCodePlugin } from "vite-plugin-replace2";
import { defineConfig } from "vite";
import packageJson from "./package.json";
export default defineConfig({
plugins: [
replaceCodePlugin({
replacements: [
{
from: "__CLI_NAME__",
to: packageJson.name,
},
{
from: /__CLI_VERSION__/g,
to: packageJson.version,
},
{
// Using function replacement
from: /__DATE__/g,
to: () => new Date().toISOString(),
},
],
exclude: ["node_modules"], // Optional: exclude directories
}),
],
});
interface VitePluginReplaceConfig {
replacements: ViteReplacement[];
exclude?: string | string[];
}
interface ViteReplacement {
from: string | RegExp; // Pattern to match
to: string | Function; // Replacement string or function
}
Option | Type | Description |
---|---|---|
replacements |
Array |
Array of replacement rules |
exclude |
string | string[] |
Files/directories to exclude from processing |
Option | Type | Description |
---|---|---|
from |
string | RegExp |
String or RegExp pattern to match |
to |
string | Function |
Replacement value or function |
{
from: "__VERSION__",
to: "1.0.0"
}
{
from: /__VERSION__/g,
to: "1.0.0"
}
{
from: /__TIMESTAMP__/g,
to: () => Date.now().toString()
}
MIT
Issues and PRs are welcome!