vite-plugin-transform-json
Version:
Vite plugin to copy and transform JSON file during build
54 lines (42 loc) • 1.32 kB
Markdown
# Vite Plugin Transform JSON
Plugin will copy JSON file, to `outDir` directory, named as per input one, during Vite build and modifies data dynamically via callback function.
## Install
Install package from npm
> npm install -D vite-plugin-transform-json
## Basic Usage
Create JSON file in `src/manifest.json`.
Add plugin to Vite configuration and add properties as showed below.
```ts
// vite.config.{js,ts}
defineConfig({
plugins: [
// use plugin here
viteCopyTransformJson({
// define path of the JSON file
srcPath: "src/manifest.json",
// callback function, can be sync or async function
async transformedProps() {
const isMatchCondition = true;
const backgroundPagePath = "assets/background.js";
// return JSON-like object
return {
version: "new version",
description: "new description",
homepage_url: "new repo url",
background: isMatchCondition
? { service_worker: backgroundPagePath }
: { page: backgroundPagePath },
};
},
// Optional properties
encoding: "utf8",
apply: "build",
}),
],
build: {
outDir: "dist",
emptyOutDir: true,
},
});
```
That should generate new manifest file with specified settings in `dist` folder