css.js
1019 Bytes
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
const fs = require('fs');
const path = require('path');
const isDev = process.env.NODE_ENV === 'dev' || !process.env.NODE_ENV;
const logger = global.yoho.logger;
const css = {};
module.exports = (file) => {
if (css[file] && !isDev) {
return Promise.resolve(css[file]);
}
const cssPath = path.join(__dirname, isDev ? 'css' : 'min-css', file);
const cssfile = fs.readFileSync(cssPath).toString();
logger.info(`cssnano process ${cssPath}`);
if (isDev) {
return require('cssnano').process(cssfile, {
safe: true,
autoprefixer: {
add: true,
browsers: ['> 1%', 'android >=4', 'ios >=8']
}
}).then(function(result) {
if (result && result.css) {
css[file] = result.css;
return css[file];
}
return '';
}).catch(err => {
logger.error(err);
return '';
});
}
return Promise.resolve(cssfile);
};