Skip to content

Commit

Permalink
browser compile
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalparadox committed Apr 24, 2012
1 parent 1f86a51 commit a2a76c5
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -14,7 +14,8 @@
"dependencies": {},
"devDependencies": {
"mocha": "*",
"chai": "*"
"chai": "*",
"folio": "0.2.x"
},
"optionalDependencies": {},
"engines": {
Expand Down
48 changes: 48 additions & 0 deletions support/browser/prefix.js
@@ -0,0 +1,48 @@
!function (name, definition) {
if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
else this[name] = definition();
}('chai_timers', function () {
// CommonJS require()
function require(p){
var path = require.resolve(p)
, mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
}

require.modules = {};

require.resolve = function (path){
var orig = path
, reg = path + '.js'
, index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
};

require.register = function (path, fn){
require.modules[path] = fn;
};

require.relative = function (parent) {
return function(p){
if ('.' != p[0]) return require(p);

var path = parent.split('/')
, segs = p.split('/');
path.pop();

for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}

return require(path.join('/'));
};
};
4 changes: 4 additions & 0 deletions support/browser/suffix.js
@@ -0,0 +1,4 @@
return require('timers');
});

chai.use(chai_timers);
86 changes: 86 additions & 0 deletions support/compile.js
@@ -0,0 +1,86 @@
/*!
* chai-spies :: browser build script
* Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/

/*!
* Script dependancies
*/

var fs = require('fs')
, path = require('path')
, join = path.join
, folio = require('folio');

/*!
* Script variables
*/

var appfiles = []
, basepath = join(__dirname, '..', 'lib');

/**
* Recursively iterate through a given path
* and add all `.js` files to an array.
*
* @param {String} absolute path
*/

function iteratePath (p) {
var self = this
, files = fs.readdirSync(p);
files.forEach(function (filename) {
var file = path.join(p, filename)
, stat = fs.statSync(file);
if (stat.isDirectory()) {
iteratePath(file);
} else if (stat.isFile()) {
if (path.extname(file) == '.js')
appfiles.push(file);
}
});
};

// Go!
iteratePath(basepath);

/**
* Package together all found files into a
* folio.Glossary, defining a custom "compiler"
* that will wrap our script with the needed commonjs
*
* @param {Array} files
* @param {Object} folio glossary configuration
*/

var applicationJs = new folio.Glossary(appfiles, {
compilers: {
js: function (name, source, filename) {
var title = filename.replace(basepath + '/', '').replace('.js', '')
, buf = '\nrequire.register("' + title + '", function (module, exports, require) {\n';
buf += source;
buf += '\n}); // module ' + name;
return buf;
}
}
});

/*!
* Load up our prefix/suffix
*/

var prefix = fs.readFileSync(join(__dirname, 'browser', 'prefix.js'), 'utf8')
, suffix = fs.readFileSync(join(__dirname, 'browser', 'suffix.js'), 'utf8')

/**
* Compile the folio.Glossary, applying our wrapper,
* and output the src. Wrap with prefix/suffix and
* write to file.
*/

applicationJs.compile(function (err, src) {
var content = prefix + src + suffix;
fs.writeFileSync(join(__dirname, '..', 'chai-timers.js'), content, 'utf8');
console.log('completed: chai-timers.js');
});

0 comments on commit a2a76c5

Please sign in to comment.