-
Notifications
You must be signed in to change notification settings - Fork 20
/
vite.config.ts
143 lines (140 loc) · 4.64 KB
/
vite.config.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import path from "path"
import type { ConfigEnv } from "vite"
import { defineConfig, loadEnv } from "vite" // 帮手函数,这样不用 jsdoc 注解也可以获取类型提示
import vue from "@vitejs/plugin-vue"
// import legacy from "@vitejs/plugin-legacy"
import eslintPlugin from "vite-plugin-eslint"
import { mars3dPlugin } from "vite-plugin-mars3d"
import { createStyleImportPlugin, AndDesignVueResolve } from "vite-plugin-style-import"
export default ({ mode }: ConfigEnv) => {
const root = process.cwd()
// 获取 .env 文件里定义的环境变量
const ENV = loadEnv(mode, root)
console.log(`当前环境信息:`, mode)
console.log(`ENV:`, ENV)
return defineConfig({
base: ENV.VITE_BASE_URL,
server: {
host: "localhost",
port: 3002
},
define: {
"process.env": {
mode,
BASE_URL: ENV.VITE_BASE_URL,
API_BASE: ENV.VITE_API_URL,
API_LOCAL: ENV.VITE_LOCAL_API
},
buildTime: new Date()
},
resolve: {
alias: {
"@mars": path.join(__dirname, "src")
},
extensions: [".js", ".ts", ".jsx", ".tsx", ".json"]
},
optimizeDeps: {
include: ["kml-geojson"]
},
json: {
// 支持从 .json 文件中进行按名导入
namedExports: true,
stringify: false
},
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
additionalData: `@import "${path.resolve(__dirname, "src/components/mars-ui/base.less")}";`
}
}
},
build: {
// 输出路径
outDir: path.join("./dist", ENV.VITE_BASE_URL),
// 小于此阈值的导入或引用资源将内联为 base64 编码, 以避免额外的http请求, 设置为 0, 可以完全禁用此项,
assetsInlineLimit: 4096,
// 启动 / 禁用 CSS 代码拆分
cssCodeSplit: true,
// 构建后是否生成 soutrce map 文件
sourcemap: false,
// 自定义rollup-commonjs插件选项
commonjsOptions: {
include: /node_modules|packages/
},
// 静态资源目录
assetsDir: "assets",
// 自定义底层的 Rollup 打包配置
rollupOptions: {
input: {
index: path.resolve(__dirname, "index.html"),
demo: path.resolve(__dirname, "demo.html")
},
output: {
chunkFileNames: "assets/js/[name]-[hash].js",
entryFileNames: "assets/js/[name]-[hash].js",
assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
}
},
// 当设置为 true, 构建后将会生成 manifest.json 文件
manifest: false,
// 设置为 false 可以禁用最小化混淆,或是用来指定是应用哪种混淆器 boolean | 'terser' | 'esbuild'
minify: "terser",
// 传递给 Terser 的更多 minify 选项
terserOptions: {
compress: {
drop_console: false, // 在生产环境移除console.log
drop_debugger: true // 在生产环境移除debugger
}
},
// 设置为false 来禁用将构建好的文件写入磁盘
write: true,
// 默认情况下 若 outDir 在 root 目录下, 则 Vite 会在构建时清空该目录。
emptyOutDir: true
},
plugins: [
vue(),
// 兼容老版本浏览器配置
// legacy({
// targets: ["> 5%", "last 2 major versions", "chrome >80", "not dead"], // 需要兼容的目标列表,可以设置多个,参考.browserslistrc等
// additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
// renderLegacyChunks: true,
// polyfills: [
// "es.symbol",
// "es.array.filter",
// "es.promise",
// "es.promise.finally",
// "es/map",
// "es/set",
// "es.array.for-each",
// "es.object.define-properties",
// "es.object.define-property",
// "es.object.get-own-property-descriptor",
// "es.object.get-own-property-descriptors",
// "es.object.keys",
// "es.object.to-string",
// "web.dom-collections.for-each",
// "esnext.global-this",
// "esnext.string.match-all"
// ]
// }),
eslintPlugin(),
mars3dPlugin({ useStatic: false }),
createStyleImportPlugin({
resolves: [AndDesignVueResolve()],
libs: [
{
libraryName: "ant-design-vue",
esModule: true,
resolveStyle: (name) => {
if (name === "auto-complete") {
return `ant-design-vue/es/${name}/index`
}
return `ant-design-vue/es/${name}/style/index`
}
}
]
})
]
})
}