Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Build apps in sequence when parallel=false #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
node_modules
node_modules
coverage/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The config object should be serializable.

###Options

- cache
- cacheFolder
- Relative path to folder where browserify-incremental cache will be stored
- Default: `false`
- parallel
Expand All @@ -59,7 +59,7 @@ The config object should be serializable.
- options
- Browserify options for this bundle
- exclude
- Modules to exclude from bundle.
- Modules to exclude from bundle.
- Default: all modules included by bundles specified in shared bundles
- The property name is the name of the bundle
- Default: `{}`
Expand Down Expand Up @@ -105,4 +105,4 @@ The config object should be serializable.
- uglify
- Options to pass to uglify. If this is set to an object, all bundles will be minified.
- Default `null`

8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ var createBuilder = require('./src/create-builder');
var createParallelBuilder = require('./src/parallel-runner.js');

module.exports = function createParallelOrNormalBuilder(config, onDone) {
if (!config) {
return;
}

if (config.parallel && !config.watch) {
return createParallelBuilder(config, onDone);
} else {
return createBuilder(config, onDone);
}

return createBuilder(config, onDone);
};
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "browserify-builder",
"version": "1.0.1",
"version": "1.0.2",
"description": "A tool for configuring complicated applications with Browserify",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha",
"coverage": "istanbul cover _mocha"
},
"repository": {
"type": "git",
Expand All @@ -16,13 +17,22 @@
"author": "Buildium",
"license": "MIT",
"dependencies": {
"lodash": "^4.11.1",
"mkdirp": "^0.5.1"
"browserify-cache-api": "3.0.1",
"lodash": "4.17.4",
"mkdirp": "0.5.1"
},
"peerDependencies": {
"browserify": "^13.0.0",
"browserify-incremental": "^3.1.1",
"watchify": "^3.0.0",
"uglify-js": "^2.6.2"
},
"devDependencies": {
"chai": "3.5.0",
"chai-deep-match": "1.0.2",
"istanbul": "0.4.5",
"mocha": "3.2.0",
"sinon": "1.17.7",
"sinon-chai": "2.8.0"
}
}
40 changes: 23 additions & 17 deletions src/browserify-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ var incrementalWatch = require('./incremental-watch.js');
var uglify = require('uglify-js');
var mkdirp = require('mkdirp');

var BUNDLE_COMPLETE_EVENT = 'builderComplete';


function uglifyFile(path, options, callback) {
var result = uglify.minify(path, options);
fs.writeFile(path, result.code, function(err) {
Expand All @@ -21,20 +18,27 @@ function uglifyFile(path, options, callback) {
}

exports.writeBundle = function writeBundle(config) {
mkdirp.sync(path.dirname(config.path));
return config.bundle.bundle().pipe(fs.createWriteStream(config.path)).on('finish', function() {
console.log('Built ' + config.name);
if (config.uglify) {
uglifyFile(config.path, config.uglify, function(err) {
if (!err) {
console.log('Minified ' + config.name);
}
config.bundle.emit(BUNDLE_COMPLETE_EVENT);
});
} else {
config.bundle.emit(BUNDLE_COMPLETE_EVENT);
}
var promise = new Promise(function(resolve) {
mkdirp(path.dirname(config.path), function(err) {
config.bundle.bundle()
.pipe(fs.createWriteStream(config.path))
.on('finish', function() {
console.log('Built ' + config.name);
if (config.uglify) {
uglifyFile(config.path, config.uglify, function(err) {
if (!err) {
console.log('Minified ' + config.name);
}
resolve();
});
} else {
resolve();
}
});
});
});

return promise;
};

exports.watchBundle = function watchBundle(config) {
Expand Down Expand Up @@ -84,7 +88,9 @@ exports.configureAppBundle = function configureAppBundle(bundler, entry, exclude
};

exports.configureSharedBundle = function configureSharedBundle(bundler, include, exclude, entry) {
bundler.external(exclude);
if (exclude) {
bundler.external(exclude);
}
bundler.require(include);
if (entry) {
bundler.add(entry);
Expand Down
42 changes: 17 additions & 25 deletions src/create-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ var defaultConfig = {
var getPath = function getPathFn(pattern, mod) {
if (mod.path) {
return mod.path;
} else {
return pattern.replace('[name]', mod.name);
}
return pattern.replace('[name]', mod.name);
};

var getModuleFromTarget = curry(function getTargetFn(config, target) {
if (config.shared[target]) {
return extend({name: target, type: 'shared'}, config.shared[target]);
} else {
return extend({name: target, type: 'app'}, config.apps[target]);
}
return extend({name: target, type: 'app'}, config.apps[target]);
});

var getSharedLibs = flow(values, map('include'), flatten);
Expand All @@ -49,6 +47,9 @@ var addFilesToBundle = curry(function addFilesToBundleFn(config, mod) {
});

var addUglify = curry(function addUglifyFn(config, mod) {
if (!config.uglify) {
return mod;
}
return extend(mod, { uglify: config.uglify });
});

Expand Down Expand Up @@ -81,18 +82,6 @@ var configureModule = curry(function configureModuleFn(config, mod) {
)(mod);
});

var allEventsFinished = curry(function onAllFinished(event, callback, emitters) {
var emitterCount = 0;
emitters.forEach(function(emitter) {
emitter.on(event, function() {
emitterCount++;
if (emitterCount === emitters.length) {
callback();
}
});
});
});

function createConfigureFlow(config) {
return flow(
getModuleFromTarget(config),
Expand All @@ -109,7 +98,6 @@ module.exports = function bundler(userConfig, onDone) {
var config = extend(defaultConfig, userConfig);
var targets = getTargetList(config);
var configureTarget = createConfigureFlow(config);
var execOnDoneAfterFinished = allEventsFinished('builderComplete', onDone);

if (config.cacheFolder) {
mkdirp.sync(config.cacheFolder);
Expand All @@ -119,12 +107,16 @@ module.exports = function bundler(userConfig, onDone) {
if (config.watch) {
bundles.forEach(bundlerTools.watchBundle);
} else {
if (onDone) {
execOnDoneAfterFinished(map('bundle', bundles));
}
bundles.forEach(bundlerTools.writeBundle);
var promise = Promise.resolve();

bundles.forEach(function(config) {
promise = promise.then(function() {
return bundlerTools.writeBundle(config);
});
});

promise.then(onDone, onDone);
}
return bundles;
}

exports.buildAll = function buildAll() {
Expand All @@ -135,17 +127,17 @@ module.exports = function bundler(userConfig, onDone) {
};

exports.buildSingle = function buildSingle(userTarget) {
return build(
build(
targets
.filter(function(item) {
return item === userTarget;
})
.map(configureTarget)
)[0];
);
};

exports.buildMulti = function buildMulti(userTargets) {
return build(
build(
targets
.filter(function(item) {
return includes(userTargets, item);
Expand Down
8 changes: 3 additions & 5 deletions src/parallel-runner-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

var createBuilder = require('./create-builder.js');

var BUILD_COMPLETE_EVENT = 'builderComplete';

process.on('message', function(message) {
var options = JSON.parse(message);

var builder = createBuilder(options.config);

builder.buildSingle(options.target).bundle.on(BUILD_COMPLETE_EVENT, function() {
var builder = createBuilder(options.config, function() {
process.send('done');
});

builder.buildSingle(options.target);
});
68 changes: 68 additions & 0 deletions test/browserify-builder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var chai = require('chai');
var sinon = require('sinon');
chai.should();
chai.use(require('sinon-chai'));

require('../src/create-builder');
require('../src/parallel-runner');

describe('browserify-builder', function() {
var sandbox,
browserifyBuilder,
createBuilder,
createParallelBuilder;

before(function() {
sandbox = sinon.sandbox.create();
createBuilder = sandbox.stub( require.cache[ require.resolve( '../src/create-builder' ) ], 'exports');
createParallelBuilder = sandbox.stub( require.cache[ require.resolve( '../src/parallel-runner' ) ], 'exports');
browserifyBuilder = require('../index');
});

beforeEach(function() {
createBuilder.reset();
createParallelBuilder.reset();
});

after(function() {
sandbox.restore();
});

it('should be a noop if no config is provided', function() {
browserifyBuilder();
createBuilder.should.have.not.been.called;
createParallelBuilder.should.have.not.been.called;
});

it('should create a builder', function() {
var config = {};
browserifyBuilder(config);
createBuilder.should.have.been.calledWith(config);
});

it('should pass callback to builder if provided', function() {
var config = {};
var callback = function() {};
browserifyBuilder(config, callback);
createBuilder.should.have.been.calledWith(config, callback);
});

it('should create a parallel builder if the parallel is true', function() {
var config = { parallel: true };
browserifyBuilder(config);
createParallelBuilder.should.have.been.calledWith(config);
});

it('should not create a parallel builder if watch is true', function() {
var config = { parallel: true, watch: true };
browserifyBuilder(config);
createParallelBuilder.should.not.have.been.called;
});

it('should pass callback to parallel builder if provided', function() {
var config = { parallel: true };
var callback = function() {};
browserifyBuilder(config, callback);
createParallelBuilder.should.have.been.calledWith(config, callback);
});
});
Loading