Automating a Batch File during a Vite Build

This article can be read in about 3 minutes.
PR

The purpose

Automatically run a specified batch file when performing a Vite build (npm run build)

Example

“Automating the following tasks seems to be useful. (There are probably many others, but these are what come to mind immediately.)”

  • SVG Optimization
  • Add copyright to the JS file.
  • Copy from dist to the production environment
PR

Setting

Open the package.json file located at the root of your Vite project.

You should find the following description.

    "scripts": {
        "dev": "node log.js dev & vite --config vite/config.dev.mjs",
        "build": "node log.js build & vite build --config vite/config.prod.mjs",
        "dev-nolog": "vite --config vite/config.dev.mjs",
        "build-nolog": "vite build --config vite/config.prod.mjs"
    },

In this article, we will modify the line "build": "node log.js build & vite build --config vite/config.prod.mjs", to add a process when npm run build is executed.

Before:

 "build": "node log.js build & vite build --config vite/config.prod.mjs",

After:

 "build": "node log.js build & vite build --config vite/config.prod.mjs & call CustomBuild.bat",

With the above modification, CustomBuild.bat will be executed after the Vite build. The path to the batch file is a relative path from the project root. The example above assumes that CustomBuild.bat is located in the root directory.

The current directory within the batch file is the project root. Please be careful when using relative paths inside the batch file.

PR

Result

I was able to execute an additional batch file when npm run build is performed.

comment

Copied title and URL