我遇到了问题:变量output
不会改变应有的方式。
在while
循环中记录它时,正在编辑的它显示正常。
但是当在循环之外再次记录它时,变化从未发生过。
这是我的代码:
(function() {
var foot, fs, head, inputFile, output, outputFile;
fs = require('fs');
head = "var setStyles = function(){";
foot = "}";
inputFile = "css/index.css";
outputFile = "compiled/index.js";
output = head;
fs.readFile(inputFile, 'utf8', function(err, data) {
var css, identifier, match, prop, regex, results, selector, styleMatch, styleRegex, styles, tag, val;
if (err) {
console.log(err);
}
css = data;
regex = /(\.|#)?([a-zA-Z-_0-9]+)\n*{([\s\S]+?)}/;
results = [];
while (match = css.match(regex)) {
identifier = match[1];
tag = match[2];
styles = match[3];
if (identifier === ".") {
selector = "document.getElementsByClassName(" + tag + ");";
} else if (identifier === "#") {
selector = "document.getElementById(" + tag + ")";
} else {
selector = "document.getElementsByTagName(" + tag + ")";
}
output = "";
styleRegex = /([a-zA-Z-_]+?):\s*(["'#-_a-zA-Z0-9]+?)\s*;/;
while (styleMatch = styles.match(styleRegex)) {
prop = styleMatch[1];
val = styleMatch[2];
styles = styles.replace(styleMatch[0], "");
}
results.push(css = css.replace(match[0], ""));
}
return results;
});
console.log(output);
output += "\n" + foot;
fs.writeFile(outputFile, output, function(err) {
if (err) {
return console.log(err);
}
});
}).call(this);
有谁能告诉我这里我做错了什么?
提前致谢, 约迪