Louis authored on25/08/2018 23:51:28
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,51 @@
1
+var path = require('path');
2
+var through = require('through');
3
+var postcss = require('postcss');
4
+var autoprefixer = require('autoprefixer');
5
+var requireFromString = require('require-from-string');
6
+
7
+/**
8
+ * Stringifies the content
9
+ * @param   {string}    content
10
+ * @returns {string}
11
+ */
12
+function stringify (content) {
13
+  return 'module.exports = ' + JSON.stringify(content) + ';\n';
14
+}
15
+
16
+module.exports = function (file, options) {
17
+
18
+  /**
19
+   * The function Browserify will use to transform the input.
20
+   * @param   {string} file
21
+   * @returns {stream}
22
+   */
23
+  function browserifyTransform (file) {
24
+    var extensions = ['.css', '.sass', '.scss', '.less'];
25
+    var chunks = [];
26
+
27
+    if (extensions.indexOf(path.extname(file)) === -1) {
28
+      return through();
29
+    }
30
+
31
+    var write = function (buffer) {
32
+      chunks.push(buffer);
33
+    };
34
+
35
+    var end = function () {
36
+      var contents = requireFromString(Buffer.concat(chunks).toString('utf8'));
37
+      contents = postcss([autoprefixer(options)]).process(contents).css;
38
+      this.queue(stringify(contents));
39
+      this.queue(null);
40
+    };
41
+
42
+    return through(write, end);
43
+  }
44
+
45
+  if (typeof file !== 'string') {
46
+    options = file;
47
+    return browserifyTransform;
48
+  } else {
49
+    return browserifyTransform(file);
50
+  }
51
+};