Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Jolley committed May 2, 2012
1 parent 2d1ed27 commit cea59b9
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/asset_copier.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function _makeWatcher(root, callback) {
}

function setup(next) {
watcher = FS.watch(root, fileChanged);
watcher = FS.watch(root, { persistent: true }, fileChanged);
FS.stat(root, function(err, stats) {
if (err) return next(err);
if (stats.isDirectory()) {
Expand Down
8 changes: 5 additions & 3 deletions lib/asset_packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var LoggedEventEmitter = require('./logged_events').LoggedEventEmitter;
var PATH = require('path');
var FS = require('fs');
var ASYNC = require('async');
var RESOLVE = require('resolve');
var RESOLVE = require('./resolver');
var UTILS = require('./utils');

var _extend = UTILS.extend;
Expand Down Expand Up @@ -95,6 +95,7 @@ _extend(AssetPackager.prototype, {
* @return {[type]} [description]
*/
AssetPackager.prototype.getSourceAsset = function(assetPath, done) {
if (!done) done = function() {};
assetPath = PATH.resolve(this.basedir, assetPath);
if (!this._sourceAssets[assetPath]) {
var self = this;
Expand Down Expand Up @@ -141,7 +142,7 @@ AssetPackager.prototype.getSourceAsset = function(assetPath, done) {
if (!err && self.watch && !self._watching[assetPath]) {
//console.log('watch', assetPath);
self._watching[assetPath] =
FS.watch(assetPath, function() {
FS.watch(assetPath, { persistent: true }, function() {
self.info('changed', assetPath);
self.invalidate();
});
Expand Down Expand Up @@ -213,7 +214,8 @@ AssetPackager.prototype.resolve = function(moduleId, basedir) {
return RESOLVE.sync(moduleId, {
extensions: Object.keys(this.compilers),
basedir: basedir || this.basedir,
isFile: _isFile
isFile: _isFile,
mainKey: this.mainKey
});
};

Expand Down
23 changes: 16 additions & 7 deletions lib/packagers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ var AssetCopier = require('./asset_copier').AssetCopier;
var UTILS = require('./utils');
var plugins = require('../pipeline_plugins');

// generates a new packager subclass with default config
function packager() {
// generates a new packager subclass with default config. Pass an optional
// BaseClass to further extend a packager.
function packager(BaseClass) {
var args = Array.prototype.slice.call(arguments);
if ('function' === typeof BaseClass) {
args.unshift();
} else {
BaseClass = AssetPackager;
}

function Packager() {
AssetPackager.apply(this, arguments);
BaseClass.apply(this, arguments);
}

Packager.prototype = UTILS.merge(Object.create(AssetPackager.prototype),
Array.prototype.slice.call(arguments));
Packager.prototype = UTILS.merge(Object.create(BaseClass.prototype), args);

return Packager;
}
Expand All @@ -29,7 +36,8 @@ exports.javascript = packager({

analyzer: plugins.CommonJSAnalyzer,
linker: plugins.CommonJSLinker,
minifier: plugins.UglifyMinifier
minifier: plugins.UglifyMinifier,
mainKey: 'main'
});


Expand All @@ -42,7 +50,8 @@ exports.legacy_javascript = packager({

analyzer: plugins.GenericAnalyzer,
linker: plugins.SimpleMergeLinker,
minifier: plugins.UglifyMinifier
minifier: plugins.UglifyMinifier,
mainkey: 'main'
});

exports.css = packager({
Expand Down
109 changes: 109 additions & 0 deletions lib/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Borrowed from resolve package
// modified to have a more refined use of 'main'

var fs = require('fs');
var path = require('path');

// taken from `ls -1 lib` in node 0.6.11
var core = exports.core = [
'assert', 'buffer_ieee754', 'buffer', 'child_process', 'cluster', 'console',
'constants', 'crypto', '_debugger', 'dgram', 'dns', 'events', 'freelist',
'fs', 'http', 'https', '_linklist', 'module', 'net', 'os', 'path',
'punycode', 'querystring', 'readline', 'repl', 'stream', 'string_decoder',
'sys', 'timers', 'tls', 'tty', 'url', 'util', 'vm', 'zlib'
].reduce(function (acc, x) { acc[x] = true; return acc }, {});

exports.isCore = function (x) { return core[x] };

exports.sync = function (x, opts) {
if (core[x]) return x;

if (!opts) opts = {};
var isFile = opts.isFile || function (file) {
return path.existsSync(file) && fs.statSync(file).isFile()
};
var readFileSync = opts.readFileSync || fs.readFileSync;

var mainKey = opts.mainKey;
var extensions = opts.extensions || [ '.js' ];
var y = opts.basedir
|| path.dirname(require.cache[__filename].parent.filename)
;

opts.paths = opts.paths || [];

if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
var m = loadAsFileSync(path.resolve(y, x))
|| loadAsDirectorySync(path.resolve(y, x));
if (m) return m;
}

var n = loadNodeModulesSync(x, y);
if (n) return n;

throw new Error("Cannot find module '" + x + "'");

function loadAsFileSync (x) {
if (isFile(x)) {
return x;
}

for (var i = 0; i < extensions.length; i++) {
var file = x + extensions[i];
if (isFile(file)) {
return file;
}
}
}

function loadAsDirectorySync (x) {
var pkgfile = path.join(x, '/package.json');
if (isFile(pkgfile)) {
var body = readFileSync(pkgfile, 'utf8');
try {
var pkg = JSON.parse(body);
if (opts.packageFilter) {
pkg = opts.packageFilter(pkg);
}

if (pkg[mainKey]) {
var m = loadAsFileSync(path.resolve(x, pkg[mainKey]));
if (m) return m;
}
}
catch (err) {}
}

return loadAsFileSync(path.join( x, '/index'));
}

function loadNodeModulesSync (x, start) {
var dirs = nodeModulesPathsSync(start);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
var m = loadAsFileSync(path.join( dir, '/', x));
if (m) return m;
var n = loadAsDirectorySync(path.join( dir, '/', x ));
if (n) return n;
}
}

function nodeModulesPathsSync (start) {
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
var parts = start.split(splitRe);

var dirs = [];
for (var i = parts.length - 1; i >= 0; i--) {
if (parts[i] === 'node_modules') continue;
var dir = path.join(
path.join.apply(path, parts.slice(0, i + 1)),
'node_modules'
);
if (!parts[0].match(/([A-Za-z]:)/)) {
dir = '/' + dir;
}
dirs.push(dir);
}
return opts.paths.concat(dirs);
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

"dependencies": {
"async": "0.1.x",
"resolve": "0.2.x",
"uglify-js": "~1.2",
"glob": "~3.1",
"mime": "~1.2"
"mime": "~1.2",
"coffee-script": "~1.3"
},

"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion pipeline_plugins/coffeescript_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
* @copyright 2012 Charles Jolley
*/

var CoffeeScript = require('coffee-script');
var FS = require('fs');

function CoffeeScriptCompiler(asset, context, done) {
done(new Error('CoffeeScriptCompiler not yet implemented.'));
FS.readFile(asset.path, 'utf8', function(err, data) {
if (err) return done(err);
asset.body = CoffeeScript.compile(data);
done();
});
}

module.exports = CoffeeScriptCompiler;
1 change: 0 additions & 1 deletion pipeline_plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
e.JavaScriptCompiler = require('./javascript_compiler');
e.CoffeeScriptCompiler = require('./coffeescript_compiler');
e.CSSCompiler = require('./css_compiler');
e.LESSCompiler = require('./less_compiler');

e.GenericAnalyzer = require('./generic_analyzer');
e.CommonJSAnalyzer = require('./commonjs_analyzer');
Expand Down
10 changes: 0 additions & 10 deletions pipeline_plugins/less_compiler.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/asset_copier_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @module asset_copier
*/

var should = require('should');
var FS = require('fs');
var UTIL = require('util');
var PATH = require('path');
var ASYNC = require('async');

var should = require('should');
var h = require('./support/helpers');
var lib = h.lib;
var utils = require('../lib/utils');
Expand Down
5 changes: 3 additions & 2 deletions test/asset_packager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module asset_packager
*/

var should = require('should');
var should = require('should');
var FS = require('fs');
var h = require('./support/helpers');
var lib = h.lib;
Expand All @@ -19,7 +19,8 @@ function packager(config) {
},
analyzer: lib.plugins.GenericAnalyzer,
linker: lib.plugins.SimpleMergeLinker,
minifier: lib.plugins.UglifyMinifier
minifier: lib.plugins.UglifyMinifier,
mainKey: 'main'
}, config);
}

Expand Down
5 changes: 3 additions & 2 deletions test/commonjs_packager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module module_packager test
*/

var should = require('should');
var should = require('should');
var h = require('./support/helpers');
var lib = h.lib;
var UGLIFY = require('uglify-js');
Expand All @@ -16,7 +16,8 @@ function packager(config) {

analyzer: lib.plugins.CommonJSAnalyzer,
linker: lib.plugins.CommonJSLinker,
minifier: lib.plugins.UglifyMinifier
minifier: lib.plugins.UglifyMinifier,
mainKey: 'main'
}, config);
}

Expand Down
1 change: 1 addition & 0 deletions test/middleware_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// most of these tests are borrowed from the connect.static middleware tests

var should = require('should');
require('./support/http');

var connect = require('connect');
Expand Down
1 change: 0 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--require should
--growl
2 changes: 1 addition & 1 deletion test/pipeline_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @copyright 2012 Charles Jolley
*/

var should = require('should');
var should = require('should');
var h = require('./support/helpers');
var lib = h.lib;
var FS = require('fs');
Expand Down

0 comments on commit cea59b9

Please sign in to comment.