javascript - 如何使用 node.js 将多个 javascript 文件合并为 1 个文件
问题描述
大家好,
我创建了一个包含 10 多个 js 文件的大型 JavaScript Projekt。现在我想将所有js文件的代码合并到一个文件中。我用 node.js 创建了一个测试文件,但它什么也没做,我不知道问题是什么。
var fs = require("fs");
var codes = ["M:/HTML-Projekte/ChabanicSouls/combineFunc/file1.js","M:/HTML-Projekte/ChabanicSouls/combineFunc/file2.js","M:/HTML-Projekte/ChabanicSouls/combineFunc/file3.js"];
var combined = "";
for(let x = 0; x < codes.length; x++) {
fs.readFile(codes[x], "UTF-8", function(err, data) {
if (err) {
throw err;
}
combined += data;
});
}
fs.writeFile('M:/HTML-Projekte/ChabanicSouls/combineFunc/ugly.js', combined , function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
如果你有一个idee,如何解决这个问题,请告诉我。
解决方案
我找到了自己解决问题的方法,
var fs = require("fs");
const codes = ["M:/HTML-Projekte/ChabanicSouls/original_Codes/chaVar.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaSocket.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaChat.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaVarMain.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaField.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaFriends.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaArchive.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaMain.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaTimer.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaActions.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaFight.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaKeys.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaLvUp.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaMarket.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaMove.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaNextPl.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaParaUp.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaSoul.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaGetFunc.js",
"M:/HTML-Projekte/ChabanicSouls/original_Codes/chaStart.js"];
function readIt() {
let combined = [];
let doneCheck = [];
let errVal = false;
for (let x = 0; x < codes.length; x++) {
doneCheck.push(false);
}
for (let x = 0; x < codes.length; x++) {
fs.readFile(codes[x], "UTF-8", function (err, data) {
if (err || !data) {
console.log(codes[x]);
} else {
combined.push(data);
doneCheck[x] = true;
}
if (x == (codes.length - 1)) {
saveIt(combined, doneCheck);
}
});
}
}
function saveIt(combined, doneCheck, round = 0) {
let counter = 0;
for (let x = 0; x < doneCheck.length; x++) {
if (doneCheck[x] == false) {
fs.readFile(codes[x], "UTF-8", function (err, data) {
if (err || !data) {
console.log(codes[x]);
} else {
combined.push(data);
doneCheck[x] = true;
}
if (x == (codes.length - 1)) {
return saveIt(combined, doneCheck, round);
}
});
} else {
counter++;
}
}
if (counter < doneCheck.length) {
return false;
}
let combined_string = "";
for (let y = 0; y < combined.length; y++) {
combined_string = combined_string + combined[y] + " ";
}
fs.writeFile('M:/HTML-Projekte/ChabanicSouls/ugly.js', combined_string, function (err) {
if (err) {
return readIt();
}
console.log("The file was saved!");
});
}
readIt();
这种方式非常有效:-)
推荐阅读
- kotlin - 我可以将检查 null 的时间更改为不同的方法吗?
- python - 在 TensorFlow 中过滤数据
- javascript - 如何用玩笑模拟 FileReader?
- python - 在项目选择问题中创建约束
- c++ - 我不能使用 mysql 数据库连接.C++ 同时使用插入和选择
- django - 在 Django 模板中禁用 {% debug %} 标签
- mysql - MySQL-Query - 无法获得正确的结果
- html - 在禁用的文本输入中显示长值
- angular - 无法初始化翻译服务错误 - Angular
- java - Mongodb和springboot如何运行特定查询