gulp-file-include
gulp-file-include copied to clipboard
if-statements leave newlines
I'm having a hard time getting clean results with fileinclude because if statements leaves \n characters behind all over. Even if the condition is not met fileinclude will add two empty rows in the build file (corresponding to the two rows with curly brackets that contain the if-statement)
I've tried adding String.trim() to the result body in the conditionalHandler, but none of my hacks have been successful. Is there any way around this?
I have a similar issue, perhaps our problems are connected. I tried to get some help here first.
http://stackoverflow.com/questions/35200386/gulp-file-include-adds-artefacts-before-every-include
It looks like a BUG :cry: , I'll take a look this weekend. PR welcome :)
I found a solution that sort of worked for me. I'm not suggesting it's the real solution though (RegEx and advanced string manipulation isn't my expertise). Below is the part of replace-operator.js I changed, with my additions commented with // Hack
Edit: It doesn't work with when several if-statements follow each other. I'm sure it introduces more bugs. I would love a real fix for this!
while (matchStart = regexpStart.exec(content)) {
startEnd = matchStart.index + matchStart[0].length;
matchBody = balanced('{', '}', content.slice(startEnd - 1))
if (matchBody && matchBody.start === 0) {
matchEnd = regexpEnd ? regexpEnd.exec(matchBody.post) : true;
if (matchEnd) {
before = content.slice(0, matchStart.index);
if(before.trim() == "") before = ""; // Hack: Added trim() here
matchEnd = regexpEnd ? matchEnd[0].length : 0;
replacement = opts.handler({
before: before,
args: matchStart[1],
body: matchBody.body,
});
if (replacement !== undefined) {
result += before + parse(replacement, opts);
content = content.slice(startEnd + matchBody.end + matchEnd)
continue;
}
}
}
result += content.slice(0, startEnd);
content = content.slice(startEnd);
}
result += content;
return result.trim(); // Hack: Added trim() here
that also resolved my issue! :+1:
@tommypreger This had just recently started happening to me but your 'hack' also resolved my issue. Looks like there are a few reported issues, for ex here that are yet to be addressed.