diff --git a/.eslintignore b/.eslintignore index 3278600389..11b456699f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -17,3 +17,4 @@ logs/ /coverage /build .eslintrc +test/files diff --git a/src/meta/minifier.js b/src/meta/minifier.js index da7c570ce7..7c1a83bea4 100644 --- a/src/meta/minifier.js +++ b/src/meta/minifier.js @@ -156,7 +156,7 @@ function concat(data, callback) { return callback(err); } - var output = files.join(os.EOL + ';'); + var output = files.join('\n;'); callback(null, { code: output }); }); diff --git a/test/build.js b/test/build.js index 3b6890ebe6..20d43033a9 100644 --- a/test/build.js +++ b/test/build.js @@ -1,8 +1,102 @@ 'use strict'; +var string = require('string'); +var path = require('path'); +var fs = require('fs'); var assert = require('assert'); +var mkdirp = require('mkdirp'); var db = require('./mocks/databasemock'); +var file = require('../src/file'); + +describe('minifier', function () { + before(function (done) { + mkdirp(path.join(__dirname, '../build/test'), done); + }); + + var minifier = require('../src/meta/minifier'); + var scripts = [ + path.resolve(__dirname, './files/1.js'), + path.resolve(__dirname, './files/2.js'), + ]; + it('.js.bundle() should concat scripts', function (done) { + minifier.js.bundle(scripts, false, false, function (err, bundle) { + assert.ifError(err); + assert.strictEqual( + bundle.code, + '(function (window, document) {' + + '\n\twindow.doStuff = function () {' + + '\n\t\tdocument.body.innerHTML = \'Stuff has been done\';' + + '\n\t};' + + '\n})(window, document);' + + '\n' + + '\n;function foo(name, age) {' + + '\n\treturn \'The person known as "\' + name + \'" is \' + age + \' years old\';' + + '\n}' + + '\n' + ); + done(); + }); + }); + + it('.js.bundle() should minify scripts', function (done) { + minifier.js.bundle(scripts, true, false, function (err, bundle) { + assert.ifError(err); + assert.strictEqual( + bundle.code, + '(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);function foo(n,o){return\'The person known as "\'+n+\'" is \'+o+" years old"}' + ); + done(); + }); + }); + + it('.js.minifyBatch() should minify each script', function (done) { + var s = scripts.map(function (script) { + return { + srcPath: script, + destPath: path.resolve(__dirname, '../build/test', path.basename(script)), + }; + }); + minifier.js.minifyBatch(s, false, function (err) { + assert.ifError(err); + + assert(file.existsSync(s[0].destPath)); + assert(file.existsSync(s[1].destPath)); + + fs.readFile(s[0].destPath, function (err, buffer) { + assert.ifError(err); + assert.strictEqual( + buffer.toString(), + '(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);' + ); + done(); + }); + }); + }); + + var styles = [ + '@import (inline) "./1.css";', + '@import "./2.less";', + ].join('\n'); + var paths = [ + path.resolve(__dirname, './files'), + ]; + it('.css.bundle() should concat styles', function (done) { + minifier.css.bundle(styles, paths, false, false, function (err, bundle) { + assert.ifError(err); + assert.strictEqual(bundle.code, '.help { margin: 10px; } .yellow { background: yellow; }\n.help {\n display: block;\n}\n.help .blue {\n background: blue;\n}\n'); + done(); + }); + }); + + it('.css.bundle() should minify styles', function (done) { + minifier.css.bundle(styles, paths, true, false, function (err, bundle) { + assert.ifError(err); + assert.strictEqual(bundle.code, '.help{margin:10px;display:block}.yellow{background:#ff0}.help .blue{background:#00f}'); + done(); + }); + }); +}); describe('Build', function () { it('should build all assets', function (done) { diff --git a/test/files/1.css b/test/files/1.css new file mode 100644 index 0000000000..840cf64b36 --- /dev/null +++ b/test/files/1.css @@ -0,0 +1 @@ +.help { margin: 10px; } .yellow { background: yellow; } \ No newline at end of file diff --git a/test/files/1.js b/test/files/1.js new file mode 100644 index 0000000000..b20055f8ee --- /dev/null +++ b/test/files/1.js @@ -0,0 +1,5 @@ +(function (window, document) { + window.doStuff = function () { + document.body.innerHTML = 'Stuff has been done'; + }; +})(window, document); diff --git a/test/files/2.js b/test/files/2.js new file mode 100644 index 0000000000..9369213316 --- /dev/null +++ b/test/files/2.js @@ -0,0 +1,3 @@ +function foo(name, age) { + return 'The person known as "' + name + '" is ' + age + ' years old'; +} diff --git a/test/files/2.less b/test/files/2.less new file mode 100644 index 0000000000..cdd5d5b5f2 --- /dev/null +++ b/test/files/2.less @@ -0,0 +1 @@ +.help { display: block; .blue { background: blue; } } \ No newline at end of file