Permalink
Browse files

updated bbb to have build tasks embedded

  • Loading branch information...
1 parent d42bda9 commit 801eba9de30525755adeabf50061b7113253b752 @tbranyen tbranyen committed Mar 31, 2012
Showing with 470 additions and 21 deletions.
  1. +1 −0 .gitignore
  2. +1 −19 bin/bbb
  3. +8 −2 package.json
  4. +34 −0 tasks/clean.js
  5. +51 −0 tasks/handlebars.js
  6. +94 −0 tasks/jst.js
  7. +49 −0 tasks/less.js
  8. +29 −0 tasks/mincss.js
  9. +145 −0 tasks/requirejs.js
  10. +58 −0 tasks/templatize.js
View
@@ -0,0 +1 @@
+node_modules
View
20 bin/bbb
@@ -1,21 +1,3 @@
#!/usr/bin/env node
-var grunt = require("grunt");
-var path = require("path");
-var fs = require("fs");
-// Shorten the path name and resolve function
-var buildTasks = "/../node_modules/build-tasks/";
-var resolve = path.resolve;
-
-// Auto-load tasks
-var taskList = fs.readdirSync(__dirname + buildTasks).filter(function(task) {
- return fs.statSync(__dirname + buildTasks + task).isDirectory();
-}).map(function(task) {
- return resolve(__dirname + buildTasks + task);
-}).concat([resolve(__dirname + buildTasks), resolve(__dirname + "/../tasks")]);
-
-// Initialize the CLI
-grunt.npmTasks("bbb").cli({
- // Load the BBB tasks
- tasks: taskList
-}, function() {});
+require("grunt").npmTasks("bbb").cli();
View
@@ -1,7 +1,7 @@
{
"name": "bbb",
"description": "Backbone Boilerplate",
- "version": "0.0.4",
+ "version": "0.0.5",
"homepage": "https://github.com/tbranyen/bbb",
"author": {
"name": "Tim Branyen (@tbranyen)",
@@ -17,7 +17,13 @@
},
"dependencies": {
"grunt": "0.3.6",
- "build-tasks": "0.1.1"
+
+ "rimraf": "1.0.9",
+ "underscore": "1.2.4",
+ "less": "1.2.1",
+ "clean-css": "0.3.2",
+ "requirejs": "1.0.7",
+ "express": "2.5.4"
},
"devDependencies": {
"grunt": "~0.3.0"
View
@@ -0,0 +1,34 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: Clean
+ * Description: Remove the contents of a given folder
+ * Dependencies: rimraf@1.0.9
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var rimraf = require("rimraf");
+ var log = grunt.log;
+ var file = grunt.file;
+
+ grunt.registerMultiTask("clean",
+ "Deletes out all contents in a directory", function() {
+
+ var files = file.expand(this.data);
+
+ files.forEach(function(file) {
+ grunt.helper("clean", file);
+ });
+
+ return grunt.errors ? false : true;
+ });
+
+ grunt.registerHelper("clean", function(path) {
+ log.writeln('Removing: ' + path);
+ rimraf.sync(path);
+ });
+
+};
View
@@ -0,0 +1,51 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: Handlebars
+ * Description: Compile handlebars templates to JST file
+ * Dependencies: None
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var config = grunt.config;
+ var file = grunt.file;
+ var log = grunt.log;
+
+ grunt.registerMultiTask("handlebars",
+ "Compile underscore templates to JST file", function() {
+
+ // If namespace is specified use that, otherwise fallback
+ var namespace = config("meta.handlebars.namespace") || "JST";
+
+ // Create JST file.
+ var files = file.expand(this.data);
+ file.write(this.target, grunt.helper("handlebars", files, namespace));
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("File \"" + this.target + "\" created.");
+ });
+
+ grunt.registerHelper("handlebars", function(files, namespace) {
+ namespace = "this['" + namespace + "']";
+
+ // Comes out looking like this["JST"] = this["JST"] || {};
+ var contents = namespace + " = " + namespace + " || {};\n\n";
+
+ // Compile the template and get the function source
+ contents += files ? files.map(function(filepath) {
+ var templateFunction =
+ require("handlebars").precompile(file.read(filepath));
+
+ return namespace + "['" + filepath + "'] = " + templateFunction;
+ }).join("\n\n") : "";
+
+ return contents;
+ });
+
+};
View
@@ -0,0 +1,94 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: JST
+ * Description: Compile underscore templates to JST file.
+ * Dependencies: underscore@1.2.4
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var file = grunt.file;
+ var log = grunt.log;
+ var config = grunt.config;
+
+ grunt.registerMultiTask("jst",
+ "Compile underscore templates to JST file", function() {
+
+ // If namespace is specified use that, otherwise fallback
+ var namespace = config("options.jst.namespace") || "JST";
+ // If template settings are available use those
+ var templateSettings = config("options.jst.templateSettings") || null;
+ // Expand files to full paths
+ var files = file.expand(this.data);
+
+ // Create JST file.
+ file.write(this.target, grunt.helper("jst", files, namespace,
+ templateSettings));
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("File \"" + this.target + "\" created.");
+ });
+
+ grunt.registerHelper("jst", function(files, namespace, templateSettings) {
+ // Ensure we get the underscore from the node_modules folder
+ var _ = require("underscore");
+
+ // Pulled from underscore 1.2.4
+ function underscoreTemplating(str) {
+ // Merge in the templateSettings that may be passed
+ var c = _.extend({}, _.templateSettings, templateSettings) ||
+ _.templateSettings;
+
+ var tmpl = '' +
+ 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
+ 'with(obj||{}){__p.push(\'' +
+ str.replace(/\\/g, '\\\\')
+ .replace(/'/g, "\\'")
+ .replace(c.escape || noMatch, function(match, code) {
+ return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
+ })
+ .replace(c.interpolate || noMatch, function(match, code) {
+ return "'," + code.replace(/\\'/g, "'") + ",'";
+ })
+ .replace(c.evaluate || noMatch, function(match, code) {
+ return "');" + code.replace(/\\'/g, "'")
+ .replace(/[\r\n\t]/g, ' ')
+ .replace(/\\\\/g, '\\') + ";__p.push('";
+ })
+ .replace(/\r/g, '\\r')
+ .replace(/\n/g, '\\n')
+ .replace(/\t/g, '\\t')
+ + "');}return __p.join('');";
+
+ return new Function('obj', '_', tmpl).toString();
+ };
+
+ namespace = "this['" + namespace + "']";
+
+ // Comes out looking like this["JST"] = this["JST"] || {};
+ var contents = namespace + " = " + namespace + " || {};\n\n";
+
+ // Compile the template and get the function source
+ contents += files ? files.map(function(filepath) {
+ var templateFunction = [
+ "function(data) { ",
+
+ "return ",
+ underscoreTemplating(file.read(filepath)).replace("anonymous", ""),
+ "(data, _)",
+
+ "};"].join("");
+
+ return namespace + "['" + filepath + "'] = " + templateFunction;
+ }).join("\n\n") : "";
+
+ return contents;
+ });
+
+};
View
@@ -0,0 +1,49 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: less
+ * Description: Compile LESS files to CSS and minify.
+ * Dependencies: less@1.2.1 / clean-css@0.3.2
+ *
+ */
+
+module.exports = function(grunt) {
+
+ grunt.registerMultiTask("less",
+ "Compile LESS files to CSS and minify.", function() {
+
+ // load libraries
+ var less = require("less");
+ var cleanCSS = require("clean-css");
+ var data = this.data;
+
+ // initialize LESS parser
+ var parser = new(less.Parser)(data.options);
+
+ // iterate over files to compile/compress
+ Object.keys(data.files).forEach(function(dest) {
+ // grab src file to compile dest to
+ var src = data.files[dest];
+
+ // run less compiler
+ parser.parse(file.read(src), function (e, tree) {
+ var css = tree.toCSS();
+
+ // if config specified minify, do so with clean-css
+ if(data.options.compress) {
+ css = cleanCSS.process(css);
+ }
+
+ file.write(dest,css);
+ });
+ });
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("LESS compiling / minification complete.");
+ });
+
+};
View
@@ -0,0 +1,29 @@
+module.exports = function(grunt) {
+
+ var log = grunt.log;
+ var file = grunt.file;
+
+ grunt.registerMultiTask("mincss",
+ "Compress down CSS files cleanly.", function() {
+
+ // Minify CSS.
+ var files = file.expand(this.data);
+ file.write(this.target, grunt.helper('mincss', files));
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("File \"" + this.target + "\" created.");
+ });
+
+ grunt.registerHelper("mincss", function(files) {
+ var cleanCSS = require("clean-css");
+
+ // Minify and combine all CSS
+ return files ? files.map(function(filepath) {
+ return cleanCSS.process(file.read(filepath));
+ }).join("") : "";
+ });
+
+};
Oops, something went wrong.

0 comments on commit 801eba9

Please sign in to comment.