-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sass.transformer.js
45 lines (37 loc) · 1.05 KB
/
sass.transformer.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
const {promisify} = require('util');
const nodeSass = require('node-sass');
const render = promisify(nodeSass.render);
const defaultOptions = {
env: 'development',
file: null,
indentWidth: 2,
precision: 5,
indentType: 'space',
linefeed: 'lf',
sourceComments: false,
functions: {},
includePaths: [],
}
async function sassTransformer(content, opt = defaultOptions) {
if (content && typeof content === 'object') {
opt = content;
content = null;
if (!opt.file) {
throw new Error('If no string content is provided, the "file" option must be provided.')
}
}
opt = {...defaultOptions, ...opt};
content = content ?? '';
if (opt.env === 'production') {
opt.outputStyle = 'compressed';
}
return render({
...opt,
data: content,
file: opt.file?.fileAbsolutePath,
indentedSyntax: opt?.file?.ext === '.sass',
...(opt.env === 'production' && {sourceMap: true}),
}).then(res => res.css.toString())
}
module.exports.sassTransformer = sassTransformer;
module.exports.defaultOptions = defaultOptions;