From 9e6a677ce7c1685287e29feef31199cb9ec290c7 Mon Sep 17 00:00:00 2001 From: slhmy <1484836413@qq.com> Date: Sun, 1 Oct 2023 22:26:12 +0800 Subject: [PATCH] Fix gh-pages base route --- .env | 0 .env.gh-pages | 1 + .github/workflows/deploy-demo-to-gh-pages.yml | 2 +- package.json | 2 +- src/index.tsx | 2 +- vite.config.js | 56 +++++++++++-------- 6 files changed, 36 insertions(+), 27 deletions(-) delete mode 100644 .env create mode 100644 .env.gh-pages diff --git a/.env b/.env deleted file mode 100644 index e69de29bb..000000000 diff --git a/.env.gh-pages b/.env.gh-pages new file mode 100644 index 000000000..a0494a973 --- /dev/null +++ b/.env.gh-pages @@ -0,0 +1 @@ +VITE_GH_PAGES_BUILD=true diff --git a/.github/workflows/deploy-demo-to-gh-pages.yml b/.github/workflows/deploy-demo-to-gh-pages.yml index 976691139..5fef80998 100644 --- a/.github/workflows/deploy-demo-to-gh-pages.yml +++ b/.github/workflows/deploy-demo-to-gh-pages.yml @@ -19,7 +19,7 @@ jobs: run: npm ci - name: Build - run: npm run build-mock + run: npm run build:gh-pages - name: Deploy if: ${{ github.event_name != 'pull_request' }} diff --git a/package.json b/package.json index 4f890152a..79db8de52 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "start": "vite", "start-mock": "vite --mode mock", "build": "vite build", - "build-mock": "vite build --mode mock", + "build:gh-pages": "vite build --mode gh-pages", "build-and-zip": "vite build && ./script/zip-dist.sh", "init-mock": "npx msw init public --no-save", "prettier": "npx prettier --write \"src/**/*.{ts,tsx}\"", diff --git a/src/index.tsx b/src/index.tsx index 19e3984c8..da1166551 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -8,7 +8,7 @@ import "./i18n/i18n"; import "./index.css"; console.log("Running in:", import.meta.env.MODE); -if (import.meta.env.MODE === "mock") { +if (import.meta.env.MODE === "mock" || import.meta.env.MODE === "gh-pages") { worker.start(); } diff --git a/vite.config.js b/vite.config.js index 953b5a717..e167ac62e 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,32 +1,40 @@ -import { defineConfig } from "vite"; +import { defineConfig, loadEnv } from "vite"; import react from "@vitejs/plugin-react"; const path = require("path"); -export default defineConfig({ - server: { - proxy: { - "/api/v1": { - target: "http://localhost:8080/api/v1", - changeOrigin: true, - rewrite: (path) => path.replace(/^\/api\/v1/, ""), +const viteConfig = ({ mode }) => { + const isGhPagesBuild = + loadEnv(mode, process.cwd()).VITE_GH_PAGES_BUILD === "true"; + + return defineConfig({ + server: { + proxy: { + "/api/v1": { + target: "http://localhost:8080/api/v1", + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api\/v1/, ""), + }, }, }, - }, - build: { - // https://stackoverflow.com/questions/76694615/module-level-directives-cause-errors-when-bundled-use-client-was-ignored-caus - rollupOptions: { - onwarn(warning, warn) { - if (warning.code === "MODULE_LEVEL_DIRECTIVE") { - return; - } - warn(warning); + base: isGhPagesBuild ? "/oj-lab-front/" : "/", + build: { + // https://stackoverflow.com/questions/76694615/module-level-directives-cause-errors-when-bundled-use-client-was-ignored-caus + rollupOptions: { + onwarn(warning, warn) { + if (warning.code === "MODULE_LEVEL_DIRECTIVE") { + return; + } + warn(warning); + }, }, }, - }, - plugins: [react()], - resolve: { - alias: { - "@": path.resolve(__dirname, "./src"), + plugins: [react()], + resolve: { + alias: { + "@": path.resolve(__dirname, "./src"), + }, }, - }, -}); + }); +}; + +export default viteConfig;