UNPKG

2.09 kBJavaScriptView Raw
1'use strict';
2
3var $postcss = require('postcss');
4
5var processors = {
6 responsiveType: require('postcss-responsive-type'),
7 shorthandPosition: require('postcss-position'),
8 quantityQueries: require('postcss-quantity-queries'),
9 alias: require('postcss-alias'),
10 inputPseudo: require('postcss-input-style'),
11 clearFix: require('postcss-clearfix'),
12 fontPath: require('postcss-fontpath'),
13 hexRGBA: require('postcss-hexrgba'),
14 easings: require('postcss-easings'),
15 fallbacks: [
16 require('postcss-color-rgba-fallback'),
17 require('postcss-opacity'),
18 require('postcss-pseudoelements'),
19 require('postcss-vmin')
20 ],
21 autoprefixer: require('autoprefixer')
22};
23
24// Error reporting
25var reporter = require('postcss-reporter');
26
27// Default options
28var defaults = {
29 autoprefixer: false,
30 fallbacks: false
31};
32
33// Build PostCSS plugin
34var rucksack = $postcss.plugin('rucksack', function(options) {
35
36 var postcss = $postcss(),
37 plugins = [];
38
39 options = options || {};
40
41 Object.keys(defaults).forEach(function(opt){
42 if (!options.hasOwnProperty(opt)){
43 options[opt] = defaults[opt];
44 }
45 });
46
47 Object.keys(processors).forEach(function(feature){
48 var processor = processors[feature];
49
50 if (options[feature] !== false) {
51
52 if (processor instanceof Array) {
53 plugins = plugins.concat(processor);
54 } else {
55 plugins.push(processor);
56 }
57
58 }
59 });
60
61 plugins.push(reporter);
62
63 // Build PostCSS bundle
64 plugins.forEach(function(plugin){
65 postcss.use(plugin);
66 });
67
68 return postcss;
69
70});
71
72// Export new PostCSS processor, bundled with plugins
73module.exports = rucksack;
74
75module.exports.process = function(css, options) {
76 options = options || {};
77 options.map = options.map || (options.sourcemap ? true : null);
78
79 var result = $postcss([rucksack(options)]).process(css, options);
80
81 // return a css string if inline/no sourcemap.
82 if (options.map === null || options.map === true || options.map && options.map.inline) {
83 return result.css;
84 }
85
86 // otherwise return an object of css & map
87 return result;
88
89};