Skip to content

Commit

Permalink
Fixed bugs reported in issues #12 and #13
Browse files Browse the repository at this point in the history
  • Loading branch information
call-a3 committed May 20, 2015
1 parent a93498c commit b3e8168
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ The second parameter to require must be an object and supports the following key
This allows options to be provided to [node-glob](https://www.npmjs.com/package/glob), which is used internally to find matching files.

### ext *[deprecated, optional, default:false]*
This option is replaced by `resolver: 'strip-ext'`, but remains supported until version 2.0.0
This option is replaced by `resolve: 'strip-ext'`, but remains supported until version 2.0.0.
**WARNING**: Backwards compatibility is not available in combination with the newer "resolve" option.

### glob *[deprecated]*
This option is replaced by `mode: 'expand'`, but remains supported until version 2.0.0

### hash *[deprecated]*
This option is replaced by `mode: 'hash'` and `resolver: ['path', 'strip-ext']`, but remains supported until version 2.0.0
This option is replaced by `mode: 'hash'` and `resolve: ['path', 'strip-ext']`, but remains supported until version 2.0.0


## Credits
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports = require('browserify-transform-tools').makeRequireTransform(
// get the second param to require as our config
config = args[1];

var skipExtCompat = typeof config.resolve !== 'undefined';

// backwards compatibility for glob and hash options, replaced by mode
if (config.glob) {
config.mode = "expand";
Expand All @@ -44,6 +46,7 @@ module.exports = require('browserify-transform-tools').makeRequireTransform(
}

// backwards compatibility for ext option
if (!skipExtCompat) {
if (typeof config.ext === 'undefined' || config.ext === false) {
if (config.resolve.indexOf('strip-ext') === -1) {
config.resolve.push('strip-ext');
Expand All @@ -56,6 +59,7 @@ module.exports = require('browserify-transform-tools').makeRequireTransform(
config.resolve.splice(sei, 1);
}
}
}

// if the config object doesn't match our specs, abort
if (typeof config.mode === 'undefined') {
Expand Down
5 changes: 3 additions & 2 deletions resolvers/path-reduce.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var path = require('path');

module.exports = function(base, files, config) {
var key, keys, common;
var key, keys, common, file;

if (Object.keys(files).length === 1) {
files[file] = path.basename(file, path.extname(file));
file = Object.keys(files)[0];
files[file] = path.basename(file);
return files;
}

Expand Down
15 changes: 7 additions & 8 deletions resolvers/strip-ext.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var path = require('path');

module.exports = function(base, files, config) {
var key, newKey;
// contains map of stripped keys
var filenames = Object.keys(files);
// contains map of stripped filenames
var conflicts = {};
for (var file in files) {
key = files[file];
if (files.hasOwnProperty(key)) {
newKey = key.substr(0, key.length - path.extname(key).length);
for (var i=0, l=filenames.length; i<l; i++) {
(function(file, key) {
var newKey = key.substr(0, key.length - path.extname(key).length);
// if already file with same stripping
if (newKey in conflicts) {
if (conflicts.hasOwnProperty(newKey)) {
// check if first conflict
if (conflicts[newKey] !== false) {
// revert previous file stripping
Expand All @@ -22,7 +21,7 @@ module.exports = function(base, files, config) {
// remember for possible later conflicts
conflicts[newKey] = [file, key];
}
}
})(filenames[i], files[filenames[i]]);
}
return files;
}
16 changes: 8 additions & 8 deletions spec/hash-1.2.0.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe('backwards compatibility', function() {

describe('hash:true', function() { // default ext:false

describe('default', function() {
describe('[default]', function() {
it('should equal {mode:"hash"}', function(done) {
compare('./dummies/module.js',
'require("./include/**/*", {hash: true});',
Expand All @@ -316,7 +316,7 @@ describe('backwards compatibility', function() {

});

describe('ext:false', function() {
describe(', ext:false', function() {

it('should equal {mode:"hash"}', function(done) {
compare('./dummies/module.js',
Expand All @@ -327,7 +327,7 @@ describe('backwards compatibility', function() {

});

describe('ext:true', function() {
describe(', ext:true', function() {

it('should equal {mode:"hash", resolve:"path-reduce"}', function(done) {
compare('./dummies/module.js',
Expand All @@ -342,7 +342,7 @@ describe('backwards compatibility', function() {

describe('hash:"path"', function() { // default ext:false

describe('default', function() {
describe('[default]', function() {
it('should equal {mode:"hash", resolve:["path", "strip-ext"]}', function(done) {
compare('./dummies/module.js',
'require("./include/**/*", {hash: "path"});',
Expand All @@ -352,7 +352,7 @@ describe('backwards compatibility', function() {

});

describe('ext:false', function() {
describe(', ext:false', function() {

it('should equal {mode:"hash", resolve:["path", "strip-ext"]}', function(done) {
compare('./dummies/module.js',
Expand All @@ -363,12 +363,12 @@ describe('backwards compatibility', function() {

});

describe('ext:true', function() {
describe(', ext:true', function() {

it('should equal {mode:"hash", resolve:"path", ext:true}', function(done) {
it('should equal {mode:"hash", resolve:"path"}', function(done) {
compare('./dummies/module.js',
'require("./include/**/*", {hash: "path", ext: true});',
'require("./include/**/*", {mode: "hash", resolve:"path", ext:true});',
'require("./include/**/*", {mode: "hash", resolve:"path"});',
done);
});

Expand Down
65 changes: 65 additions & 0 deletions spec/issues-1.2.1.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var util = require('./util');
var test = util.test;
var compare = util.compare;
var expect = util.expect;

describe('issues/12', function() {

it('should output a reduced path with extensions stripped when resolver: ["path-reduce", "strip-ext"] is used', function(done) {
test(
'./dummies/include/module.js',
'var deps = require("./*", {mode: "hash", resolve: [ "strip-ext", "path-reduce" ]});',
function(data) {
expect(data).to.equal("var deps = {'INCLUDED': require('./INCLUDED.js'),'INCLUDED2': require('./INCLUDED2.js')};");
}, done);
});

it('should output a reduced path with extensions stripped when resolver: ["strip-ext", "path-reduce"] is used', function(done) {
test(
'./dummies/include/module.js',
'var deps = require("./*", {mode: "hash", resolve: [ "path-reduce", "strip-ext" ]});',
function(data) {
expect(data).to.equal("var deps = {'INCLUDED': require('./INCLUDED.js'),'INCLUDED2': require('./INCLUDED2.js')};");
}, done);
});

it('should return the same whether path-reduce or strip-ext comes first', function(done) {
compare('./dummies/module.js',
'require("./include/**/*", {mode: "hash", resolve: [ "strip-ext", "path-reduce" ]});',
'require("./include/**/*", {mode: "hash", resolve: [ "path-reduce", "strip-ext" ]});',
done);
});

});

describe('issues/13', function() {
// // Input
// var templates = require('./templates/*.hbs', { mode: 'hash' });
//
// // Output
// var templates = {
// './templates/zoning': require('./templates/zoning.hbs')
// };
//
// // Output expected
// var templates = {
// 'zoning.hbs': require('./templates/zoning.hbs')
// };
it('should output a single file with relative directory without extension', function(done) {
test(
'./dummies/include/module.js',
'var deps = require("./nesting/*.js", {mode: "hash"});', //default resolve: ["path-reduce", "strip-ext"]
function(data) {
expect(data).to.equal("var deps = {'NESTED_INCLUDE': require('./nesting/NESTED_INCLUDE.js')};");
}, done);
});

it('should output a single file with relative directory with extension if only path-reduce is specified', function(done) {
test(
'./dummies/include/module.js',
'var deps = require("./nesting/*.js", {mode: "hash", resolve:["path-reduce"]});', //default resolve: ["path-reduce", "strip-ext"]
function(data) {
expect(data).to.equal("var deps = {'NESTED_INCLUDE.js': require('./nesting/NESTED_INCLUDE.js')};");
}, done);
});
});

0 comments on commit b3e8168

Please sign in to comment.