-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.js
57 lines (56 loc) · 1.9 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { defineConfig } from 'vite'
import legacy from '@vitejs/plugin-legacy'
import { createVuePlugin } from 'vite-plugin-vue2'
import viteCompression from 'vite-plugin-compression';
import viteRequireContext from '@originjs/vite-plugin-require-context'
import fs from 'fs'
import path from 'path';
import dotenv from 'dotenv'
const HOST = "0.0.0.0"
const REPLACEMENT = `${path.resolve(__dirname, './src')}/`
export default ( /** if you want to use mode : { mode }*/ ) => {
const NODE_ENV = process.env.NODE_ENV || 'development'
const envFiles = [
`.env.${NODE_ENV}`
]
for (const file of envFiles) {
const envConfig = dotenv.parse(fs.readFileSync(file))
for (const k in envConfig) {
process.env[k] = envConfig[k]
}
}
return defineConfig({
base: "./",
server: {
host: HOST,
port: process.env.VITE_PORT,
},
resolve: {
alias: [{
find: '@/',
replacement: REPLACEMENT,
},
{
find: 'src/',
replacement: REPLACEMENT,
},
// 适配 ~/xxxx
{ find: /* ~/ */ /^~(?=\/)/, replacement: path.join(__dirname, 'node_modules') },
// 适配 ~xxxx
{ find: /* ~ */ /^~(?!\/)/, replacement: path.join(__dirname, 'node_modules/') },
],
// 同 webpack 中的 extensions
extensions: ['.vue', '.js', '.jsx', '.ts', '.tsx', '.json'],
},
plugins: [
createVuePlugin( /* options */ ),
legacy({
targets: ['ie >= 11'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
}),
viteCompression(),
// 处理 webpack 项目中 require.context 写法
viteRequireContext()
],
})
}