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 0e90e5f commit 9905502
Show file tree
Hide file tree
Showing 2 changed files with 48 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 compress 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('utf-8');

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

return str;
}, this);
}

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

describe('compress', function () {
var reference = [
'!function r(n,e,o){function t(f,i){if(!e[f]){if(!n[f]){' +
'var c="function"==typeof require&&require;if(!i&&c)return c(f,!0);if(u)return u(f,!0);' +
'var a=new Error("Cannot find module \'"+f+"\'");' +
'throw a.code="MODULE_NOT_FOUND",a}var p=e[f]={exports:{}};n[f][0].call(p.exports,function(r){' +
'var e=n[f][1][r];return t(e?e:r)},p,p.exports,r,n,e,o)}return e[f].exports}' +
'for(var u="function"==typeof require&&require,f=0;f<o.length;f++)t(o[f]);return t}' +
'({1:[function(r,n,e){},{}],2:[function(r,n,e){foo&&bar()},{}]},{},[1,2]);'
].join(''),
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.be.equal(reference);
});
});

it('must not compress if files is not bundled', function () {
var reference = 'var b=function(){};foo&&bar();';

return build(scheme, { compress: true })
.spread(function (content) {
content.should.not.be.equal(reference);
});
});
});

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

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

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

0 comments on commit 9905502

Please sign in to comment.