gulp-postcss:条件选项(优化)

时间:2017-11-19 06:55:14

标签: gulp postcss cssnano

我了解了list of cssnano optimizations:它包括autoprefixer(非默认),cssnano-util-raw-cache等。

接下来我查看gulp-postcss documentation:它有以下示例:

var postcss = require('gulp-postcss');
var gulp = require('gulp');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');

gulp.task('css', function () {
    var plugins = [
        autoprefixer({browsers: ['last 1 version']}),
        cssnano()
    ];
    return gulp.src('./src/*.css')
        .pipe(postcss(plugins))
        .pipe(gulp.dest('./dest'));
});

好吧,虽然有人说autoprefixercssnano优化之一,但在上面的示例中,它是单独定义的。但是,我无法从上面的示例中了解如何从第一个链接(cssnano优化列表)设置所需的优化。

电子。 G。我想在开发和生产版本中使用postcss-calc,但是我只需要在生成版本中使用postcss-normalize-whitespace。我该如何填写以下代码?

const   gulp = require('gulp'),
        gulpIf = require('gulp-if'),
        sass = require('gulp-sass'),
        postcss = require('gulp-postcss');

const isDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV === 'develpment';

gulp.task('styles', function(){

    // first the optimizations for both Development and production
    let plugins = [
        autoprefixer({ browsers: ['>= 1%', 'last 5 major versions', 'ie >= 6']}),
        // ...
    ];

    if (isDevelopment) {
        plugins.push(/* plugins for development build */);
    }
    else {
        plugins.push(/* plugins for production build */);   
    }

    return gulp.src(HPath.sassSourceFilesSelection)
        .pipe(gulpIf(isDevelopment, sourcemaps.init()))
        .pipe(sass())
        .pipe(postcss(plugins))

        // ...

});

1 个答案:

答案 0 :(得分:2)

let PostCssPlugins = [
    // non-conditional
    require('cssnano-util-raw-cache')(), 
    // conditional
    !options.isDevelopment ? require('postcss-normalize-whitespace')() : false
].filter(Boolean);

return gulp.src(sassFilesSelection)
    .pipe(plugins.sass())
    .pipe(plugins.postcss(PostCssPlugins))
    .pipe(gulp.dest(outputPath));