-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
62 lines (52 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const defined = require('defined')
const postcss = require('postcss')
const extend = require('xtend')
const resolve = require('resolve')
const postcssrc = require('postcss-load-config')
module.exports = transform
function transform (filename, source, options, done) {
options = defined(options, {})
const basedir = options.basedir
const plugins = defined(options.plugins, [])
.map(plugin => {
if (typeof plugin === 'string') {
plugin = [plugin]
}
return {
path: resolve.sync(plugin[0], { basedir }),
options: plugin[1]
}
})
.map(plugin => require(plugin.path)(plugin.options))
const ctx = extend({
sourcemap: true,
from: filename,
messages: {
browser: true,
console: false
}
}, options)
delete ctx.plugins
postcssrc(ctx, basedir).then(compile, function () {
return compile({ options: ctx })
}).then(function (result) {
done(null, result)
}, done)
function compile (config) {
return postcss(plugins.concat(config.plugins).filter(Boolean))
.process(source, config.options)
.then(function (result) {
// Collect imported files for watchify
const files = [filename]
result.messages.forEach(function (msg) {
if (msg.type === 'dependency') {
files.push(msg.file)
}
})
return {
css: result.css,
files: files
}
})
}
}