当前位置:   article > 正文

uniapp vite 动态修改manifest.json_vite manifest.json

vite manifest.json

1、modifyManifest.ts

// 读取 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');
}

export const modifyManifest = ((mode = 'prod')=>{
	const BASEMAP = Object.freeze({
		'dev':'/dev/',
		'prod':'/prod/',
	}) 
	replaceManifest('h5.router.base',`"${BASEMAP[mode]}"`);
	fs.writeFileSync(manifestPath, Manifest, {
		flag: 'w',
	});
})
  • 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

2、vite.config.ts

import { defineConfig } from "vite";
import uni from '@dcloudio/vite-plugin-uni';
import { modifyManifest } from './modifyManifest';

export default defineConfig(({ mode }: Record<string, any>) => {
  modifyManifest(mode)
  return {
  		plugins: [uni()],
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3、指路=>官方文档:https://uniapp.dcloud.net.cn/collocation/vite-config.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读