From 1e37259c98c959c6534cdb3ecd924b6e2eb879ef Mon Sep 17 00:00:00 2001 From: duorameng <2997944583@qq.com> Date: Fri, 24 Apr 2026 09:26:54 +0800 Subject: [PATCH] chore: release binary front assets with cdn --- Makefile | 11 ++++++++++- web/src/main.ts | 2 +- web/src/types/window.d.ts | 1 + web/vite.config.ts | 31 ++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 9452755..be82f19 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,16 @@ build: CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BINARY) main.go # Build release version (Frontend + Backend with embedded assets) -release: build-web +release: + cd web && npm ci && npm run build + @mkdir -p bin + rm -rf internal/static/dist + cp -r web/dist internal/static/dist + CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) $(TAGS_WEB) -o $(BINARY) main.go + +# Build release version (Frontend + Backend with embedded assets) +release-binary: + cd web && npm ci && VITE_RELEASE_OPTIMIZE=true npm run build @mkdir -p bin rm -rf internal/static/dist cp -r web/dist internal/static/dist diff --git a/web/src/main.ts b/web/src/main.ts index 3dd7901..2eb300b 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -17,7 +17,7 @@ createApp(App) .use(router) .use(VueMonacoEditorPlugin, { paths: { - vs: `${origin}${BASE_URL}/assets/vs` + vs: __MONACO_CDN__ || `${origin}${BASE_URL}/assets/vs` } }) .mount('#app') diff --git a/web/src/types/window.d.ts b/web/src/types/window.d.ts index 58324c8..3a9f4f2 100644 --- a/web/src/types/window.d.ts +++ b/web/src/types/window.d.ts @@ -4,6 +4,7 @@ declare global { __BASE_URL__?: string __API_VERSION__?: string } + const __MONACO_CDN__: string | null } export {} diff --git a/web/vite.config.ts b/web/vite.config.ts index 1ff46a7..62b5e55 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -44,11 +44,38 @@ const compressionPlugin = () => ({ } } as const) +// Release optimization plugin to offload fonts to CDN +const releaseOptimizePlugin = (isOptimize: boolean) => ({ + name: 'release-optimize-plugin', + transformIndexHtml(html: string) { + if (isOptimize) { + return html.replace( + '', + ` \n \n ` + ) + } + return html + }, + transform(code: string, id: string) { + if (isOptimize && id.includes('index.css')) { + return { + code: code + .replace(/@import\s+["']@fontsource-variable\/inter\/index\.css["'];/g, '/* Removed for CDN */') + .replace(/@import\s+["']@fontsource-variable\/noto-sans-sc\/index\.css["'];/g, '/* Removed for CDN */') + .replace(/@import\s+["']@fontsource-variable\/jetbrains-mono\/index\.css["'];/g, '/* Removed for CDN */'), + map: null + } + } + } +}) + +const isOptimize = process.env.VITE_RELEASE_OPTIMIZE === 'true' + export default defineConfig({ plugins: [ vue(), tailwindcss(), - viteStaticCopy({ + !isOptimize && viteStaticCopy({ targets: [ { src: 'node_modules/monaco-editor/min/vs', @@ -56,6 +83,7 @@ export default defineConfig({ } ] }), + releaseOptimizePlugin(isOptimize), compressionPlugin(), VitePWA({ registerType: 'autoUpdate', @@ -147,5 +175,6 @@ export default defineConfig({ } }, define: { + __MONACO_CDN__: isOptimize ? JSON.stringify('https://registry.npmmirror.com/monaco-editor/0.52.0/files/min/vs') : 'null' } })