[英]How to combine multiple javascript files to 1 file using node.js
Hallo everyone ,大家好,
I have created a big JavaScript Projekt with more than 10 js files.我创建了一个包含 10 多个 js 文件的大型 JavaScript 项目。 Now I want to combine the codes of all js files in 1 file.
现在我想将所有 js 文件的代码合并到 1 个文件中。 I have created a test file with node.js, but it does nothing and i dont know what the Problem is.
我用 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!"); });
If you have an Idee, how to solve this Problem, please let me know.如果您有 Idee,如何解决此问题,请告诉我。
I have found a way to solve my Problem by my self ,我找到了自己解决问题的方法,
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();
This way works perfectly :-)这种方式非常有效:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.