Skip to content

Commit

Permalink
#12 Fix pr notations. Realize hack way to use compress and sourcepam …
Browse files Browse the repository at this point in the history
…options together
  • Loading branch information
tormozz48 committed Aug 31, 2015
1 parent 1e35769 commit 3ccb229
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
32 changes: 19 additions & 13 deletions techs/browser-js.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var vow = require('vow'),
var EOL = require('os').EOL,
vow = require('vow'),
vfs = require('enb/lib/fs/async-fs'),
Utils = require('enb-source-map/lib/utils'),
File = require('enb-source-map/lib/file'),
minify = require('uglify-js').minify;

Expand Down Expand Up @@ -63,29 +65,33 @@ module.exports = require('enb/lib/build-flow').create()
.defineOption('iife', false)
.defineOption('compress', false)
.defineOption('sourcemap', false)
.wrapper(function (str) {
if (this._compress) {
return minify(str, { fromString: true }).code;
}

return str;
})
.builder(function (sourceFiles) {
return this._readSourceFiles(sourceFiles)
.then(function (sources) {
var file = new File(this.node.resolvePath(this._target), this._sourcemap),
var file = new File(this.node.resolvePath(this._target), { sourceMap: this._sourcemap }),
needWrapIIFE = this._iife,
needToAddComments = !this._compress;
needToAddComments = !this._compress,
compressOptions = { fromString: true};

sources.forEach(function (source) {
needToAddComments && file.writeLine('/* begin: ' + source.relPath + ' */');
needWrapIIFE && file.writeLine('(function(){');
file.writeFileContent(source.path, source.contents);
needWrapIIFE && file.writeLine('}());');
needToAddComments && file.writeLine('/* end: ' + source.relPath + ' *' + '/');
needToAddComments && file.writeLine('/* end: ' + source.relPath + ' */');
});

return file.render();
if(!this._compress) {
return file.render();
}

if (!this._sourcemap) {
return minify(file.render(), compressOptions).code;
}

file._content = minify(file._content.join(EOL), compressOptions).code;

return Utils.joinContentAndSourceMap(file._content, file._map);
}, this);
})
.methods({
Expand All @@ -94,7 +100,7 @@ module.exports = require('enb/lib/build-flow').create()
*
* @protected
* @param {FileList} files
* @returns {Array.<{path: String, relPath: String, contents: String}>}
* @returns {FileData[]}
*/
_readSourceFiles: function (files) {
var node = this.node;
Expand Down
12 changes: 12 additions & 0 deletions test/techs/browser-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ describe('browser-js', function () {
content.should.match(/sourceMappingURL/);
});
});

it('must generate sourcemap with minification', function () {
var blocks = {
'block0.vanilla.js': 'Hello0',
'block1.browser.js': 'Hello1'
};

return build(blocks, { sourcemap: true, compress: true })
.spread(function (content) {
content.should.match(/Hello0,Hello1;\n\/\/#\ssourceMappingURL/);
});
})
});
});

Expand Down

0 comments on commit 3ccb229

Please sign in to comment.