Skip to content

Commit 7aa17ed

Browse files
committed
feat: add testing env for vite
1 parent fd30b95 commit 7aa17ed

File tree

9 files changed

+26
-16
lines changed

9 files changed

+26
-16
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Base public path.
2-
VITE_APP_BASE_URL="/"
2+
VITE_BASE_PUBLIC_PATH="/"
33

44
# Use gzip or brotli to compress resources.
55
VITE_APP_GZIP="true"

.env.development

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Base public path.
2-
VITE_APP_BASE_URL="/dev"
2+
VITE_BASE_PUBLIC_PATH="/dev"
33

44
# Request url.
5-
VITE_PUBLIC_REQUEST_URL="/api"
5+
VITE_REQUEST_BASE_URL="/api"
66

77
# Request url with proxy.
8-
VITE_PUBLIC_REQUEST_URL_PROXY="https://jsonplaceholder.typicode.com"
8+
VITE_REQUEST_BASE_URL_PROXY="https://jsonplaceholder.typicode.com"

.env.production

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Base public path.
2-
VITE_APP_BASE_URL="/"
2+
VITE_BASE_PUBLIC_PATH="/"
33

44
# Request url.
5-
VITE_PUBLIC_REQUEST_URL="https://jsonplaceholder.typicode.com"
5+
VITE_REQUEST_BASE_URL="/api"
66

77
# Request url with proxy.
8-
VITE_PUBLIC_REQUEST_URL_PROXY=""
8+
VITE_REQUEST_BASE_URL_PROXY="https://jsonplaceholder.typicode.com"

.env.testing

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Base public path.
2+
VITE_BASE_PUBLIC_PATH="/"
3+
4+
# Request url.
5+
VITE_REQUEST_BASE_URL="/api"
6+
7+
# Request url with proxy.
8+
VITE_REQUEST_BASE_URL_PROXY="https://jsonplaceholder.typicode.com"

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"preinstall": "npx only-allow pnpm",
1515
"prepare": "simple-git-hooks",
1616
"dev": "vite dev",
17+
"dev:test": "vite dev --mode testing",
18+
"build:test": "vite build --mode testing",
1719
"build": "vite build",
1820
"preview": "vite preview",
1921
"test": "vitest",

src/composables/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function useRequest(config?: AxiosRequestConfig, interceptors?: {
1818
responseError?: (error: AxiosError) => AxiosError
1919
}) {
2020
const request = axios.create(assign({
21-
baseURL: import.meta.env.VITE_PUBLIC_REQUEST_URL,
21+
baseURL: import.meta.env.VITE_REQUEST_BASE_URL,
2222
timeout: 1000 * 10,
2323
headers: {
2424
'Content-Type': ContentTypeEnum.JSON,

src/modules/vue-router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createRouter, createWebHistory } from 'vue-router'
44
import { routes as autoRoutes, handleHotUpdate } from 'vue-router/auto-routes'
55

66
export const router = createRouter({
7-
history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
7+
history: createWebHistory(import.meta.env.VITE_BASE_PUBLIC_PATH),
88
routes: setupLayouts(autoRoutes),
99
})
1010

typings/env.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
/// <reference types="unplugin-vue-router/client" />
33

44
interface ImportMetaEnv {
5-
readonly VITE_APP_BASE_URL: string
5+
readonly VITE_BASE_PUBLIC_PATH: string
66
readonly VITE_APP_GZIP: string
7-
readonly VITE_PUBLIC_REQUEST_URL: string
8-
readonly VITE_PUBLIC_REQUEST_URL_PROXY: string
7+
readonly VITE_REQUEST_BASE_URL: string
8+
readonly VITE_REQUEST_BASE_URL_PROXY: string
99
}
1010

1111
interface ImportMeta {

vite.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ export default defineConfig(({ mode }) => {
2121
const chunks: string[] = ['axios', 'nprogress']
2222

2323
return {
24-
base: env.VITE_APP_BASE_URL,
24+
base: env.VITE_BASE_PUBLIC_PATH,
2525
server: {
2626
host: true,
2727
port: 9865,
2828
// Proxy request and socket.
2929
// https://vitejs.dev/config/server-options.html#server-proxy
3030
proxy: {
31-
[env.VITE_PUBLIC_REQUEST_URL]: {
32-
target: env.VITE_PUBLIC_REQUEST_URL_PROXY,
31+
[env.VITE_REQUEST_BASE_URL]: {
32+
target: env.VITE_REQUEST_BASE_URL_PROXY,
3333
changeOrigin: true,
34-
rewrite: (path: string) => path.replace(new RegExp(`^${env.VITE_PUBLIC_REQUEST_URL}`), ''),
34+
rewrite: (path: string) => path.replace(new RegExp(`^${env.VITE_REQUEST_BASE_URL}`), ''),
3535
},
3636
},
3737
},

0 commit comments

Comments
 (0)