Skip to content

Commit

Permalink
updated prefix functionality to allow something similar to browserifi…
Browse files Browse the repository at this point in the history
…es old aliasing functionality, updated test cases, added minimatch dependency
  • Loading branch information
spencer-leopold committed Nov 11, 2015
1 parent 1a38af9 commit e244c45
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
69 changes: 65 additions & 4 deletions lib/mundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var browserify = require('browserify');
var watchify = require('watchify');
var Promise = require('bluebird');
var glob = require('glob');
var minimatch = require('minimatch');
var chalk = require('chalk');
var processCwd = process.cwd();
chalk.enabled = true;
Expand Down Expand Up @@ -426,18 +427,78 @@ Mundler.prototype.configureBundle = function(bundleName, props, modules, aliases
return true;
})
.map(function(file) {
var src;
var minimatchOpts = { matchBase: true };
var isMatch = false;
var expose = file.replace(basePath + '/', '');

// clone expose path to use in
// matching functionality when prefix
// is an object
var mutableExpose = expose;

if (!!prefix) {
if (prefix.slice(-1) !== '/') {
prefix += '/';
}

expose = prefix + expose;
if (typeof prefix === 'string') {

// If the prefix is a string it
// should be prepended to all expose
// paths
if (prefix.slice(-1) !== '/') {
prefix += '/';
}

expose = prefix + expose;
}
else {

// If the prefix is an object, it contains at least
// a src and dest in which case we need to match the
// filepath to check if it should be rewritten in the
// expose
if (prefix.options) {
minimatchOpts = prefix.options;
}

// update expose to be relative to prefix.cwd
if (prefix.cwd) {
mutableExpose = path.relative(prefix.cwd, mutableExpose);
}

// if the path start with a .. it means the pattern didn't
// match since the expose path is at least one directory lower
// than it should be
if (mutableExpose.charAt(0) !== '.') {
if (typeof prefix.src === 'string') {
isMatch = minimatch(mutableExpose, prefix.src, minimatchOpts);
}
else {
for (var i = 0; i < prefix.src.length; i++) {
src = prefix.src[i];

if (isMatch = minimatch(mutableExpose, src, minimatchOpts)) {
break;
}
}
}

// If the expose path matches the pattern,
// prepend it with the prefix.dest
if (isMatch) {

if (prefix.dest.slice(-1) !== '/') {
prefix.dest += '/';
}

expose = prefix.dest + mutableExpose;
}
}
}
}

// remove file extensions
expose = expose.substr(0, expose.lastIndexOf('.'));
console.log(expose);

b[bMethod](file, { expose: expose });

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
"browserify": "10.2.4",
"chalk": "1.1.0",
"glob": "5.0.13",
"minimatch": "3.0.0",
"object-assign": "3.0.0",
"stream-buffers": "^2.2.0",
"vinyl": "^1.1.0",
"stream-buffers": "2.2.0",
"vinyl": "1.1.0",
"watchify": "3.2.3",
"yargs": "3.15.0"
},
Expand Down
10 changes: 5 additions & 5 deletions test/lib/mundler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('lib/mundler', function() {
});

it('should find all requires/imports', function() {
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'chalk', 'chai'];
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'minimatch', 'chalk', 'chai'];
return m.searchForDependencies('test', [path.resolve('test/fixtures/sample.js')], testConfig.test).should.eventually.deep.equal(expectedModules);
});

Expand All @@ -74,12 +74,12 @@ describe('lib/mundler', function() {
describe('#buildDependencyList()', function() {

it('should return object containing array of external module dependencies', function() {
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'chalk', 'chai'];
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'minimatch', 'chalk', 'chai'];
return m.buildDependencyList('test', testConfig.test).should.eventually.deep.equal(expectedModules);
});

it('should work if no CWD is set', function() {
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'chalk', 'chai'];
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'minimatch', 'chalk', 'chai'];
return m.buildDependencyList('test', testConfigNoCwd.test).should.eventually.deep.equal(expectedModules);
});

Expand All @@ -98,11 +98,11 @@ describe('lib/mundler', function() {
});

it('should recurse if match is internal', function() {
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'chalk'];
var expectedModules = ['fs', 'path', 'util', 'events', 'child_process', 'stream', 'vinyl', 'object-assign', 'browserify', 'watchify', 'bluebird', 'glob', 'minimatch', 'chalk'];
var spy = sinon.spy(m, 'processMatch');

return m.processMatch('test', [path.resolve('test/fixtures/sample.js')], '../../lib/mundler', testConfig.test).then(function(modules) {
spy.should.have.been.callCount(14); // once for initial call and once for each module found (modules in array above)
spy.should.have.been.callCount(15); // once for initial call and once for each module found (modules in array above)
modules.should.deep.equal(expectedModules);
});
});
Expand Down

0 comments on commit e244c45

Please sign in to comment.