Skip to content

Commit

Permalink
Added compress option to node-js tech
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewblond committed Aug 24, 2015
1 parent cee0376 commit 0974033
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 13 additions & 2 deletions techs/node-js.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var EOL = require('os').EOL,
browserify = require('browserify'),
minify = require('uglify-js').minify,
promisify = require('vow-node').promisify;

/**
Expand All @@ -21,6 +22,9 @@ var EOL = require('os').EOL,
* in the assembly.
* @param {Boolean} [options.devMode=true] Drops cache for `require` of source modules.
* @param {Boolean} [options.bundled=false] Builds CommonJS files in one file.
* @param {Boolean} [options.compress=false] Minifies and compresses JS code.
* Works if `bundled` option is enabled.
*
* @example
* // Code in a file system before build:
Expand Down Expand Up @@ -58,6 +62,7 @@ module.exports = require('enb/lib/build-flow').create()
.useFileList(['vanilla.js', 'node.js'])
.defineOption('devMode', true)
.defineOption('bundled', false)
.defineOption('compress', false)
.builder(function (sourceFiles) {
var node = this.node,
dropRequireCacheFunc = [
Expand Down Expand Up @@ -94,8 +99,14 @@ module.exports = require('enb/lib/build-flow').create()

return bundle()
.then(function (buf) {
return buf.toString('utf-8');
});
var str = buf.toString();

if (this._compress) {
return minify(str, { fromString: true }).code;
}

return str;
}, this);
}

return [
Expand Down
26 changes: 25 additions & 1 deletion test/techs/node-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ describe('node-js', function () {
});
});

describe('compress', function () {
var scheme = {
blocks: {
'block0.vanilla.js': 'var b = function () {};',
'block1.node.js': 'if (foo) { bar(); }'
}
};

it('must compress files', function () {
return build(scheme, { compress: true, bundled: true })
.spread(function (content) {
content.should.include('foo&&bar()');
});
});

it('must not compress if files is not bundled', function () {
return build(scheme, { compress: true })
.spread(function (content) {
content.should.include('require("../blocks/block0.vanilla.js");')
.and.include('require("../blocks/block1.node.js");');
});
});
});

describe('devMode', function () {
var scheme = {
blocks: {
Expand Down Expand Up @@ -214,5 +238,5 @@ function build(scheme, options) {

bundle.provideTechData('?.files', fileList);

return bundle.runTech(NodeJsTech, options);
return bundle.runTechAndGetContent(NodeJsTech, options);
}

0 comments on commit 0974033

Please sign in to comment.