Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ inherits( Parcelify, EventEmitter );

function Parcelify( browserifyInstance, options ) {
var _this = this;

this.preProcessors = [];

if( ! ( this instanceof Parcelify ) ) return new Parcelify( browserifyInstance, options );

options = _.defaults( {}, options, {
Expand Down Expand Up @@ -127,25 +128,34 @@ Parcelify.prototype.processParcels = function( browserifyInstance, options, call
process.nextTick( function() {
async.series( [ function( nextSeries ) {
// fire package events for any new packages
_.each( packagesThatWereCreated, function( thisPackage ) {
var isParcel = thisPackage.isParcel;
var pipelinePackages = _.map( packagesThatWereCreated, function( thisPackage ) {
return function (nextPackage) {
var isParcel = thisPackage.isParcel;

log.verbose( 'Created new ' + ( isParcel ? 'parcel' : 'package' ) + ' ' + thisPackage.path + ' with id ' + thisPackage.id );

log.verbose( 'Created new ' + ( isParcel ? 'parcel' : 'package' ) + ' ' + thisPackage.path + ' with id ' + thisPackage.id );
existingPackages[ thisPackage.id ] = thisPackage;
if( isParcel ) {
_this._setupParcelEventRelays( thisPackage );
parcelsThatWereCreated.push( thisPackage );
}

existingPackages[ thisPackage.id ] = thisPackage;
if( isParcel ) {
_this._setupParcelEventRelays( thisPackage );
parcelsThatWereCreated.push( thisPackage );
}
thisPackage.on( 'error', function( err ) {
_this.emit( 'error', err );
} );

thisPackage.on( 'error', function( err ) {
_this.emit( 'error', err );
} );
var pipelineProcessors = _.map( _this.preProcessors, function (p) {
return async.apply( p, thisPackage);
});

_this.emit( 'packageCreated', thisPackage );
async.parallel(pipelineProcessors, nextPackage);

_this.emit( 'packageCreated', thisPackage );
};
} );

nextSeries();
async.parallel(pipelinePackages, nextSeries);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the magic, we will not move to the next series function until all the processors are finished. We still emit packageCreate event


}, function( nextSeries ) {
if( parcelsThatWereCreated.length > 1 && ! options.bundlesByEntryPoint ) {
return nextSeries( new Error( 'Multiple entry points detected, but bundlesByEntryPoint option was not supplied.' ) );
Expand Down Expand Up @@ -201,6 +211,10 @@ Parcelify.prototype.processParcels = function( browserifyInstance, options, call
} );
};

Parcelify.prototype.addPreProcessor = function (preProcessor) {
this.preProcessors.push(preProcessor);
};

Parcelify.prototype.instantiateParcelAndPackagesFromMap = function( parcelMapResult, existingPacakages, assetTypes, appTransforms, appTransformDirs, callback ) {
var _this = this;
var mappedParcel = null;
Expand Down