Inspired from NEXT.JS Instrumentation
This plugin is useful for performing asynchronous processing at the time the server starts up or during the build process.
ex: Injecting environment variables or secrets via Secret Manager
npm install -D vite-plugin-instrumentation
// vite.config.js
import { defineConfig } from "vite";
import { instrumentationPlugin } from "vite-plugin-instrumentation";
/**
* NOTE
* - A function that retrieves environment variables and secrets such as those from Secret Manager
*/
const fetchEnv = async () => {
return {
message: "MESSAGE",
};
};
export default defineConfig({
plugins: [
instrumentationPlugin([
async () => {
console.log("Hello World!!");
},
async (config) => {
const env = await fetchEnv();
config.define = {
...config.define,
__MESSAGE__: JSON.stringify(env.message),
};
},
]),
],
});
More details can be found here.