1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const { relative, dirname } = require('path');
|
10 |
|
11 |
|
12 | const Sass = require('node-sass');
|
13 | const PostCSS = require('postcss');
|
14 | const postCSSClean = require('postcss-clean');
|
15 | const autoPrefixer = require('autoprefixer');
|
16 |
|
17 |
|
18 | const sassFns = require('./util/sass-fns');
|
19 | const { replaceExt } = require('./utils');
|
20 | const { cssErr } = require('./errors');
|
21 |
|
22 |
|
23 | module.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 |
|
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 |
|
60 | loggerFn.error(err.formatted);
|
61 | return cssErr(err.formatted, '#F2E6EA');
|
62 | });
|
63 | };
|
64 | };
|