forked from arpadHegedus/postcss-node-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (47 loc) · 1.58 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
/**
* POSTCSS-NODE-SASS
* A PostCSS plugin to parse styles with node-sass
*/
let postcss = require('postcss'),
sass = require('node-sass');
module.exports = opt => ({
postcssPlugin: 'postcss-node-sass',
Once (root, { result }) {
let map = typeof result.opts.map === 'object' ? result.opts.map : {}
let css = root.toResult(Object.assign(result.opts, {
map: Object.assign({
annotation: false,
inline: false,
sourcesContent: true
}, map)
}));
opt = Object.assign({
indentWidth: 4,
omitSourceMapUrl: true,
outputStyle: 'expanded',
sourceMap: true,
sourceMapContents: true
}, opt, {
data: css.css,
file: result.opts.from,
outFile: result.opts.to
});
let includedFiles
return new Promise((resolve, reject) => sass.render(
opt,
(err, res) => err ? reject(err) : resolve(res)
)).then(res => {
includedFiles = res.stats.includedFiles.filter((item, pos, array) => array.indexOf(item) === pos)
return postcss.parse(res.css.toString(), {
from: result.opts.from,
map: {
prev: res.map ? JSON.parse(res.map.toString()) : ''
}
})
}).then(res => {
result.root = res;
result.messages = includedFiles.map(file => ({ type: 'dependency', parent: result.opts.from, file }))
});
}
});
module.exports.postcss = true;