2

我了解了cssnano 优化列表:它包括autoprefixer(不是默认的)cssnano-util-raw-cache,等等。

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

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 优化列表)设置所需的优化。

例如。我想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))

        // ...

});
4

1 回答 1

4
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));
于 2018-03-25T22:08:15.357 回答