Skip to content

Commit

Permalink
fix(版本文件): 解决打包时静态资源文件路径问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hesetiema committed May 25, 2024
1 parent d058ad5 commit 5dc0095
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { PluginOption } from 'vite'
import fs from 'fs'
import path from 'path'

export default function vitePluginUpdateVersion(): PluginOption {
let config: any, lastBuildTime: string
Expand All @@ -16,10 +14,24 @@ export default function vitePluginUpdateVersion(): PluginOption {
config = resolvedConfig
},

buildStart() {
lastBuildTime = new Date().toLocaleString()
const filePath = config.publicDir + path.sep + 'version.json'
fs.writeFileSync(filePath, JSON.stringify({ lastBuildTime }, null, 2))
buildEnd() {
lastBuildTime = new Date().toISOString()
},

generateBundle(_, bundle = {}) {
// inject version json file
const content = {
lastBuildTime
}
const jsonContent = JSON.stringify(content, null, 2)

bundle.version = {
type: 'asset',
name: undefined,
source: jsonContent,
fileName: `version.json`,
needsCodeReference: false
}
},

transformIndexHtml(html) {
Expand Down

0 comments on commit 5dc0095

Please sign in to comment.