1+ import { writeFileSync } from 'node:fs'
12import { defineConfig } from 'vite'
23import Vue from '@vitejs/plugin-vue'
34import VueJsx from '@vitejs/plugin-vue-jsx'
45import { resolve } from 'path'
6+ import type { OutputAsset , OutputChunk } from 'rollup'
57import { visualizer } from 'rollup-plugin-visualizer'
68import { getLastCommit } from 'git-last-commit'
79import UnoCSS from 'unocss/vite'
@@ -15,6 +17,44 @@ function pathResolve(dir: string) {
1517 return resolve ( __dirname , '.' , dir )
1618}
1719
20+ /** 打包完成后把主入口 JS / 抽取的 CSS 文件名写入 dist/vs.json,供部署或扩展读取 */
21+ function recordBuildAssetsJson ( ) {
22+ const OUTPUT_NAME = 'vs.json'
23+ return {
24+ name : 'record-build-assets-json' ,
25+ writeBundle ( options : { dir ?: string } , bundle : Record < string , OutputChunk | OutputAsset > ) {
26+ const outDir = options . dir ?? pathResolve ( 'dist' )
27+ const jsChunks : string [ ] = [ ]
28+ const cssAssets : string [ ] = [ ]
29+ let entryJs = ''
30+
31+ for ( const fileName of Object . keys ( bundle ) ) {
32+ const item = bundle [ fileName ]
33+ if ( item . type === 'chunk' && fileName . endsWith ( '.js' ) ) {
34+ jsChunks . push ( fileName )
35+ if ( ( item as OutputChunk ) . isEntry ) entryJs = fileName
36+ }
37+ if ( item . type === 'asset' && fileName . endsWith ( '.css' ) ) {
38+ cssAssets . push ( fileName )
39+ }
40+ }
41+
42+ const payload = {
43+ /** 主入口 JS(含 hash),无多入口时与 js 列表第一项一致 */
44+ js : entryJs || jsChunks [ 0 ] || '' ,
45+ /** 主样式 CSS(含 hash),通常仅一条 */
46+ css : cssAssets [ 0 ] || '' ,
47+ /** 若以后拆 chunk,可查看全部 */
48+ jsAll : jsChunks ,
49+ cssAll : cssAssets ,
50+ generatedAt : new Date ( ) . toISOString ( ) ,
51+ }
52+
53+ writeFileSync ( resolve ( outDir , OUTPUT_NAME ) , JSON . stringify ( payload , null , 2 ) , 'utf-8' )
54+ } ,
55+ }
56+ }
57+
1858const lifecycle = process . env . npm_lifecycle_event
1959let isAnalyseBuild = [ 'report-oss' , 'report' ] . includes ( lifecycle )
2060
@@ -25,6 +65,7 @@ export default defineConfig(() => {
2565 if ( ! err ) latestCommitHash = commit . shortHash
2666 resolve ( {
2767 plugins : [
68+ recordBuildAssetsJson ( ) ,
2869 AutoImport ( {
2970 imports : [ 'vue' ] ,
3071 dts : 'src/auto-imports.d.ts' ,
0 commit comments