Skip to content

Commit

Permalink
fix(plug-in): use absolute paths to reference entry points
Browse files Browse the repository at this point in the history
Closes #52
  • Loading branch information
nikku committed Dec 21, 2014
1 parent d38fd2d commit 0cac743
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 78 deletions.
87 changes: 28 additions & 59 deletions lib/bro.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ function Bro(bundleFile) {
'transform', 'plugin', 'configure', 'bundleDelay'
]));


if ('prebundle' in browserifyOptions) {
log.warn('The prebundle hook got removed in favor of configure');
}


var w = watchify(browserify(browserifyOptions));
w.setMaxListeners(Infinity);

_.forEach(bopts.plugin, function(p) {
// ensure we can pass plugin options as
Expand Down Expand Up @@ -188,9 +188,6 @@ function Bro(bundleFile) {
log.info(msg);
});

// files contained in bundle
var files = [ ];


// update bundle file
w.on('bundled', function(err, content) {
Expand All @@ -211,61 +208,34 @@ function Bro(bundleFile) {
rebuild();
}


var MISSING_MESSAGE = /^Cannot find module '([^']+)'/;

var rebuild = _.debounce(function rebuild() {

// check if we already bundled once and
// restore transforms / plug-ins accordingly
// after resetting the bundle

if (w._bundled) {
log.debug('resetting bundle');

var recorded = w._recorded;
w.reset();

log.debug('resetting bundle');

recorded.forEach(function (tr) {
if (tr.transform) {
w.pipeline.write(tr);

recorded.forEach(function(e) {
// we remove missing files on the fly
// to cope with bundle internals missing
if (e.file && !fs.existsSync(e.file)) {
log.debug('removing missing file', path.relative(config.basePath, e.file));
} else {
w.pipeline.write(e);
}
});
}

// fire this small little event so that
// parties may take part in the bundling process
// in the configure hook

w.emit('prebundle');



w.emit('prebundle', w);

log.debug('bundling');

files.forEach(function(f) {
w.require(f, { expose: path.relative(config.basePath, f) });
});


w.bundle(function(err, content) {

if (err) {
log.error('bundle error');
log.error(String(err));

// try to recover from removed test case
// rebuild, if successful
var match = MISSING_MESSAGE.exec(err.message);
if (match) {
var idx = files.indexOf(match[1]);

if (idx !== -1) {
log.debug('removing %s from bundle', match[1]);
files.splice(idx, 1);

log.debug('attempting rebuild');
return rebuild();
}
}
}

w.emit('bundled', err, content);
Expand All @@ -276,28 +246,27 @@ function Bro(bundleFile) {
w.bundleFile = function(file, done) {

var absolutePath = file.path,
relativePath = path.relative(config.basePath, absolutePath);

if (files.indexOf(absolutePath) === -1) {

// add file
log.debug('adding %s to bundle', relativePath);

files.push(absolutePath);
}
relativePath = path.relative(config.basePath, absolutePath),
cache = w._options.cache;

// add file
log.debug('updating %s in bundle', relativePath);

// add the file during next prebundle step
w.once('prebundle', function() {
w.require('./' + relativePath, { expose: absolutePath });
});

deferredBundle(function(err) {
done(err, 'require("' + escape(relativePath) + '");');
done(err, 'require("' + absolutePath + '");');
});
};


/**
* Wait for the bundle creation to have stabilized (no more additions) and invoke a callback.
*
* @param {Function} cb
* @param {Number} delay
* @param {Number} timeout
* @param {Function} [callback] invoked with (err, content)
*/
w.deferredBundle = deferredBundle;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"bugs": "https://github.com/Nikku/karma-browserify/issues",
"dependencies": {
"browserify": "~7.0.2",
"browserify": "Nikku/node-browserify.git#local-fix",
"convert-source-map": "~0.3.3",
"js-string-escape": "^1.0.0",
"lodash": "~2.4.1",
Expand Down
5 changes: 2 additions & 3 deletions test/spec/browserifySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe('browserify', function() {
it('should expose external require', function(done) {

// given
bundler.add('./test/fixtures/a.js');
bundler.require('./test/fixtures/a.js', { expose: './test/fixtures/a.js' });
bundler.require('./test/fixtures/a.js', { expose: 'a' });

// when
bundler.bundle(function(err, content) {
Expand All @@ -34,7 +33,7 @@ describe('browserify', function() {
content = content + '\nexpect(require(moduleName)).to.equal("A");';
vm.runInNewContext(content, {
expect: expect,
moduleName: '/' + path.relative('.', 'test/fixtures/a.js')
moduleName: 'a'
});

done(err);
Expand Down
46 changes: 31 additions & 15 deletions test/spec/pluginSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,21 @@ function preprocess(bundle, testFiles, done) {
});
}

function expectedBundleFile(filename) {
return escape(path.resolve(filename));
}

function expectedBundle(filename) {
return 'require("' + escape(path.relative('', filename)) + '");';
return 'require("' + expectedBundleFile(filename) + '");';
}

function expectBundleContainments(bundleFile, testFiles) {
var extractedFiles = unpack(bundleFile.bundled).map(function(row) { return row.id; });

_.forEach(testFiles, function(f) {
expect(extractedFiles).to.contain(f.path);
});
}

describe('bro', function() {

Expand Down Expand Up @@ -264,11 +275,8 @@ describe('bro', function() {

// then
// bundle got created
var bundledFiles = unpack(bundleFile.bundled)
.map(function (row) { return row.id; });
expect(bundledFiles).to.contain(path.relative('', 'test/fixtures/c.js'));
expect(bundledFiles).to.contain(path.relative('', 'test/fixtures/b.js'));

expectBundleContainments(bundleFile, [ testFileB, testFileC ]);

// test file stub got created
expect(testFileB.bundled).to.eql(expectedBundle('test/fixtures/b.js'));
expect(testFileC.bundled).to.eql(expectedBundle('test/fixtures/c.js'));
Expand Down Expand Up @@ -296,9 +304,7 @@ describe('bro', function() {
// then

// bundle got passed through
var bundledFiles = unpack(bundleFile.bundled)
.map(function (row) { return row.id; });
expect(bundledFiles).to.contain(path.relative('', 'test/fixtures/b.js'));
expectBundleContainments(bundleFile, [ testFile ]);

// test file got regenerated
expect(testFile.bundled).to.eql(expectedBundle('test/fixtures/b.js'));
Expand Down Expand Up @@ -462,7 +468,7 @@ describe('bro', function() {

expect(bundleFile.realContents()).not.to.contain('/b.js');
done();
}, 3000);
}, 5000);

});

Expand Down Expand Up @@ -547,6 +553,8 @@ describe('bro', function() {
// then
// bundle got created
expect(bundleFile.bundled).to.exist;
expect(bundleFile.bundled).to.contain('require(\'foobar\')');

done();
});
});
Expand Down Expand Up @@ -607,33 +615,41 @@ describe('bro', function() {


it('should persist transforms', function(done) {
// given
var bundleFile = createFile(bundle.location);
var testFile = createFile('test/fixtures/transform.js');

var plugin = createPlugin({
browserify: {
transform: [ 'brfs' ],
// Hook into bundler/pipeline events for success/error
configure: function(bundle) {
// After first bundle

// after first bundle
bundle.once('bundled', function (err) {
// Fail if there was an error

// fail if there was an error
if (err) return done(err);
// Set up error/success handlers

// set up error/success handlers
bundle.on('bundle', function (pipeline) {
pipeline
.on('error', done)
.on('end', function() {
console.log(bundleFile.bundled);
expect(bundleFile.bundled).to.contain("module.exports.text = '<' + \"HALLO\" + '>'");
done();
});
});
// Rebundle

// rebundle
plugin.preprocess(bundleFile, [ testFile ], function() {});
});
}
}
});
// Initial bundle

// initial bundle
plugin.preprocess(bundleFile, [ testFile ], function() {});
});

Expand Down

0 comments on commit 0cac743

Please sign in to comment.