Skip to content

Commit 04c36d8

Browse files
committed
wip
1 parent 511fa91 commit 04c36d8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

apps/vscode-web/vite.config.mts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { writeFileSync } from 'node:fs'
12
import { defineConfig } from 'vite'
23
import Vue from '@vitejs/plugin-vue'
34
import VueJsx from '@vitejs/plugin-vue-jsx'
45
import { resolve } from 'path'
6+
import type { OutputAsset, OutputChunk } from 'rollup'
57
import { visualizer } from 'rollup-plugin-visualizer'
68
import { getLastCommit } from 'git-last-commit'
79
import 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+
1858
const lifecycle = process.env.npm_lifecycle_event
1959
let 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

Comments
 (0)