Skip to content

Commit

Permalink
test(module): nested require resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
thepian committed Apr 3, 2015
1 parent 5d23201 commit 845685f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 37 deletions.
4 changes: 1 addition & 3 deletions .javascript.json
Expand Up @@ -5,12 +5,11 @@
"browser": true,
"node": true,
"camelcase": true,
"curly": true,
"curly": false,
"forin": true,
"immed": true,
"latedef": "nofunc",
"maxlen": 150,
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
Expand All @@ -30,7 +29,6 @@
"module",
"exports"
],
"quotmark": true,
"trailing": true,
"undef": true,
"unused": true
Expand Down
3 changes: 1 addition & 2 deletions lib/client/bundler/index.js
Expand Up @@ -137,8 +137,7 @@ module.exports = function(ss,options) {
var firstIndex, // if no entry point, use first index found
firstFile; // if no entry and index, use first file found

/*jshint -W084 */
for(var i = 0,rel; rel = client.paths.code[i]; ++i) {
for(var i = 0,rel; (rel = client.paths.code[i]); ++i) {
var p = path.join(ss.root, options.dirs.client, rel);
if (fs.statSync(p).isDirectory()) {

Expand Down
3 changes: 1 addition & 2 deletions lib/client/bundler/proto.js
Expand Up @@ -154,8 +154,7 @@ module.exports = function(ss, bundlers, bundlerById, options) {
}

if (pathAry.indexOf('entry.js') === -1 && options && options.browserifyExcludePaths) {
/*jshint -W084 */
for(var i, p; p = options.browserifyExcludePaths[i]; ++i) {
for(var i, p; (p = options.browserifyExcludePaths[i]); ++i) {
if ( entry.file.split( p )[0] === '' ) {
return code;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/client/system/index.js
Expand Up @@ -48,9 +48,9 @@ var send = exports.send = function (type, name, content, options) {
switch (type) {
case 'const':
case 'constant':
return assets.constants[name] = {value:content,name:name,type:'const',options:options};
return (assets.constants[name] = {value:content,name:name,type:'const',options:options});
case 'local':
return assets.locals[name] = { value:content,name:name,type:type,options:options};
return (assets.locals[name] = { value:content,name:name,type:type,options:options});
case 'start':
case 'code':
return assets.startCode.push({content:content,options:options, type:'start'});
Expand Down
11 changes: 6 additions & 5 deletions lib/client/system/libs/browserify.js
Expand Up @@ -19,25 +19,26 @@ require._core = {
};

require.resolve = function (x, cwd) {
if (!cwd) cwd = '/';
if (!cwd) { cwd = '/'; }

if (require._core[x]) return x;
if (require._core[x]) {return x;}
var path = require.modules.path();
//cwd = path.resolve('/', cwd); // ruins require of builtin/sub-module

// paths are ../ or ./ or /
x = x.match(/^(?:\.\.?\/|\/)/) ? path.resolve(cwd,x) : x;
x = x.charAt(0) === '/' ? path.resolve(cwd, x.substring(1)) : x;
x = x.match(/^(?:\.\.?\/)/) ? path.resolve(cwd,x) : x;

var resolved = firstMatch(
x, // as a file
path.join(x,'index') // as a directory
);
if (resolved) return resolved;
if (resolved) {return resolved;}

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

function firstMatch() {
for(var i= 0,a; a = arguments[i]; ++i) {
for(var i= 0,a; (a = arguments[i]); ++i) {
if (require.modules[a]) { return a; }
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/client/system/modules/socketstream.js
Expand Up @@ -51,7 +51,7 @@ async = {
exports.load = {

// Enables asynchronous loading of additional client-side modules
// Pass the name of a module file (complete with file extension), or name of a directory in /client/code
// Pass the name of a module file (complete with file extension), or name of a directory in /client/code
code: function(nameOrDir, cb) {
var errorPrefix, onError, onSuccess;

Expand All @@ -60,19 +60,20 @@ exports.load = {
nameOrDir = nameOrDir.substr(1);
}

// Check for errors. Note this feature requires jQuery at the moment
// Check for errors. Note this feature requires jQuery at the moment
errorPrefix = 'SocketStream Error: The ss.load.code() command ';
if (!jQuery) {
return console.error(errorPrefix + 'requires jQuery to be present');
}
if (!nameOrDir) {
return console.error(errorPrefix + 'requires a directory to load. Specify it as the first argument. E.g. The ss.load.code(\'/mail\',cb) will load code in /client/code/mail');
return console.error(errorPrefix + 'requires a directory to load. Specify it as the first argument. '+
'E.g. The ss.load.code(\'/mail\',cb) will load code in /client/code/mail');
}
if (!cb) {
return console.error(errorPrefix + 'requires a callback. Specify it as the last argument');
}

// If we've loaded this module or package before, callback right away
// If we've loaded this module or package before, callback right away
if (async.loaded[nameOrDir]) {
return cb();
}
Expand All @@ -91,7 +92,7 @@ exports.load = {
return async.loading.emit(nameOrDir);
};

// Send request to server
// Send request to server
return $.ajax({
url: '/_serve/code?' + nameOrDir,
type: 'GET',
Expand All @@ -103,7 +104,7 @@ exports.load = {
}
},

// Load Web Workers from /client/workers
// Load Web Workers from /client/workers
worker: function(name) {
return new Worker('/_serve/worker?' + name);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/client/template_engine.js
Expand Up @@ -10,9 +10,7 @@
require('colors');

var path = require('path'),
formatters = require('./formatters'),
client = require('./system'),
log = require('../utils/log');
client = require('./system');

// Allow Template Engine to be configured
module.exports = function(ss,options) {
Expand Down Expand Up @@ -85,7 +83,8 @@ module.exports = function(ss,options) {
if (require.resolve(modPath)) {
return require(modPath);
} else {
throw new Error('The ' + nameOrModule + ' template engine is not supported by SocketStream internally. Please pass a compatible module instead');
throw new Error('The ' + nameOrModule + ' template engine is not supported by SocketStream internally. '+
'Please pass a compatible module instead');
}
}
})();
Expand All @@ -105,7 +104,8 @@ module.exports = function(ss,options) {
mods.forEach(function(mod) {
return mod.dirs.forEach(function(dir) {
if (dir.substring(0, 1) !== '/' && dir.substring(0,2) !== './' && dir !== '.') {
throw new Error('Directory name \'' + dir + '\' passed to second argument of ss.client.templateEngine.use() command must start with / or ./');
throw new Error('Directory name \'' + dir + '\' passed to second argument of ss.client.templateEngine.use() '+
'command must start with / or ./');
}
templateEngines[dir] = mod.engine;
return templateEngines[dir];
Expand Down Expand Up @@ -170,7 +170,7 @@ module.exports = function(ss,options) {
return cb(output);
}
}, function(err) {
ss.log.clientIssue(client,options,err,entry);
ss.log.clientIssue(client,options,err,desc);
return cb('Couldn\'t format ' + desc.file + err.userInfoHTML);
});
});
Expand Down
3 changes: 0 additions & 3 deletions lib/client/view.js
Expand Up @@ -3,9 +3,6 @@
// Generates HTML output for each single-page view
'use strict';

var path = require('path'),
magicPath = require('./magic_path');

module.exports = function(ss, client, options, cb) {
var templateEngine = require('./template_engine')(ss),
bundler = ss.bundler.get(client);
Expand Down
2 changes: 2 additions & 0 deletions test/unit/client/abcClient.js
@@ -1,3 +1,5 @@
'use strict';

var ss = require( '../../../lib/socketstream');

module.exports = defineAbcClientAndLoad;
Expand Down
17 changes: 9 additions & 8 deletions test/unit/client/system/browserify.test.js
Expand Up @@ -65,6 +65,7 @@ describe('browserify', function() {
bPath.resolve('/').should.be.equal('/');
bPath.resolve('abc/def','./ghi').should.be.equal('abc/def/ghi');
bPath.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile').should.be.equal('/tmp/subfile');
bPath.resolve('/a/b','c/index').should.be.equal('/a/b/c/index');
});

it('should have functioning require.modules.path.dirname',function() {
Expand All @@ -76,22 +77,22 @@ describe('browserify', function() {

it('should resolve /index directly or indirectly', function() {
browser.require.define('/a/b/c/d',
function(require, module, exports, __dirname, __filename) {
function(/*require, module, exports, __dirname, __filename*/) {
}
);

browser.require.define('/a/b/c/index',
function(require, module, exports, __dirname, __filename) {
function(/*require, module, exports, __dirname, __filename*/) {
}
);

browser.require.define('builtin/e',
function(require, module, exports, __dirname, __filename) {
function(/*require, module, exports, __dirname, __filename*/) {
}
);

browser.require.define('builtin/index',
function(require, module, exports, __dirname, __filename) {
function(/*require, module, exports, __dirname, __filename*/) {
}
);

Expand All @@ -101,13 +102,13 @@ describe('browserify', function() {
browser.require.resolve('/a/b/c').should.equal('/a/b/c/index');
browser.require.resolve('/a/b/c/').should.equal('/a/b/c/index');

//browser.require.resolve('/c/index','/a/b').should.equal('/a/b/c/index');
//browser.require.resolve('/c','/a/b').should.equal('/a/b/c/index');
//browser.require.resolve('/c/','/a/b').should.equal('/a/b/c/index');
browser.require.resolve('/c/index','/a/b').should.equal('/a/b/c/index');
browser.require.resolve('/c','/a/b').should.equal('/a/b/c/index');
browser.require.resolve('/c/','/a/b').should.equal('/a/b/c/index');

browser.require.resolve('builtin/index').should.equal('builtin/index');

//browser.require.resolve('builtin/index','/a/b').should.equal('builtin/index');
browser.require.resolve('builtin/index','/a/b').should.equal('builtin/index');
});

it('should resolve require with absolute path', function() {
Expand Down

0 comments on commit 845685f

Please sign in to comment.