diff --git a/packages/minifier-css/minifier-async-tests.js b/packages/minifier-css/minifier-async-tests.js new file mode 100644 index 0000000000..755595ba6e --- /dev/null +++ b/packages/minifier-css/minifier-async-tests.js @@ -0,0 +1,51 @@ +import { CssTools } from './minifier'; +const TEST_CASES = [ + ['a \t\n{ color: red } \n', 'a{color:red}', 'whitespace check'], + [ + 'a \t\n{ color: red; margin: 1; } \n', + 'a{color:red;margin:1}', + 'only last one loses semicolon', + ], + [ + 'a \t\n{ color: red;;; margin: 1;;; } \n', + 'a{color:red;margin:1}', + 'more semicolons than needed', + ], + ['a , p \t\n{ color: red; } \n', 'a,p{color:red}', 'multiple selectors'], + ['body {}', '', 'removing empty rules'], + [ + '*.my-class { color: #fff; }', + '.my-class{color:#fff}', + 'removing universal selector', + ], + [ + 'p > *.my-class { color: #fff; }', + 'p>.my-class{color:#fff}', + 'removing optional whitespace around ">" in selector', + ], + [ + 'p + *.my-class { color: #fff; }', + 'p+.my-class{color:#fff}', + 'removing optional whitespace around "+" in selector', + ], + [ + 'a {\n\ + font:12px \'Helvetica\',"Arial",\'Nautica\';\n\ + background:url("/some/nice/picture.png");\n}', + 'a{font:12px Helvetica,Arial,Nautica;background:url(/some/nice/picture.png)}', + 'removing quotes in font and url (if possible)', + ], + ['/* no comments */ a { color: red; }', 'a{color:red}', 'remove comments'], +]; + +Tinytest.addAsync( + '[Async] minifier-css - simple CSS minification', + async (test) => { + const promises = TEST_CASES.map(([css, expected, desc]) => + CssTools.minifyCssAsync(css).then((minifiedCss) => { + test.equal(minifiedCss[0], expected, desc); + }) + ); + return Promise.all(promises); + } +); diff --git a/packages/minifier-css/minifier.js b/packages/minifier-css/minifier.js index 174452f1ee..a4c662e9e5 100644 --- a/packages/minifier-css/minifier.js +++ b/packages/minifier-css/minifier.js @@ -1,6 +1,5 @@ import path from 'path'; import url from 'url'; -import Future from 'fibers/future'; import postcss from 'postcss'; import cssnano from 'cssnano'; @@ -65,23 +64,21 @@ const CssTools = { * @return {String[]} Array containing the minified CSS. */ minifyCss(cssText) { - const f = new Future; - postcss([ - cssnano({ safe: true }), - ]).process(cssText, { - from: void 0, - }).then(result => { - f.return(result.css); - }).catch(error => { - f.throw(error); - }); - const minifiedCss = f.wait(); + return Promise.await(CssTools.minifyCssAsync(cssText)); + }, - // Since this function has always returned an array, we'll wrap the - // minified css string in an array before returning, even though we're - // only ever returning one minified css string in that array (maintaining - // backwards compatibility). - return [minifiedCss]; + /** + * Minify the passed in CSS string. + * + * @param {string} cssText CSS string to minify. + * @return {Promise} Array containing the minified CSS. + */ + async minifyCssAsync(cssText) { + return await postcss([cssnano({ safe: true })]) + .process(cssText, { + from: void 0, + }) + .then((result) => [result.css]); }, /** @@ -187,6 +184,7 @@ if (typeof Profile !== 'undefined') { 'parseCss', 'stringifyCss', 'minifyCss', + 'minifyCssAsync', 'mergeCssAsts', 'rewriteCssUrls', ].forEach(funcName => { diff --git a/packages/minifier-css/package.js b/packages/minifier-css/package.js index 022ed4c78c..057a6f2104 100644 --- a/packages/minifier-css/package.js +++ b/packages/minifier-css/package.js @@ -19,6 +19,7 @@ Package.onTest(function (api) { api.use('tinytest'); api.addFiles([ 'minifier-tests.js', + 'minifier-async-tests.js', 'urlrewriting-tests.js' ], 'server'); }); diff --git a/packages/standard-minifier-css/plugin/minify-css.js b/packages/standard-minifier-css/plugin/minify-css.js index 2b8c4d5e44..8ac2b0db75 100644 --- a/packages/standard-minifier-css/plugin/minify-css.js +++ b/packages/standard-minifier-css/plugin/minify-css.js @@ -60,7 +60,7 @@ class CssToolsMinifier { path: 'merged-stylesheets.css' }]; } else { - const minifiedFiles = CssTools.minifyCss(merged.code); + const minifiedFiles = await CssTools.minifyCssAsync(merged.code); result = minifiedFiles.map(minified => ({ data: minified