inject VITE_APP_VERSION into index.html with vite

I want to inject VITE_APP_VERSION into index.html with vite:

<!-- index.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Areditors v%VITE_APP_VERSION%</title>
    <link rel="icon" href="/favicon.ico">
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

I google to find the below method:

# vite.config.ts
import { readFileSync } from 'fs'
import path from "path"
import { fileURLToPath } from 'url'
import { VitePWA } from 'vite-plugin-pwa'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

const file = fileURLToPath(new URL('package.json', import.meta.url));
const json = readFileSync(file, 'utf8');
const pkg = JSON.parse(json);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), VitePWA({
    registerType: 'autoUpdate',
    injectRegister: false,

    pwaAssets: {
      disabled: false,
      config: true,
    },

    manifest: {
      name: 'aaeditors',
      short_name: 'aae',
      description: 'Anflext areditors',
      theme_color: '#ffffff',
      icons: [
        {
          src: 'logo192.png',
          sizes: '192x192',
          type: 'image/png'
        },
        {
          src: 'logo512.png',
          sizes: '512x512',
          type: 'image/png'
        }
      ]
    },

    workbox: {
      globPatterns: ['**/*.{js,css,html,svg,png,ico}'],
      cleanupOutdatedCaches: true,
      clientsClaim: true,
    },

    devOptions: {
      enabled: false,
      navigateFallback: 'index.html',
      suppressWarnings: true,
      type: 'module',
    },
  })],
  resolve: {
    alias: [{ find: "@", replacement: path.resolve("./src") }],
  },
  server: {
    port: 3009, // aaeditors length is nine, add to 3000 for unique
  },
  define: {
    VITE_APP_VERSION: JSON.stringify(pkg.version),
  },
})

But that is not effect to my project. So I find more in the stackoverflow.com and that is fit my project usage:

# vite.config.ts
import path from "path"
import { VitePWA } from 'vite-plugin-pwa'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import packageJson from './package.json'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), VitePWA({
    registerType: 'autoUpdate',
    injectRegister: false,

    pwaAssets: {
      disabled: false,
      config: true,
    },

    manifest: {
      name: 'aaeditors',
      short_name: 'aae',
      description: 'Anflext areditors',
      theme_color: '#ffffff',
      icons: [
        {
          src: 'logo192.png',
          sizes: '192x192',
          type: 'image/png'
        },
        {
          src: 'logo512.png',
          sizes: '512x512',
          type: 'image/png'
        }
      ]
    },

    workbox: {
      globPatterns: ['**/*.{js,css,html,svg,png,ico}'],
      cleanupOutdatedCaches: true,
      clientsClaim: true,
    },

    devOptions: {
      enabled: false,
      navigateFallback: 'index.html',
      suppressWarnings: true,
      type: 'module',
    },
  })],
  resolve: {
    alias: [{ find: "@", replacement: path.resolve("./src") }],
  },
  server: {
    port: 3009, // aaeditors length is nine, add to 3000 for unique
  },
  define: {
    'import.meta.env.VITE_APP_VERSION': JSON.stringify(packageJson.version),
  },
})

I think the key usage is the define name VITE_APP_VERSION should be 'import.meta.env.VITE_APP_VERSION', then I try to modify the google method define name and that is same OK:

# vite.config.ts
import { readFileSync } from 'fs'
import path from "path"
import { fileURLToPath } from 'url'
import { VitePWA } from 'vite-plugin-pwa'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

const file = fileURLToPath(new URL('package.json', import.meta.url));
const json = readFileSync(file, 'utf8');
const pkg = JSON.parse(json);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), VitePWA({
    registerType: 'autoUpdate',
    injectRegister: false,

    pwaAssets: {
      disabled: false,
      config: true,
    },

    manifest: {
      name: 'aaeditors',
      short_name: 'aae',
      description: 'Anflext areditors',
      theme_color: '#ffffff',
      icons: [
        {
          src: 'logo192.png',
          sizes: '192x192',
          type: 'image/png'
        },
        {
          src: 'logo512.png',
          sizes: '512x512',
          type: 'image/png'
        }
      ]
    },

    workbox: {
      globPatterns: ['**/*.{js,css,html,svg,png,ico}'],
      cleanupOutdatedCaches: true,
      clientsClaim: true,
    },

    devOptions: {
      enabled: false,
      navigateFallback: 'index.html',
      suppressWarnings: true,
      type: 'module',
    },
  })],
  resolve: {
    alias: [{ find: "@", replacement: path.resolve("./src") }],
  },
  server: {
    port: 3009, // aaeditors length is nine, add to 3000 for unique
  },
  define: {
    'import.meta.env.VITE_APP_VERSION': JSON.stringify(pkg.version),
  },
})

The google method is based on AI. And now AI is agent for coding, is not final solution. And stackoverflow.com is better usage for coding currently. So I think the AI for coding is a better search engine ONLY now.

17

Top articles