mirror of
https://github.com/hanzoai/postcss.git
synced 2026-09-24 17:01:56 +00:00
83 lines
2.1 KiB
JavaScript
83 lines
2.1 KiB
JavaScript
var Declaration = require('./declaration');
|
|
var Comment = require('./comment');
|
|
var AtRule = require('./at-rule');
|
|
var Result = require('./result');
|
|
var Rule = require('./rule');
|
|
var Root = require('./root');
|
|
|
|
// List of functions to process CSS
|
|
class PostCSS {
|
|
constructor(processors = []) {
|
|
this.processors = processors.map( (i) => this.normalize(i) );
|
|
}
|
|
|
|
// Add another function to CSS processors
|
|
use(processor) {
|
|
processor = this.normalize(processor);
|
|
this.processors.push(processor);
|
|
return this;
|
|
}
|
|
|
|
// Process CSS throw installed processors
|
|
process(css, opts = { }) {
|
|
if ( opts.map == 'inline' ) opts.map = { inline: true };
|
|
|
|
var parsed;
|
|
if ( css instanceof Root ) {
|
|
parsed = css;
|
|
} else if ( css instanceof Result ) {
|
|
parsed = css.root;
|
|
} else {
|
|
parsed = postcss.parse(css, opts);
|
|
}
|
|
|
|
for ( var processor of this.processors ) {
|
|
var returned = processor(parsed, opts);
|
|
if ( returned instanceof Root ) parsed = returned;
|
|
}
|
|
|
|
return parsed.toResult(opts);
|
|
}
|
|
|
|
// Return processor function
|
|
normalize(processor) {
|
|
if ( typeof(processor) == 'object' && processor.postcss ) {
|
|
return processor.postcss;
|
|
} else {
|
|
return processor;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Framework for CSS postprocessors
|
|
//
|
|
// var processor = postcss(function (css) {
|
|
// // Change nodes in css
|
|
// });
|
|
// processor.process(css)
|
|
var postcss = function (...processors) {
|
|
return new PostCSS(processors);
|
|
};
|
|
|
|
// Compile CSS to nodes
|
|
postcss.parse = require('./parse');
|
|
|
|
// Nodes shortcuts
|
|
postcss.comment = function (defaults) {
|
|
return new Comment(defaults);
|
|
};
|
|
postcss.atRule = function (defaults) {
|
|
return new AtRule(defaults);
|
|
};
|
|
postcss.decl = function (defaults) {
|
|
return new Declaration(defaults);
|
|
};
|
|
postcss.rule = function (defaults) {
|
|
return new Rule(defaults);
|
|
};
|
|
postcss.root = function (defaults) {
|
|
return new Root(defaults);
|
|
};
|
|
|
|
module.exports = postcss;
|