Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_36567140
unidocs-zh
提交
80d2dda6
U
unidocs-zh
项目概览
qq_36567140
/
unidocs-zh
与 Fork 源项目一致
Fork自
DCloud / unidocs-zh
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
unidocs-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
80d2dda6
编写于
6月 10, 2022
作者:
L
liuxiaohang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
chore: vite-config.md
上级
1d09bbdd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
179 addition
and
0 deletion
+179
-0
docs/collocation/_sidebar.md
docs/collocation/_sidebar.md
+1
-0
docs/collocation/vite-config.md
docs/collocation/vite-config.md
+177
-0
docs/collocation/vue-config.md
docs/collocation/vue-config.md
+1
-0
未找到文件。
docs/collocation/_sidebar.md
浏览文件 @
80d2dda6
...
...
@@ -2,6 +2,7 @@
*
[
manifest.json 应用配置
](
collocation/manifest.md
)
*
[
package.json
](
collocation/package.md
)
*
[
vue.config.js
](
collocation/vue-config.md
)
*
[
vite.config.js
](
collocation/vite-config.md
)
*
[
uni.scss
](
collocation/uni-scss.md
)
*
[
App.vue
](
collocation/App.md
)
*
[
main.js
](
collocation/main.md
)
...
...
docs/collocation/vite-config.md
0 → 100644
浏览文件 @
80d2dda6
vite.config.js 是一个可选的配置文件,如果项目的根目录中存在这个文件,那么它会被自动加载,一般用于配置 vite 的编译选项,具体规范参考:
[
vite.config.js
](
https://cn.vitejs.dev/
)
**支持情况**
-
CLI 工程
[
创建教程
](
/quickstart-cli
)
-
HBuilderX 3.2.0 及以上版本
**注意事项**
-
仅
`vue 3`
项目生效
部分配置项会被编译配置覆盖,例如:
-
base:支持
-
root:不支持
-
mode:不支持
-
publicDir: 不支持,固定为 static
-
build.outDir:不支持
-
build.assetsInlineLimit:仅 h5 支持
-
build.cssCodeSplit:不支持,固定为 true
-
build.lib:不支持
-
build.manifest:不支持
-
build.ssrManifest:不支持
-
build.ssr:不支持
**基础内容**
> 必须引用 '@dcloudio/vite-plugin-uni' 并且添加到 plugins 中
```
js
import
{
defineConfig
}
from
'
vite
'
;
import
uni
from
'
@dcloudio/vite-plugin-uni
'
;
export
default
defineConfig
({
plugins
:
[
uni
()],
});
```
**使用示例**
**自定义静态资源目录**
```
js
import
path
from
'
path
'
;
import
fs
from
'
fs-extra
'
;
import
{
defineConfig
}
from
'
vite
'
;
import
uni
from
'
@dcloudio/vite-plugin-uni
'
;
function
copyFile
()
{
return
{
enforce
:
'
post
'
,
async
writeBundle
()
{
await
fs
.
copy
(
path
.
resolve
(
__dirname
,
'
images
'
),
path
.
join
(
__dirname
,
'
unpackage/dist
'
,
process
.
env
.
NODE_ENV
===
'
production
'
?
'
build
'
:
'
dev
'
,
process
.
env
.
UNI_PLATFORM
,
'
images
'
)
);
},
};
}
export
default
defineConfig
({
plugins
:
[
uni
(),
copyFile
()],
});
```
**注入全局依赖**
```
js
// 示例从插件市场下载 mp-storage
import
path
from
'
path
'
;
import
{
defineConfig
}
from
'
vite
'
;
import
uni
from
'
@dcloudio/vite-plugin-uni
'
;
import
inject
from
'
@rollup/plugin-inject
'
;
const
mpStoragePath
=
path
.
resolve
(
__dirname
,
'
./js_sdk/mp-storage/mp-storage
'
);
export
default
defineConfig
({
plugins
:
[
uni
(),
inject
({
localStorage
:
[
mpStoragePath
,
'
localStorage
'
],
'
window.localStorage
'
:
[
mpStoragePath
,
'
localStorage
'
],
}),
],
define
:
{
'
process.env.VUE_APP_TEST
'
:
JSON
.
stringify
(
'
test
'
),
},
});
```
**配置环境变量**
```
js
import
{
defineConfig
}
from
'
vite
'
;
import
uni
from
'
@dcloudio/vite-plugin-uni
'
;
export
default
defineConfig
({
plugins
:
[
uni
()],
define
:
{
'
process.env.VUE_APP_TEST
'
:
JSON
.
stringify
(
'
test
'
),
},
});
```
**发布时删除 console**
```
js
import
{
defineConfig
}
from
'
vite
'
;
import
uni
from
'
@dcloudio/vite-plugin-uni
'
;
export
default
defineConfig
({
plugins
:
[
uni
()],
build
:
{
minify
:
'
terser
'
,
terserOptions
:
{
compress
:
{
drop_console
:
true
,
},
},
},
});
```
**发布时动态修改 manifest.json**
```
js
// 读取 manifest.json ,修改后重新写入
const
fs
=
require
(
'
fs
'
);
const
manifestPath
=
'
./src/manifest.json
'
;
let
Manifest
=
fs
.
readFileSync
(
manifestPath
,
{
encoding
:
'
utf-8
'
});
function
replaceManifest
(
path
,
value
)
{
const
arr
=
path
.
split
(
'
.
'
);
const
len
=
arr
.
length
;
const
lastItem
=
arr
[
len
-
1
];
let
i
=
0
;
let
ManifestArr
=
Manifest
.
split
(
/
\n
/
);
for
(
let
index
=
0
;
index
<
ManifestArr
.
length
;
index
++
)
{
const
item
=
ManifestArr
[
index
];
if
(
new
RegExp
(
`"
${
arr
[
i
]}
"`
).
test
(
item
))
++
i
;
if
(
i
===
len
)
{
const
hasComma
=
/,/
.
test
(
item
);
ManifestArr
[
index
]
=
item
.
replace
(
new
RegExp
(
`"
${
lastItem
}
"[\\s\\S]*:[\\s\\S]*`
),
`"
${
lastItem
}
":
${
value
}${
hasComma
?
'
,
'
:
''
}
`
);
break
;
}
}
Manifest
=
ManifestArr
.
join
(
'
\n
'
);
}
// 使用
replaceManifest
(
'
app-plus.usingComponents
'
,
false
);
replaceManifest
(
'
app-plus.splashscreen.alwaysShowBeforeRender
'
,
false
);
replaceManifest
(
'
mp-baidu.usingComponents
'
,
false
);
fs
.
writeFileSync
(
manifestPath
,
Manifest
,
{
flag
:
'
w
'
,
});
export
default
defineConfig
({
// ...
});
```
启用压缩的方法:
-
HBuilderX 创建的项目勾选运行-->运行到小程序模拟器-->运行时是否压缩代码
-
cli 创建的项目可以在
`package.json`
中添加参数
`--minify`
docs/collocation/vue-config.md
浏览文件 @
80d2dda6
...
...
@@ -45,6 +45,7 @@ module.exports = {
**注入全局依赖**
```
js
// 示例从插件市场下载 mp-storage
const
webpack
=
require
(
'
webpack
'
)
module
.
exports
=
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录