Files
tl-rtc-file/svr/build/webpack/comm/util.js
https://blog.iamtsm.cn 69a43cce7d feat: 目录结构优化重整
feat: 支持单独发送文件
feat: 支持文本私聊
feat: 支持seafile网盘暂存文件
feat: 支持直播房间
feat: 支持取件码下载文件
feat: 支持预览视频文件
feat: 支持报错告警
feat: 调整补充启动logo
feat: 调整补充免责协议
feat: 调整补充配置中的版本号
feat: 调整优化开源协议
feat: 调整补充定制收费服务
feat: 调整优化服务端代码
feat: 调整优化批量发送逻辑
feat: 调整优化样式体验
feat: 调整优化conf中ws, manage相关配置
feat: 调整优化文件发送时间间隔为1秒钟
feat: 调整优化文件发送体验
feat: 调整优化选择文件逻辑
feat: 调整优化启动文件/命令
feat: 调整优化socket配置区分
feat: 调整优化分享进入房间
feat: 调整优化右上角消息提示
feat: 调整删除npm依赖
feat: 调整删除首次弹窗
feat: 即将支持远程cavas画笔
feat: 即将支持远程控制
2023-06-02 00:16:39 +08:00

180 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const glob = require('glob');
const path = require("path");
const fs = require("fs");
const readline = require('readline')
const Concat = require('concat-with-sourcemaps');
let jsCount = 1;
let jsIsFirst = true;
const Log = require("./log");
const {
JS_PATH,
CSS_PATH,
} = require("./path");
// 将后缀 .src.css .src.js .js .css 截断
const removeExt = (pathname) => {
let idx = -1;
if (pathname.indexOf('.src.') !== -1) {
idx = pathname.indexOf('.src.');
} else {
idx = pathname.indexOf('.js') === -1 ? pathname.indexOf('.css') : pathname.indexOf('.js');
}
if (idx === -1) {
return pathname;
} else {
return pathname.substring(0, idx);
}
};
// 将 \ 反斜杠 换成 / 正斜杠
const unifiedSlash = (pathname) => {
return pathname.replace(/\\/g, '/');
};
// 执行编译后的回调函数
const startCompiler = (err, stats, resolve, reject) => {
return new Promise((res)=>{
if (err) {
Log.error(err.stack || err);
if (err.details) {
Log.error(err.details);
}
typeof reject === "function" && reject();
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
Log.error('webpack', '编译错误,错误信息如下:');
info.errors.forEach(err => {
Log.error('error', err);
});
typeof reject === 'function' && reject();
}
if (stats.hasWarnings()) {
Log.warn('webpack', '编译提醒信息如下:');
info.warnings.forEach(warning => {
Log.warn(warning)
})
}
typeof resolve === 'function' && resolve();
Log.log(stats.toString({
hash: true,
timings: true,
version: true,
cached: false,
cachedAssets: false,
colors: true,
modules: false,
children: false,
resons: false,
source: false,
chunks: false,
chunkModules: false,
entrypoints: false
}) + '\n\n');
res();
})
};
/**
* 文件是否更新
* @param pathArr
*/
const watchFile = ( pathArr ) => {
if(!pathArr || !(pathArr instanceof Array)){
return;
}
for (let pathArrKey in pathArr) {
fs.watch(pathArr[pathArrKey],(event,filename)=>{
if (filename && event === 'change') {
buildAllFListJS(['features.js','template.js']);
}
})
}
};
const startCompilerCss = (err, stats, resolve, reject) => {
return startCompiler(err, stats, resolve, reject);
};
const startCompilerJs = (err, stats, resolve, reject) => {
return startCompiler(err, stats, resolve, reject);
};
function transformFListFileToArray(partitionFilePath) {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: fs.createReadStream(partitionFilePath),
terminal: false,
crlfDelay: Infinity,
});
const files = [];
rl.on('line', (line) => {
if (!line.includes("<!--")) {
files.push(line)
}
});
rl.on('close', function () {
resolve(files);
})
})
}
/**
* 合并某个目录下的js到js目录下
*/
function buildSingleFListJs(pathname = '') {
if (pathname === '') return buildAllFListJS();
const _flist = path.resolve(pathname, './_flist')
const outputFileName = path.basename(pathname).replace('.js', '.src.js');
/**
* 按照_flist的顺序合并为一个src文件src文件统一放到flist下面
*/
transformFListFileToArray(_flist).then((files) => {
const concat = new Concat(false, outputFileName, '\n');
files.forEach(filename => {
concat.add(filename, fs.readFileSync(path.resolve(pathname, `./${filename}`)))
});
fs.writeFile(path.resolve(pathname, `../single/${outputFileName}`), concat.content, (err) => {
if (err) throw err
});
})
}
/**
* 合并目录下所有的xx.js的目录
*/
function buildAllFListJS(pathArr = []) {
if(pathArr === undefined || pathArr.length === 0){
return;
}
for(let i = 0; i < pathArr.length; i++){
glob.sync(path.resolve(JS_PATH, pathArr[i])).forEach(buildSingleFListJs);
}
}
module.exports = {
unifiedSlash,
startCompilerCss,
startCompilerJs,
startCompiler,
removeExt,
buildAllFListJS,
watchFile
};