From 002c3be3986afe6642170381106e282a3ba03c12 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Sat, 9 Oct 2021 18:40:02 +0800 Subject: [PATCH] docs: vue.config.json --- docs/collocation/vue-config.md | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/docs/collocation/vue-config.md b/docs/collocation/vue-config.md index 9be680c10..8c6b5418d 100644 --- a/docs/collocation/vue-config.md +++ b/docs/collocation/vue-config.md @@ -98,6 +98,47 @@ module.exports = { } ``` +**发布时动态修改 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" +}) + +module.exports = { + // ... +} +``` + 启用压缩的方法: - HBuilderX创建的项目勾选运行-->运行到小程序模拟器-->运行时是否压缩代码 - cli创建的项目可以在`package.json`中添加参数`--minimize`,示例:`"dev:mp-weixin": "cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch --minimize"` -- GitLab