Permalink
Browse files

refining build process so AMD and browser/node.js modules can be built

  • Loading branch information...
1 parent d2e3a68 commit cb3cffca27edade59bf3618fed9c570d6a2304c3 @svnlto svnlto committed May 18, 2012
View
@@ -1,34 +1,102 @@
+/**
+ * Builds the distribution files from /src
+ *
+ * Includes code from Miller Medeiros, http://blog.millermedeiros.com/node-js-as-a-build-script/
+ */
-var parser = require( 'uglify-js' ).parser,
- uglify = require( 'uglify-js' ).uglify,
- fs = require( 'fs' ),
- src = '../src/backbone-eventbroker.js',
- built = '../backbone-eventbroker-min.js';
-
-var _build = function()
-{
- console.log( 'Loading ' + src );
-
- fs.readFile( src, 'utf8', function ( error, data ) {
- if ( error ) {
- return console.log( 'Error' + error );
- }
- _write( _minify( data ) );
- });
-};
+// DEPENDENCIES
+var fs = require('fs'),
+ util = require('util'),
+ _ = require('underscore'),
+ uglifyJS = require('uglify-js');
-var _minify = function( source ) {
- var ast = uglify.ast_squeeze( uglify.ast_mangle( parser.parse( source ) ) );
- return uglify.gen_code( ast );
-};
+// CONFIG
+var outputDir = __dirname + '/../dist',
+ srcDir = __dirname + '/../src',
+ templateDir = __dirname + '/templates',
+ eol = '\n',
+ fileEncoding = 'utf-8',
+ packageJSON = JSON.parse(fs.readFileSync(__dirname + '/../package.json', fileEncoding));
+
+// SETTINGS
+// Use mustache template tags
+_.templateSettings.interpolate = /\{\{(.+?)\}\}/g;
+
+
+// TASKS
+/**
+ * Build the output by concatenating files and optionally wrapping them in a template
+ *
+ * @param {String[]} fileList File paths, relative to current directory
+ * @param {String} outputPath Relative path to output file
+ * @param {Object} [options]
+ *
+ * @param {String} [options.template] Template for adding headers/footers etc. Content will inserted in place of a {{body}} tag
+ * @param {Object} [options.data] Date to populate template tags (in {{tag}} format)
+ */
+function build(fileList, outputPath, options) {
+ var out = fileList.map(function(filePath) {
+ filePath = filePath;
+
+ return fs.readFileSync(filePath, fileEncoding);
+ });
+
+ var content = out.join(eol);
+
+ if (options && options.template) {
+ var data = _.extend(options.data, {
+ body: content
+ });
+
+ var templateString = fs.readFileSync(options.template, fileEncoding);
+
+ content = _.template(templateString, data);
+ }
+
+ fs.writeFileSync(outputPath, content);
-var _write = function( min )
-{
- fs.writeFile( built, min, function( error ) {
- if ( error ) {
- return console.log( 'Error' + error );
- }
- console.log( 'Minified ' + built + '\nDone' );
- });
+ console.log('READY: ' + outputPath);
+}
+
+/**
+ * Minify files using UglifyJS
+ * @param {String} srcPath Relative path to source file
+ * @param {outputPath} outputPath Relative path to output file
+ */
+function uglify(srcPath, outputPath) {
+ var parse = uglifyJS.parser.parse,
+ uglify = uglifyJS.uglify;
+
+ var output = parse(fs.readFileSync(srcPath, fileEncoding));
+
+ output = uglify.ast_mangle(output);
+ output = uglify.ast_squeeze(output);
+
+ fs.writeFileSync(outputPath, uglify.gen_code(output), fileEncoding);
+
+ console.log('READY: ' + outputPath);
+}
+
+// RUN
+var fileList = [
+ srcDir + '/backbone-eventbroker.js'
+];
+
+var templateData = {
+ version: packageJSON.version
};
-_build();
+
+//Main file
+build(fileList, outputDir + '/backbone-eventbroker.js', {
+ template: templateDir + '/backbone-eventbroker.js',
+ data: templateData
+});
+uglify(outputDir + '/backbone-eventbroker.js', outputDir + '/backbone-eventbroker.min.js');
+
+
+//File for AMD (requireJS)
+build(fileList, outputDir + '/backbone-eventbroker.amd.js', {
+ template: templateDir + '/backbone-eventbroker.amd.js',
+ data: templateData
+});
+uglify(outputDir + '/backbone-eventbroker.amd.js', outputDir + '/backbone-eventbroker.amd.min.js');
@@ -0,0 +1,28 @@
+/**
+ * Backbone Eventbroker v{{version}}
+ *
+ * NOTE:
+ * This version is for use with RequireJS
+ * If using regular <script> tags to include your files, use backbone-memory.min.js
+ *
+ * Copyright (c) 2012 Eric Feminella, Sven Lito
+ *
+ * License and more information at:
+ * http://code.ericfeminella.com/license/LICENSE.txt
+ */
+define([
+ 'jquery',
+ 'backbone',
+ 'underscore'
+],
+
+function($, Backbone, _) {
+
+ "use strict";
+
+ {{body}}
+
+ //Exports
+ return Backbone.EventBroker;
+
+});
@@ -0,0 +1,30 @@
+/**
+ * Backbone Eventbroker v{{version}}
+ *
+ * Copyright (c) 2012 Eric Feminella, Sven Lito
+ *
+ * License and more information at:
+ * http://code.ericfeminella.com/license/LICENSE.txt
+ */
+;(function($, _, Backbone) {
+
+ "use strict";
+
+ {{body}}
+
+ //EXPORTS
+ //AMD (RequireJS) - For exporting as a module when Backbone and jQuery are on the page
+ //If using RequireJS to load Backbone, Underscore and jQuery, use the AMD-specific file
+ if (typeof define === 'function' && define.amd) {
+ return define(function() {
+ return Backbone.EventBroker;
+ });
+ }
+
+ //CommonJS (NodeJS)
+ if (typeof module === 'object' && typeof module.exports === 'object') {
+ module.exports = Backbone.EventBroker;
+ return;
+ }
+
+}(jQuery, _, Backbone));
Oops, something went wrong. Retry.

0 comments on commit cb3cffc

Please sign in to comment.