From 93be61aaf380e05eb72f83632b1a0747f5ce9d61 Mon Sep 17 00:00:00 2001
From: CaIon <1808837298@qq.com>
Date: Sat, 23 Mar 2024 19:09:09 +0800
Subject: [PATCH] feat: vite
---
main.go | 4 ++--
router/api-router.go | 2 +-
web/.env | 2 ++
web/index.html | 19 ++++++++++++++++++
web/package.json | 16 ++++++++-------
web/src/components/Footer.js | 2 +-
web/src/helpers/api.js | 2 +-
web/vite.config.js | 38 ++++++++++++++++++++++++++++++++++++
8 files changed, 73 insertions(+), 12 deletions(-)
create mode 100644 web/.env
create mode 100644 web/index.html
create mode 100644 web/vite.config.js
diff --git a/main.go b/main.go
index 234bca79..37c6a0a4 100644
--- a/main.go
+++ b/main.go
@@ -20,10 +20,10 @@ import (
_ "net/http/pprof"
)
-//go:embed web/build
+//go:embed web/dist
var buildFS embed.FS
-//go:embed web/build/index.html
+//go:embed web/dist/index.html
var indexPage []byte
func main() {
diff --git a/router/api-router.go b/router/api-router.go
index 592e8ede..85474543 100644
--- a/router/api-router.go
+++ b/router/api-router.go
@@ -17,7 +17,7 @@ func SetApiRouter(router *gin.Engine) {
apiRouter.GET("/status/test", middleware.AdminAuth(), controller.TestStatus)
apiRouter.GET("/notice", controller.GetNotice)
apiRouter.GET("/about", controller.GetAbout)
- apiRouter.GET("/midjourney", controller.GetMidjourney)
+ //apiRouter.GET("/midjourney", controller.GetMidjourney)
apiRouter.GET("/home_page_content", controller.GetHomePageContent)
apiRouter.GET("/verification", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendEmailVerification)
apiRouter.GET("/reset_password", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendPasswordResetEmail)
diff --git a/web/.env b/web/.env
new file mode 100644
index 00000000..449b3908
--- /dev/null
+++ b/web/.env
@@ -0,0 +1,2 @@
+VITE_REACT_APP_SERVER_URL=
+VITE_REACT_APP_VERSION=
\ No newline at end of file
diff --git a/web/index.html b/web/index.html
new file mode 100644
index 00000000..0760dd29
--- /dev/null
+++ b/web/index.html
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
+
+ New API
+
+
+
+
+
+
+
diff --git a/web/package.json b/web/package.json
index dc6ca101..c3bac854 100644
--- a/web/package.json
+++ b/web/package.json
@@ -2,6 +2,7 @@
"name": "react-template",
"version": "0.1.0",
"private": true,
+ "type": "module",
"dependencies": {
"@douyinfe/semi-icons": "^2.46.1",
"@douyinfe/semi-ui": "^2.46.1",
@@ -21,14 +22,13 @@
"react-toastify": "^9.0.8",
"react-turnstile": "^1.0.5",
"semantic-ui-css": "^2.5.0",
- "semantic-ui-react": "^2.1.3",
- "usehooks-ts": "^2.9.1"
+ "semantic-ui-react": "^2.1.3"
},
"scripts": {
- "start": "react-scripts start",
- "build": "react-scripts build",
- "test": "react-scripts test",
- "eject": "react-scripts eject"
+ "dev": "vite",
+ "build": "vite build",
+ "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
+ "preview": "vite preview"
},
"eslintConfig": {
"extends": [
@@ -50,7 +50,9 @@
},
"devDependencies": {
"prettier": "2.8.8",
- "typescript": "4.4.2"
+ "typescript": "4.4.2",
+ "vite": "^5.2.0",
+ "@vitejs/plugin-react": "^4.2.1"
},
"prettier": {
"singleQuote": true,
diff --git a/web/src/components/Footer.js b/web/src/components/Footer.js
index 9e294268..59676387 100644
--- a/web/src/components/Footer.js
+++ b/web/src/components/Footer.js
@@ -41,7 +41,7 @@ const Footer = () => {
href="https://github.com/Calcium-Ion/new-api"
target="_blank" rel="noreferrer"
>
- New API {process.env.REACT_APP_VERSION}{' '}
+ New API {import.meta.env.VITE_REACT_APP_VERSION}{' '}
由{' '}
diff --git a/web/src/helpers/api.js b/web/src/helpers/api.js
index 35fdb1e9..ac82f294 100644
--- a/web/src/helpers/api.js
+++ b/web/src/helpers/api.js
@@ -2,7 +2,7 @@ import { showError } from './utils';
import axios from 'axios';
export const API = axios.create({
- baseURL: process.env.REACT_APP_SERVER ? process.env.REACT_APP_SERVER : '',
+ baseURL: import.meta.env.VITE_REACT_APP_SERVER_URL ? import.meta.env.VITE_REACT_APP_SERVER_URL : '',
});
API.interceptors.response.use(
diff --git a/web/vite.config.js b/web/vite.config.js
new file mode 100644
index 00000000..ce25a6ef
--- /dev/null
+++ b/web/vite.config.js
@@ -0,0 +1,38 @@
+import { defineConfig, transformWithEsbuild } from 'vite';
+import react from '@vitejs/plugin-react';
+
+// https://vitejs.dev/config/
+export default defineConfig({
+ plugins: [
+ {
+ name: 'treat-js-files-as-jsx',
+ async transform(code, id) {
+ if (!id.match(/src\/.*\.js$/)) return null
+
+ // Use the exposed transform from vite, instead of directly
+ // transforming with esbuild
+ return transformWithEsbuild(code, id, {
+ loader: 'jsx',
+ jsx: 'automatic',
+ })
+ },
+ },
+ react(),
+ ],
+ optimizeDeps: {
+ force: true,
+ esbuildOptions: {
+ loader: {
+ '.js': 'jsx',
+ },
+ },
+ },
+ server: {
+ proxy: {
+ '/api': {
+ target: "http://localhost:3000",
+ changeOrigin: true,
+ }
+ }
+ }
+});