UNPKG

1.88 kBJavaScriptView Raw
1// _
2// | |
3// _ __ ___ _ __ __| | ___ _ __ ______ ___ ___ ___ ___
4// | '__/ _ \ '_ \ / _` |/ _ \ '__|______/ __|/ __/ __/ __|
5// | | | __/ | | | (_| | __/ | \__ \ (__\__ \__ \
6// |_| \___|_| |_|\__,_|\___|_| |___/\___|___/___/
7
8// node
9const { relative, dirname } = require('path');
10
11// npm
12const Sass = require('node-sass');
13const PostCSS = require('postcss');
14const postCSSClean = require('postcss-clean');
15const autoPrefixer = require('autoprefixer');
16
17// local
18const sassFns = require('./util/sass-fns');
19const { replaceExt } = require('./utils');
20const { cssErr } = require('./errors');
21
22// export
23module.exports = function({srcDir, loggerFn, options}) {
24
25 const { isDev, isBuild, browsers } = options;
26
27 return function(srcFile) {
28 const file = relative(process.cwd(), srcFile);
29 const outFile = replaceExt(file, '.css');
30 return new Promise((resolve, reject) => {
31 Sass.render(
32 {
33 file, outFile,
34 includePaths: ['node_modules', '.', dirname(file)],
35 outputStyle: 'nested',
36 sourceMap: isDev,
37 functions: sassFns(srcFile)
38 },
39 (err, data) => {
40 if (err) reject(err);
41 resolve(data);
42 }
43 );
44 }).then(data => {
45 // http://api.postcss.org/global.html#processOptions
46 return PostCSS([autoPrefixer({ browsers })]
47 .concat(isDev?[]:[postCSSClean({})]))
48 .process(data.css, {
49 from: file,
50 to: outFile,
51 map: data.map ? { inline: true, prev: data.map.toString() } : false
52 });
53 })
54
55
56 .then(data => data.css)
57 .catch(err => {
58 if (isBuild) throw Error(err);
59 // TODO: verify that this is the best thing to return
60 loggerFn.error(err.formatted);
61 return cssErr(err.formatted, '#F2E6EA');
62 });
63 };
64};