Pull all imports with css-parse

This commit is contained in:
Slava Kim
2013-12-13 16:40:12 -08:00
parent 21cd9172fe
commit ceea2a1cf2
4 changed files with 48 additions and 6 deletions

View File

@@ -34,6 +34,22 @@
"version": "1.0.1"
}
}
},
"css-parse": {
"version": "1.6.0"
},
"css-stringify": {
"version": "1.4.1",
"dependencies": {
"source-map": {
"version": "0.1.31",
"dependencies": {
"amdefine": {
"version": "0.1.0"
}
}
}
}
}
}
}

View File

@@ -5,4 +5,8 @@ CleanCSSProcess = function (source, options) {
return instance.minify(source);
};
CssParse = Npm.require('css-parse');
CssStringify = Npm.require('css-stringify');
UglifyJSMinify = Npm.require('uglify-js').minify;

View File

@@ -5,10 +5,13 @@ Package.describe({
Npm.depends({
"clean-css": "2.0.2",
"uglify-js": "2.4.7"
"uglify-js": "2.4.7",
"css-parse": "1.6.0",
"css-stringify": "1.4.1"
});
Package.on_use(function (api) {
api.export(['CleanCSSProcess', 'UglifyJSMinify']);
api.export(['CssParse', 'CssStringify']);
api.add_files('minifiers.js', 'server');
});

View File

@@ -759,13 +759,32 @@ _.extend(ClientTarget.prototype, {
minifyCss: function (minifiers) {
var self = this;
var allCss = _.map(self.css, function (file) {
return file.contents('utf8');
}).join('\n');
// after concatenation some @import's might be left in the middle of CSS
// file but they required to be in the beginning.
var importsAst = {
type: 'stylesheet',
stylesheet: { rules: [] }
};
allCss = minifiers.CleanCSSProcess(allCss);
var cssAsts = _.map(self.css, function (file) {
var ast = minifiers.CssParse(file.contents('utf8'));
function isImportRule (node) {
return node.type === 'import';
}
self.css = [new File({ data: new Buffer(allCss, 'utf8') })];
var imports = _.filter(ast.stylesheet.rules, isImportRule);
importsAst.stylesheet.rules = importsAst.stylesheet.rules.concat(imports);
ast.stylesheet.rules = _.reject(ast.stylesheet.rules, isImportRule);
return ast;
});
var allCss =
_.map([importsAst].concat(cssAsts), minifiers.CssStringify).join('\n');
var minifiedCss = minifiers.CleanCSSProcess(allCss);
self.css = [new File({ data: new Buffer(minifiedCss, 'utf8') })];
self.css[0].setUrlToHash(".css");
},