mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Merge pull request #12105 from meteor/feature/package-minifier-css-async
Create async method `CssTools.minifyCssAsync`
This commit is contained in:
51
packages/minifier-css/minifier-async-tests.js
Normal file
51
packages/minifier-css/minifier-async-tests.js
Normal file
@@ -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);
|
||||
}
|
||||
);
|
||||
@@ -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<String[]>} 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 => {
|
||||
|
||||
@@ -19,6 +19,7 @@ Package.onTest(function (api) {
|
||||
api.use('tinytest');
|
||||
api.addFiles([
|
||||
'minifier-tests.js',
|
||||
'minifier-async-tests.js',
|
||||
'urlrewriting-tests.js'
|
||||
], 'server');
|
||||
});
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user