Skip to content

Commit

Permalink
Added compress option for browser-js tech
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewblond committed Aug 24, 2015
1 parent bf5a6c0 commit 0e90e5f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
28 changes: 22 additions & 6 deletions techs/browser-js.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var EOL = require('os').EOL;
var minify = require('uglify-js').minify,
EOL = require('os').EOL;

/**
* @class BrowserJsTech
Expand All @@ -19,6 +20,7 @@ var EOL = require('os').EOL;
* involved in the assembly.
* @param {Boolean} [options.iife=false] Adds an option to wrap merged<br>
* files to IIFE.
* @param {Boolean} [options.compress=false] Minifies and compress JS code.
*
* @example
* // Code in a file system before build:
Expand Down Expand Up @@ -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);

Expand All @@ -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();
21 changes: 18 additions & 3 deletions test/techs/browser-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
});
});
});
Expand Down

0 comments on commit 0e90e5f

Please sign in to comment.