UNPKG

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