0

我正在尝试读取文件并将文件内容保存到变量中。然后我想使用该变量并连接到它并以其他方式解析它,然后使用 terser 将其缩小,但我不断收到错误消息:

(node:13708) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token: eof (undefined)

当我删除此行时,它可以工作:

fileContents = 'String.prototype.hexDecode = function(){var j;var hexes = this.match(/.{1,4}/g) || [];var back = "";for(j = 0; j<hexes.length; j++) {back += String.fromCharCode(parseInt(hexes[j], 16));}return back;}' + fileContents;

但是,删除它的问题是我将无法修改所需的字符串。我还需要在整个程序中多次执行此操作,然后我想缩小并将解析的完整输出写入文件。以下是相关代码:

var input = "input.txt";
function write(data) {
  if (!(output == undefined)) {
    fs.writeFile(output, data, (err) => {
      if (err) console.log(err);
    });
  }
}

async function mini() {

  var { minify } = require("terser");
  var fileContents = fs.readFileSync(input).toString();
  fileContents = 'String.prototype.hexDecode = function(){var j;var hexes = this.match(/.{1,4}/g) || [];var back = "";for(j = 0; j<hexes.length; j++) {back += String.fromCharCode(parseInt(hexes[j], 16));}return back;}' + fileContents;


  var result = await minify(fileContents, { sourceMap: true });
  //write(fileContents);
  write(result.code)

}
4

0 回答 0