Skip to content

Commit 645f8d3

Browse files
committed
github打包配置环境变量
1 parent c0b98a1 commit 645f8d3

File tree

4 files changed

+75
-69
lines changed

4 files changed

+75
-69
lines changed

.env.github

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_APP_BASE_URL = /buildElement/

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
2424
run: |
2525
pnpm install
26-
pnpm run build --base /buildElement/
26+
pnpm run build:github
2727
cp dist/index.html dist/404.html
2828
- name: Deploy 🚀
2929
uses: peaceiris/actions-gh-pages@v3

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev": "vite",
77
"build": "vue-tsc --noEmit && vite build",
8+
"build:github": "vue-tsc --noEmit && vite build --mode github",
89
"preview": "vite preview --port 8888",
910
"eslint": "eslint src --fix --ext .js,.ts,.jsx,.tsx,.vue"
1011
},

vite.config.ts

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineConfig } from 'vite'
1+
import { defineConfig, loadEnv } from 'vite'
22
import vue from '@vitejs/plugin-vue'
33
import vueJsx from '@vitejs/plugin-vue-jsx'
44
import { visualizer } from 'rollup-plugin-visualizer'
@@ -15,76 +15,80 @@ import postcssPresetEnv from 'postcss-preset-env'
1515
import { formatDate } from './src/utils/time'
1616

1717
// https://vitejs.dev/config/
18-
export default defineConfig({
19-
base: '/',
20-
resolve: {
21-
alias: {
22-
'@': resolve(__dirname, 'src'),
18+
export default ({ mode }) => {
19+
const env = loadEnv(mode, process.cwd())
20+
const { VITE_APP_BASE_URL: BASE_URL = '/' } = env
21+
return defineConfig({
22+
base: BASE_URL,
23+
resolve: {
24+
alias: {
25+
'@': resolve(__dirname, 'src'),
26+
},
2327
},
24-
},
25-
build: {
26-
terserOptions: {
27-
compress: {
28-
keep_infinity: true, // 防止 Infinity 被压缩成 1/0,这可能会导致 Chrome 上的性能问题
29-
drop_console: true, // 生产环境去除 console
30-
drop_debugger: true, // 生产环境去除 debugger
28+
build: {
29+
terserOptions: {
30+
compress: {
31+
keep_infinity: true, // 防止 Infinity 被压缩成 1/0,这可能会导致 Chrome 上的性能问题
32+
drop_console: true, // 生产环境去除 console
33+
drop_debugger: true, // 生产环境去除 debugger
34+
},
3135
},
3236
},
33-
},
34-
css: {
35-
preprocessorOptions: {
36-
scss: {
37-
additionalData: `@use "@/styles/theme.scss" as *;`,
37+
css: {
38+
preprocessorOptions: {
39+
scss: {
40+
additionalData: `@use "@/styles/theme.scss" as *;`,
41+
},
42+
},
43+
postcss: {
44+
plugins: [postcssPresetEnv()],
3845
},
3946
},
40-
postcss: {
41-
plugins: [postcssPresetEnv()],
47+
define: {
48+
__PKG_TIME__: JSON.stringify(formatDate(new Date())),
4249
},
43-
},
44-
define: {
45-
__PKG_TIME__: JSON.stringify(formatDate(new Date())),
46-
},
47-
plugins: [
48-
vue(),
49-
vueJsx(),
50-
visualizer({
51-
// open: true, //注意这里要设置为true,否则无效
52-
gzipSize: true,
53-
brotliSize: true,
54-
}),
55-
AutoImport({
56-
imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
57-
dts: './src/auto-imports.d.ts',
58-
eslintrc: {
59-
enabled: false, // Default `false`
60-
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
61-
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
62-
},
63-
resolvers: [ElementPlusResolver()],
64-
}),
65-
Components({
66-
dirs: ['src/components'],
67-
resolvers: [ElementPlusResolver()],
68-
dts: 'src/components.d.ts',
69-
include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/, /\.md$/],
70-
}),
71-
// ElementPlus(),
72-
Unocss(),
73-
viteMockServe({
74-
supportTs: false,
75-
logger: false,
76-
mockPath: './src/mock',
77-
localEnabled: true, // 开发打包开关
78-
prodEnabled: true, // 生产打包开关 // 这样可以控制关闭mock的时候不让mock打包到最终代码内
79-
injectCode: ` import { setupProdMockServer } from './mockProdServer'; setupProdMockServer(); `,
80-
}),
81-
vueSetupExtend(),
82-
eslintPlugin(),
83-
webUpdateNotice({ logVersion: true, injectFileBase: '/' }),
84-
],
85-
server: {
86-
host: '0.0.0.0',
87-
open: true,
88-
port: 4646,
89-
},
90-
})
50+
plugins: [
51+
vue(),
52+
vueJsx(),
53+
visualizer({
54+
// open: true, //注意这里要设置为true,否则无效
55+
gzipSize: true,
56+
brotliSize: true,
57+
}),
58+
AutoImport({
59+
imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
60+
dts: './src/auto-imports.d.ts',
61+
eslintrc: {
62+
enabled: false, // Default `false`
63+
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
64+
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
65+
},
66+
resolvers: [ElementPlusResolver()],
67+
}),
68+
Components({
69+
dirs: ['src/components'],
70+
resolvers: [ElementPlusResolver()],
71+
dts: 'src/components.d.ts',
72+
include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/, /\.md$/],
73+
}),
74+
// ElementPlus(),
75+
Unocss(),
76+
viteMockServe({
77+
supportTs: false,
78+
logger: false,
79+
mockPath: './src/mock',
80+
localEnabled: true, // 开发打包开关
81+
prodEnabled: true, // 生产打包开关 // 这样可以控制关闭mock的时候不让mock打包到最终代码内
82+
injectCode: ` import { setupProdMockServer } from './mockProdServer'; setupProdMockServer(); `,
83+
}),
84+
vueSetupExtend(),
85+
eslintPlugin(),
86+
webUpdateNotice({ logVersion: true, injectFileBase: BASE_URL }),
87+
],
88+
server: {
89+
host: '0.0.0.0',
90+
open: true,
91+
port: 4646,
92+
},
93+
})
94+
}

0 commit comments

Comments
 (0)