From 0e90e5f86be73f017aa0c7f60c780ece2e5bac33 Mon Sep 17 00:00:00 2001 From: andrewblond Date: Mon, 24 Aug 2015 10:04:40 +0300 Subject: [PATCH] Added `compress` option for `browser-js` tech --- package.json | 11 ++++++----- techs/browser-js.js | 28 ++++++++++++++++++++++------ test/techs/browser-js.test.js | 21 ++++++++++++++++++--- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index e294035..7897e42 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,12 @@ "peerDependencies": { "enb": ">= 0.8.22" }, + "dependencies": { + "browserify": "10.2.6", + "uglify-js": "2.4.24", + "vow": "0.4.10", + "vow-node": "0.3.0" + }, "devDependencies": { "chai": "3.2.0", "chai-as-promised": "5.1.0", @@ -46,10 +52,5 @@ "unit": "mocha -R spec", "cover": "istanbul cover _mocha", "coveralls": "npm i coveralls && npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls" - }, - "dependencies": { - "browserify": "10.2.6", - "vow": "0.4.10", - "vow-node": "0.3.0" } } diff --git a/techs/browser-js.js b/techs/browser-js.js index 76ca9c0..1f47026 100644 --- a/techs/browser-js.js +++ b/techs/browser-js.js @@ -1,4 +1,5 @@ -var EOL = require('os').EOL; +var minify = require('uglify-js').minify, + EOL = require('os').EOL; /** * @class BrowserJsTech @@ -19,6 +20,7 @@ var EOL = require('os').EOL; * involved in the assembly. * @param {Boolean} [options.iife=false] Adds an option to wrap merged
* files to IIFE. + * @param {Boolean} [options.compress=false] Minifies and compress JS code. * * @example * // Code in a file system before build: @@ -56,6 +58,14 @@ module.exports = require('enb/lib/build-flow').create() .target('target', '?.browser.js') .useFileList(['vanilla.js', 'js', 'browser.js']) .defineOption('iife', false) + .defineOption('compress', false) + .wrapper(function (str) { + if (this._compress) { + return minify(str, { fromString: true }).code; + } + + return str; + }) .justJoinFiles(function (filename, contents) { var relPath = this.node.relativePath(filename); @@ -66,10 +76,16 @@ module.exports = require('enb/lib/build-flow').create() '}());' ].join(EOL); } - return [ - '/* begin: ' + relPath + ' */', - contents, - '/* end: ' + relPath + ' *' + '/' - ].join(EOL); + + // after compress comments will be removed + if (!this._compress) { + contents = [ + '/* begin: ' + relPath + ' */', + contents, + '/* end: ' + relPath + ' *' + '/' + ].join(EOL); + } + + return contents; }) .createTech(); diff --git a/test/techs/browser-js.test.js b/test/techs/browser-js.test.js index 4824571..c46e99a 100644 --- a/test/techs/browser-js.test.js +++ b/test/techs/browser-js.test.js @@ -13,7 +13,7 @@ describe('browser-js', function () { mock.restore(); }); - describe('must join files with comments', function () { + describe('join files', function () { it('must join all files', function () { var blocks = { 'block0.vanilla.js': 'Hello0', @@ -29,8 +29,23 @@ describe('browser-js', function () { ].join(EOL); return build(blocks) - .then(function (content) { - content[0].should.be.equal(reference); + .spread(function (content) { + content.should.be.equal(reference); + }); + }); + }); + + describe('compress', function () { + it('must compress files', function () { + var blocks = { + 'block0.vanilla.js': 'var b = function () {};', + 'block1.browser.js': 'if (foo) { bar(); }' + }, + reference = 'var b=function(){};foo&&bar();'; + + return build(blocks, { compress: true }) + .spread(function (content) { + content.should.be.equal(reference); }); }); });