【使用的HBuilderX可视化创建项目开发】
发布时,修改了 manifest.json 里面的 H5的路径配置, 但是打包出来的还是上一次修改的, 并没有使用最新的, 这个该如何解决
这是vite.config.js 的配置文件, 通过配置 自定义发行 不同的环境来修改 manifest.json 里面的 H5的路径, 但是都是上一次的配置路径, 比如: 配置的 test环境 path: /test pro环境 path: /path
第一次打包,manifest.json中的路径是空的, 使用test打包就是空的, 再使用 pro打包出现的就是上一次test的路径
import {
defineConfig
} from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import fs from 'fs-extra';
import path from 'path'
const myTest = JSON.parse(process.env.UNI_CUSTOM_DEFINE) || 'test'
const BUILD_DIR = 'build'
function copyFile() {
return {
enforce: 'post',
async writeBundle() {
await fs.copy(
path.resolve(__dirname, 'unpackage/dist/build'),
path.join(
__dirname,
BUILD_DIR,
myTest.MY_TEST,
)
);
},
};
}
const manifestPath = path.resolve(__dirname, 'manifest.json');
function updateManifest() {
// 修改配置文件
let Manifest = fs.readFileSync(manifestPath, {
encoding: 'utf-8'
});
function replaceManifest(path, value) {
console.log('替换', 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');
console.log('Manifest', Manifest);
}
console.log('path:', `"/weblive/${myTest.MY_TEST}"`);
replaceManifest('h5.router.base', `"/weblive/${myTest.MY_TEST}"`);
fs.writeFileSync(manifestPath, Manifest, {
flag: 'w',
});
}
updateManifest()
// 自定义插件清空目录
function emptyDirPlugin() {
return {
name: 'empty-dir-plugin',
buildStart() {
console.log('开始清除====')
// const model = require('./manifest.json')
// console.log(model)
console.log('开始清除=========')
const dir = path.resolve(__dirname, 'unpackage/dist/build'); // 指定打包路径
const fs = require('fs');
try {
if (fs.existsSync(dir)) {
fs.rmdirSync(dir, {
recursive: true
}); // 递归删除目录
}
} catch (error) {
console.error('Error while emptying the directory:', error);
}
},
};
}
export default defineConfig({
plugins: [
uni(), copyFile(), emptyDirPlugin(),
],
resolve: {
alias: {
ENV: path.resolve(__dirname, `./common/env/${myTest.MY_TEST}.js`), // 这也是处理配置的一种
},
},
server: {
port: 3003,
proxy: {
}
}
})
使用test打包情况
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/assets/uni.9c20b024.css">
<meta charset="UTF-8" />
<script>
var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') ||
CSS.supports('top: constant(a)'))
document.write(
'<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +
(coverSupport ? ', viewport-fit=cover' : '') + '" />')
</script>
<title>WebLive</title>
<script type="module" crossorigin src="/assets/index-CLnecFIb.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-7edSVY4j.css">
</head>
<body>
<div id="app">
<!--app-html-->
</div>
</body>
</html>
1***@qq.com (作者)
不行, 就是看到打包 没有代入路径, 你用的是什么开发模式, 我用的是 HBuilderX可视化创建项目开发 , 不是通过命令创建的 你的vite.config.js 是如何配置的呢? 我是直接使用的官网给的例子 是否是我我的配置修改时机不对导致?
2024-05-09 14:25