Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress Option #32

Merged
merged 1 commit into from
Aug 24, 2015
Merged
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
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 compresses 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 compression 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