-
Notifications
You must be signed in to change notification settings - Fork 58
/
vite.config.ts
138 lines (135 loc) · 4.37 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
import path from "path"
import type { ConfigEnv } from "vite"
import { defineConfig, loadEnv } from "vite" // 帮手函数,这样不用 jsdoc 注解也可以获取类型提示
import vue from "@vitejs/plugin-vue"
import examplePlugin from "./build/vite-example-plugin"
import copyPlugin from "rollup-plugin-copy"
import monacoEditorPlugin from "vite-plugin-monaco-editor"
import eslintPlugin from "vite-plugin-eslint"
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: 3001
},
define: {
"process.env": {
mode,
BASE_URL: ENV.VITE_BASE_URL,
EXAMPLE_SOURCE_PATH: ENV.VITE_EXAMPLE_SOURCE_PATH,
EDITOR_MODE: ENV.VITE_EDITOR_MODE !== "0"
}
},
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
"@mars": path.join(__dirname, "src")
},
extensions: [".js", ".ts", ".jsx", ".tsx", ".json"]
},
optimizeDeps: {
exclude: ["mars3d-cesium"]
},
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: "example/assets-vue",
// 自定义底层的 Rollup 打包配置
rollupOptions: {
input: {
index: path.resolve(__dirname, "index.html"),
editor: path.resolve(__dirname, "editor-vue.html"),
read: path.resolve(__dirname, "read-vue.html")
},
output: {
chunkFileNames: "example/assets-vue/js/[name]-[hash].js",
entryFileNames: "example/assets-vue/js/[name]-[hash].js",
assetFileNames: "example/assets-vue/[ext]/[name]-[hash].[ext]"
}
},
// 当设置为 true, 构建后将会生成 manifest.json 文件
manifest: false,
// 用来指定是应用哪种混淆器 boolean | 'terser' | 'esbuild'
minify: "terser",
// 传递给 Terser 的更多 minify 选项
terserOptions: {},
// 设置为false 来禁用将构建好的文件写入磁盘
write: true,
// 默认情况下 若 outDir 在 root 目录下, 则 Vite 会在构建时清空该目录。
emptyOutDir: true
},
plugins: [
vue(),
eslintPlugin({ cache: 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`
}
}
]
}),
examplePlugin(mode),
monacoEditorPlugin({ publicPath: "example/assets-monaco" }),
{
...copyPlugin({
hook: "closeBundle",
targets: [
{
src: "src/components/**/*.vue",
dest: "dist/vue",
rename: (_name, _extension, fullPath) => {
return fullPath.split("components")[1]
}
},
{
src: "src/example/**/*.*",
dest: "dist/example",
rename: (_name, _extension, fullPath) => {
return fullPath.split("example")[1]
}
}
]
})
}
]
})
}