From ff17c901399b7ecbe0b58144ca2fab0d3606559e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 27 Apr 2023 01:35:30 +0800 Subject: [PATCH] implement production build --- frontend/main.go | 10 +++++++ frontend/vben/.env.production | 2 +- frontend/vben/.gitignore | 1 + frontend/vben/main.go | 29 +++++++++++++++++++ .../workbench/components/DynamicInfo.vue | 2 +- frontend/vben/vite.config.ts | 16 +++++----- main.go | 2 ++ 7 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 frontend/main.go create mode 100644 frontend/vben/main.go diff --git a/frontend/main.go b/frontend/main.go new file mode 100644 index 0000000..9d75a40 --- /dev/null +++ b/frontend/main.go @@ -0,0 +1,10 @@ +package frontend + +import ( + "net/http" + + "github.com/fumiama/paper-manager/frontend/vben" +) + +// StaticHandler serves contents in frontend +var StaticHandler = http.FileServer(vben.Distribution) diff --git a/frontend/vben/.env.production b/frontend/vben/.env.production index 40548cd..21b6719 100644 --- a/frontend/vben/.env.production +++ b/frontend/vben/.env.production @@ -32,4 +32,4 @@ VITE_USE_IMAGEMIN= true VITE_USE_PWA = false # Is it compatible with older browsers -VITE_LEGACY = false +VITE_LEGACY = true diff --git a/frontend/vben/.gitignore b/frontend/vben/.gitignore index 2201bd7..d28ddfa 100644 --- a/frontend/vben/.gitignore +++ b/frontend/vben/.gitignore @@ -32,3 +32,4 @@ package-lock.json pnpm-lock.yaml .vscode +dist.zip diff --git a/frontend/vben/main.go b/frontend/vben/main.go new file mode 100644 index 0000000..540c14e --- /dev/null +++ b/frontend/vben/main.go @@ -0,0 +1,29 @@ +package vben + +import ( + "archive/zip" + "bytes" + _ "embed" // embed dist.zip + "io/fs" + "net/http" +) + +//go:generate npm run build + +//go:generate zip -9 -r -D dist.zip dist/* -x "dist/.DS_Store" "dist/*/.DS_Store" + +//go:embed dist.zip +var distzipbytes []byte + +// Distribution ... +var Distribution = func() http.FileSystem { + distzip, err := zip.NewReader(bytes.NewReader(distzipbytes), int64(len(distzipbytes))) + if err != nil { + panic(err) + } + distfs, err := fs.Sub(fs.FS(distzip), "dist") + if err != nil { + panic(err) + } + return http.FS(distfs) +}() diff --git a/frontend/vben/src/views/dashboard/workbench/components/DynamicInfo.vue b/frontend/vben/src/views/dashboard/workbench/components/DynamicInfo.vue index 2d7d555..d26ad0b 100644 --- a/frontend/vben/src/views/dashboard/workbench/components/DynamicInfo.vue +++ b/frontend/vben/src/views/dashboard/workbench/components/DynamicInfo.vue @@ -36,7 +36,7 @@ import { Card, List } from 'ant-design-vue' import { getMessageList, acceptMessage, deleteMessage } from '/@/api/dashboard/index' import { useMessage } from '/@/hooks/web/useMessage' - import { MessageTypeEnum, MessageItem } from '../../../../api/dashboard/model/model.js' + import { MessageTypeEnum, MessageItem } from '/@/api/dashboard/model/model' import { Avatar } from 'ant-design-vue' import headerImg from '/@/assets/images/header.jpg' diff --git a/frontend/vben/vite.config.ts b/frontend/vben/vite.config.ts index bd1302b..ef631d9 100644 --- a/frontend/vben/vite.config.ts +++ b/frontend/vben/vite.config.ts @@ -64,20 +64,20 @@ export default ({ command, mode }: ConfigEnv): UserConfig => { pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [], }, build: { - target: 'es2015', + target: 'es2022', cssTarget: 'chrome80', outDir: OUTPUT_DIR, - // minify: 'terser', + minify: 'terser', /** * 当 minify=“minify:'terser'” 解开注释 * Uncomment when minify="minify:'terser'" */ - // terserOptions: { - // compress: { - // keep_infinity: true, - // drop_console: VITE_DROP_CONSOLE, - // }, - // }, + terserOptions: { + compress: { + keep_infinity: true, + drop_console: VITE_DROP_CONSOLE, + }, + }, // Turning off brotliSize display can slightly reduce packaging time brotliSize: false, chunkSizeWarningLimit: 2000, diff --git a/main.go b/main.go index b2caa12..da0bde8 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/sirupsen/logrus" "github.com/fumiama/paper-manager/backend" + "github.com/fumiama/paper-manager/frontend" ) func line() int { @@ -33,6 +34,7 @@ func main() { http.HandleFunc("/file/", backend.FileHandler) http.HandleFunc("/paper/", backend.PaperHandler) http.HandleFunc("/upload", backend.UploadHandler) + http.Handle("/", frontend.StaticHandler) logrus.Infoln("[http.Serve] start at", l.Addr()) logrus.Errorln("[http.Serve]", http.Serve(l, nil))