JSPM

vite-plugin-transform-json

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q28871F
  • License GPL-3.0-only

Vite plugin to copy and transform JSON file during build

Package Exports

  • vite-plugin-transform-json
  • vite-plugin-transform-json/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vite-plugin-transform-json) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

// 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