- uniapp编译打包组件引用为绝对路径,作为子包需要二次编译引入路径
main();
function main() {
const allFiles = getAllFiles('dist/build/yourBuildPath');
for (let i = 0; i < allFiles.length; i++) {
// console.log(allFiles[i]);
// 同步读取文件内容
handleJSONFile(allFiles[i]);
}
}
/**
* 递归遍历,获取指定文件夹下面的所有文件路径
*/
function getAllFiles(filePath) {
let allFilePaths = [];
if (fs.existsSync(filePath)) {
const files = fs.readdirSync(filePath);
for (let i = 0; i < files.length; i++) {
const file = files[i]; // 文件名称(不包含文件路径)
const currentFilePath = `${filePath}/${file}`;
const stats = fs.lstatSync(currentFilePath);
if (/components|pages|node-modules/.test(currentFilePath)) {
if (stats.isDirectory()) {
allFilePaths = allFilePaths.concat(getAllFiles(currentFilePath));
} else {
if (/.json/.test(currentFilePath)) {
allFilePaths.push(currentFilePath);
}
}
}
}
} else {
console.warn(`指定的目录${filePath}不存在!`);
}
return allFilePaths;
}
function handleJSONFile(filePath) {
fs.readFile(filePath, 'utf8', function(err, data) {
const jsonData = JSON.parse(data);
console.log(filePath, jsonData);
if (jsonData.usingComponents && typeof jsonData.usingComponents === 'object') {
const usingComponents = {};
Object.keys(jsonData.usingComponents).forEach((keys) => {
usingComponents[keys] = `/yourSubpackagePath${jsonData.usingComponents[keys]}`;
});
jsonData.usingComponents = usingComponents;
}
fs.writeFileSync(filePath, JSON.stringify(jsonData));
});
}
0 个评论
要回复文章请先登录或注册