css.js 1019 Bytes
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);
};