Skip to content

Commit

Permalink
strip UTF-8 BOM when reading text files. Allow bundler.cmd to specify…
Browse files Browse the repository at this point in the history
… user-defined directories to look in.
  • Loading branch information
mythz committed Feb 7, 2012
1 parent ae810fc commit 8d00f38
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 52 deletions.
2 changes: 1 addition & 1 deletion NuGet/bundler.nuspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>Bundler</id> <id>Bundler</id>
<version>0.98</version> <version>0.99</version>
<authors>Demis Bellot</authors> <authors>Demis Bellot</authors>
<owners>Demis Bellot</owners> <owners>Demis Bellot</owners>
<summary>CoffeeScript, JavaScript, Less, Sass, Css compiler, minifier and combiner for ASP.NET MVC websites.</summary> <summary>CoffeeScript, JavaScript, Less, Sass, Css compiler, minifier and combiner for ASP.NET MVC websites.</summary>
Expand Down
2 changes: 1 addition & 1 deletion NuGet/content/bundler/bundler.cmd
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
node bundler.js node bundler.js ../Content ../Scripts
129 changes: 79 additions & 50 deletions NuGet/content/bundler/bundler.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ SOFTWARE.
*/ */


//recursively scans the directory below for *.js.bundle and *.css.bundle files //recursively scans the directory below for *.js.bundle and *.css.bundle files
var SCAN_ROOT_DIR = "../Content"; var SCAN_ROOT_DIRS = process.argv.splice(2); //directories specified in bundler.cmd
if (!SCAN_ROOT_DIRS.length) {
console.log("No directories were specified.");
console.log("Usage: bundler.cmd ../Content ../Scripts");
return;
}


var fs = require("fs"), var fs = require("fs"),
path = require("path"), path = require("path"),
Expand All @@ -35,8 +40,6 @@ var fs = require("fs"),
cleanCss = require('clean-css'), cleanCss = require('clean-css'),
Step = require('step'); Step = require('step');


//console.log(sass.render('body\n a\n :color #fff'));

String.prototype.startsWith = function (str){ String.prototype.startsWith = function (str){
return this.indexOf(str) === 0; return this.indexOf(str) === 0;
}; };
Expand All @@ -53,9 +56,9 @@ var walk = function (dir, done) {
var file = list[i++]; var file = list[i++];
if (!file) return done(null, results); if (!file) return done(null, results);
file = dir + '/' + file; file = dir + '/' + file;
fs.stat(file, function (err, stat) { fs.stat(file, function (_, stat) {
if (stat && stat.isDirectory()) { if (stat && stat.isDirectory()) {
walk(file, function (err, res) { walk(file, function (_, res) {
results = results.concat(res); results = results.concat(res);
next(); next();
}); });
Expand All @@ -68,26 +71,45 @@ var walk = function (dir, done) {
}); });
}; };


walk(SCAN_ROOT_DIR, function (err, allFiles) { var scanIndex = 0;
if (err) throw err; (function scanNext() {
var jsBundles = allFiles.filter(function(file) { return file.endsWith(".js.bundle"); }); if (scanIndex < SCAN_ROOT_DIRS.length) {
var cssBundles = allFiles.filter(function(file) { return file.endsWith(".css.bundle"); }); var rootDir = SCAN_ROOT_DIRS[scanIndex++];
path.exists(rootDir, function(exists) {
if (exists) {
walk(rootDir, function(err, allFiles) {
if (err) throw err;
scanDir(allFiles, scanNext);
});
} else {
console.log("\nSpecified dir '" + rootDir + "' does not exist, skipping...");
scanNext();
}
});
} else
console.log("\nDone.");
})();

function scanDir(allFiles, cb) {

var jsBundles = allFiles.filter(function (file) { return file.endsWith(".js.bundle"); });
var cssBundles = allFiles.filter(function (file) { return file.endsWith(".css.bundle"); });


Step( Step(
function () { function () {
var next = this; var next = this;
var index = 0; var index = 0;
var nextBundle = function() { var nextBundle = function () {
if (index == jsBundles.length) if (index < jsBundles.length)
next();
else
processBundle(jsBundles[index++]); processBundle(jsBundles[index++]);
else
next();
}; };
function processBundle(jsBundle) { function processBundle(jsBundle) {
var bundleDir = path.dirname(jsBundle); var bundleDir = path.dirname(jsBundle);
var bundleName = jsBundle.replace('.bundle', ''); var bundleName = jsBundle.replace('.bundle', '');
fs.readFile(jsBundle, 'utf-8', function (_, data) { readTextFile(jsBundle, function (data) {
var jsFiles = data.toString().replace("\r", "").split("\n"); var jsFiles = removeCR(data).split("\n");
processJsBundle(jsBundle, bundleDir, jsFiles, bundleName, nextBundle); processJsBundle(jsBundle, bundleDir, jsFiles, bundleName, nextBundle);
}); });
}; };
Expand All @@ -97,27 +119,24 @@ walk(SCAN_ROOT_DIR, function (err, allFiles) {
var next = this; var next = this;
var index = 0; var index = 0;
var nextBundle = function () { var nextBundle = function () {
if (index == cssBundles.length) if (index < cssBundles.length)
next();
else
processBundle(cssBundles[index++]); processBundle(cssBundles[index++]);
else
next();
}; };
function processBundle(cssBundle) { function processBundle(cssBundle) {
var bundleDir = path.dirname(cssBundle); var bundleDir = path.dirname(cssBundle);
var bundleName = cssBundle.replace('.bundle', ''); var bundleName = cssBundle.replace('.bundle', '');
fs.readFile(cssBundle, 'utf-8', function(_, data) { readTextFile(cssBundle, function (data) {
var cssFiles = data.toString().replace("\r", "").split("\n"); var cssFiles = removeCR(data).split("\n");
processCssBundle(cssBundle, bundleDir, cssFiles, bundleName, nextBundle); processCssBundle(cssBundle, bundleDir, cssFiles, bundleName, nextBundle);
}); });
}; };
nextBundle(); nextBundle();
}, },
function () { cb
console.log("\nDone.");
}
); );

}
});


function processJsBundle(jsBundle, bundleDir, jsFiles, bundleName, cb) { function processJsBundle(jsBundle, bundleDir, jsFiles, bundleName, cb) {


Expand All @@ -129,14 +148,12 @@ function processJsBundle(jsBundle, bundleDir, jsFiles, bundleName, cb) {


var allJs = "", allMinJs = ""; var allJs = "", allMinJs = "";
for (var i = 0; i < allJsArr.length; i++) { for (var i = 0; i < allJsArr.length; i++) {
allJs += allJsArr[i] + ";"; allJs += ";" + allJsArr[i] + "\n";
allMinJs += allMinJsArr[i] + ";"; allMinJs += ";" + allMinJsArr[i] + "\n";
} }


fs.writeFile(bundleName, allJs, function (_) { fs.writeFile(bundleName, allJs, function (_) {
fs.writeFile(bundleName.replace(".js", ".min.js"), allMinJs, function (_) { fs.writeFile(bundleName.replace(".js", ".min.js"), allMinJs, cb);
cb();
});
}); });
}; };


Expand All @@ -157,13 +174,11 @@ function processJsBundle(jsBundle, bundleDir, jsFiles, bundleName, cb) {
function () { function () {
var next = this; var next = this;
if (isCoffee) { if (isCoffee) {
fs.readFile(filePath, 'utf-8', function (_, coffee) { readTextFile(filePath, function (coffee) {
getOrCreateJs(coffee, filePath, jsPath, next); getOrCreateJs(coffee, filePath, jsPath, next);
}); });
} else { } else {
fs.readFile(jsPath, 'utf-8', function (_, js) { readTextFile(jsPath, next);
next(js);
});
} }
}, },
function (js) { function (js) {
Expand Down Expand Up @@ -192,9 +207,7 @@ function processCssBundle(cssBundle, bundleDir, cssFiles, bundleName, cb) {
} }


fs.writeFile(bundleName, allCss, function(_) { fs.writeFile(bundleName, allCss, function(_) {
fs.writeFile(bundleName.replace(".css", ".min.css"), allMinCss, function (_) { fs.writeFile(bundleName.replace(".css", ".min.css"), allMinCss, cb);
cb();
});
}); });
}; };


Expand All @@ -218,17 +231,15 @@ function processCssBundle(cssBundle, bundleDir, cssFiles, bundleName, cb) {
function () { function () {
var next = this; var next = this;
if (isLess) { if (isLess) {
fs.readFile(filePath, 'utf-8', function (_, less) { readTextFile(filePath, function (lessText) {
getOrCreateLessCss(less, filePath, cssPath, next); getOrCreateLessCss(lessText, filePath, cssPath, next);
}); });
} else if (isSass) { } else if (isSass) {
fs.readFile(filePath, 'utf-8', function (_, sassText) { readTextFile(filePath, function (sassText) {
getOrCreateSassCss(sassText, filePath, cssPath, next); getOrCreateSassCss(sassText, filePath, cssPath, next);
}); });
} else { } else {
fs.readFile(cssPath, 'utf-8', function (_, css) { readTextFile(cssPath, next);
next(css);
});
} }
}, },
function (css) { function (css) {
Expand Down Expand Up @@ -261,8 +272,7 @@ function getOrCreateLessCss(less, lessPath, cssPath, cb /*cb(css)*/) {


function getOrCreateSassCss(sassText, sassPath, cssPath, cb /*cb(sass)*/) { function getOrCreateSassCss(sassText, sassPath, cssPath, cb /*cb(sass)*/) {
compileAsync("compiling", function (sassText, sassPath, cb) { compileAsync("compiling", function (sassText, sassPath, cb) {
var cleanSass = sassText.replace( /\r/g , ""); cb(sass.render(removeCR(sassText), { options: path.basename(sassPath) }));
cb(sass.render(cleanSass, { options: path.basename(sassPath) }));
}, sassText, sassPath, cssPath, cb); }, sassText, sassPath, cssPath, cb);
} }


Expand Down Expand Up @@ -304,9 +314,7 @@ function compileAsync(mode, compileFn /*compileFn(text, textPath, cb(compiledTex
} }
} }
else { else {
fs.readFile(compileTextPath, 'utf-8', function (_, minText) { readTextFile(compileTextPath, cb);
cb(minText);
});
} }
} }
); );
Expand All @@ -316,7 +324,7 @@ function compileLess(lessCss, lessPath, cb) {
var lessDir = path.dirname(lessPath), var lessDir = path.dirname(lessPath),
fileName = path.basename(lessPath), fileName = path.basename(lessPath),
options = { options = {
paths: [SCAN_ROOT_DIR, '.', lessDir], // Specify search paths for @import directives paths: ['.', lessDir], // Specify search paths for @import directives
filename: fileName filename: fileName
}; };


Expand All @@ -333,3 +341,24 @@ function minifyjs(js) {
var minJs = pro.gen_code(ast); var minJs = pro.gen_code(ast);
return minJs; return minJs;
} }

function removeCR(text) {
return text.replace(/\r/g, '');
}

function stripBOM(content) {
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
// because the buffer-to-string conversion in `fs.readFileSync()`
// translates it to FEFF, the UTF-16 BOM.
if (content.charCodeAt(0) === 0xFEFF) {
content = content.slice(1);
}
return content;
}

function readTextFile(filePath, cb) {
fs.readFile(filePath, 'utf-8', function(err, fileContents) {
if (err) throw err;
cb(stripBOM(fileContents));
});
}

0 comments on commit 8d00f38

Please sign in to comment.