(优发娱乐官网) nodejs,想自己实现fis的inline功能,写出了以下代码,有没有更简洁的写法??

代码逻辑

  1. 创建两个文件

  2. 读入final_file并将其内容放入finaldata

  3. 将finaldata进行处理,将其引入的src_file文件抽出

  4. 读入src_file,并将其值赋给finaldata

  5. 再将finaldata写入final_file

 var fs = requirefs; var finalData; var arrayFile =[]; var arrayStr=[]; fs.writeFilesrc_file.js, fsdfsdfwsffffffffffffffffffffffffffffff, functionerr, fd { console.logcreate newe new_file; }; fs.writeFilefinal_file.js, __inline"src_file.js", functionerr, fd { console.logcreate newe new_file; }; fs.readFilefinal_file.js, utf8, functionerr, data { var finalData = data; arrayFile = findInlineArraydata; forvar i in arrayFile{ fs.readFilearrayFile[i],"utf8",functionerr, fd { finalData = fd; fs.writeFilefinal_file.js, finalData, functionerr, fd { console.logsuccess; }; }; } function findInlineArraydata { var arrayFile = []; var patt = /__inline["].*?=["]/; //但是这样的写法只能匹配出现一个inline的情况, //需要改进 var result = patt.execdata[0].replacepatt.execdata[1],; var tempReadFile; arrayFile.pushresult; return arrayFile; } };

不介意用同步的话可以这样。


const fs = requirefs fs.readFilefinal_file.js,err,content => { content = content.toString content = content.replace/_inline\s*["][^"]+["]s*/g, str,script => { if fs.existsSyncscript { try { return fs.readFileSyncscript,utf8 } catcherr { return str } } return str } fs.writeFilefinal_file.js, content
}

不喜欢同步的话,可以这样

const fs = requirefs var readFile = script => { return new Promiseresolve,reject => { fs.readFilescript, err,content => { if err { return resolvenull } resolvecontent } }
} readFilefinal_file.js .thencontent => { content = content.toString var promises = [] const INLINE_REG = /_inline\s*["][^"]+["]s*/g content.replaceINLINE_REG , str,script => { promises.pushreadFilescript } return Promise.allpromises .thencontents => { var i = 0 content = content.replaceINLINE_REG , str, script => { return contents[i++] || str.toString } return content } } .thencontent => { fs.writeFilefinal_file.dist.js, content } 

es7 async/await

最近刚好在学习nodeJS stream,所以借用楼上同学 @泡泡 的replace正则,用stream方式写了一个答案:

var fs = requirefs;
var replaceStream = requirereplacestream fs.writeFileSyncsrc_file.js, This is src_file.js content;
fs.writeFileSyncfinal_file.js, _inline"src_file.js"; fs.createReadStreamfinal_file.js .pipereplaceStream/_inline\s*["][^"]+["]s*/g, functionstr, script{ if fs.existsSyncscript { try { return fs.readFileSyncscript, utf8; } catcherr { return str; } } return str; } .pipefs.createWriteStreamfinal_file.js.tmp;

或者自己实现一个transform的stream,这样自己就可以在stream中随意定制了。

补充,这显然不是一个最佳方法,最好是可以在_transform方法中分段替换后,进行push,但是牵扯到被替换的关键字有可能被分为两段,这是一个难点,正在想办法,当然你可以使用上面的replacestream去处理。 stream方式在处理大文件时,优势是很明显的。

var stream = requirestream, util = requireutil, fs = requirefs; var Transform = stream.Transform; var Inline = function options { Transform.callthis, options; this._text = ;
}; util.inheritsInline, Transform; Inline.prototype._transform = function chunk, encoding, callback { var text = chunk.toStringutf8; this._text += text; callback;
}; Inline.prototype._flush = function callback { this._text = this._text.replace/_inline\s*["][^"]+["]s*/g, functionstr,script { if fs.existsSyncscript { try { return fs.readFileSyncscript,utf8 } catcherr { return str } } return str }; this.pushthis._text, utf8; callback;
}; fs.writeFileSyncsrc_file.js, This is src_file.js content;
fs.writeFileSyncfinal_file.js, _inline"src_file.js"; fs.createReadStreamfinal_file.js .pipenew Inline.pipefs.createWriteStreamfinal_file.js.tmp;

发表评论

电子邮件地址不会被公开。 必填项已用*标注