使用 vite 框架向 index.html 注入 VITE_APP_VERSION 环境变量使用心得
使用 vite 框架将 package.json 中的 version 字段以环境变量 VITE_APP_VERSION 注入到 index.html 文件中,需要使用 %VITE_APP_VERSION% 进行注入:
<!-- 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>
有关自动提取 package.json 文件中的 version 字段,谷歌给出了如下方法:
# 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),
},
})
但并没有达到预期,反而得到了如下提示:
(!) %VITE_APP_VERSION% is not defined in env variables found in /index.html. Is the variable mistyped?
经过进一步在 stackoverflow.com 寻找解决方案,最终发现如下方法是适用的:
# 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),
},
})
观察后觉得关键用法是 define 名称 VITE_APP_VERSION 应该写作 'import.meta.env.VITE_APP_VERSION',随后尝试修改了谷歌给出的代码中的 define 名称。最终实现了同样的效果:
# 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),
},
})谷歌给出的方案是基于 AI 的,现在来看 AI 对编写软件代码仅是一个辅助,还不能作为最终解决方案。 stackoverflow.com 起码在现阶段对编写软件代码更靠谱。进一步来说, AI 对编写软件代码来说现在还只是一个更好的搜索引擎而已。
12