简体   繁体   English

使用Gulp缩小CSS

[英]Minify css using Gulp

I want only "app.scss" in minified version, right now I am getting all three SCSS file converted into ".min" files. 我只需要缩小版本的“ app.scss”,现在我将所有三个SCSS文件都转换为“ .min”文件。 Please help 请帮忙

var gulp = require('gulp');
var sass = require('gulp-sass');
var rename = require('gulp-rename')

gulp.task('sass', function(){
    return gulp.src(['scss/style.scss', 'scss/variable.scss', 'scss/app.scss'])
    .pipe(sass())
    .pipe(gulp.dest('css'))
    .pipe(sass({outputStyle:'compressed'}))
    .pipe(rename({suffix:'.min'}))
    .pipe(gulp.dest('css'))
});

You're currently piping all three files to the same sass pipe with the same compressed setting. 您当前正在使用相同的compressed设置将所有三个文件通过管道传输到同一sass管道。 There are various options to fix this, including: 有多种解决方案,包括:

  • use gulp-if to selectively do compressed an non-compressed calls to sass 使用gulp-if选择性地对sass进行非压缩调用
  • create different streams and use merge-stream to return them as one merged result from your task 创建不同的流并使用merge-stream作为任务的合并结果返回它们

What is happening is you are running all your commands on a single source blob. 发生的事情是您在单个源Blob上运行所有命令。 This causes all operations to perform on the same set of files. 这将导致所有操作都在同一组文件上执行。 What you need is the output of one command into another. 您需要的是将一个命令输出到另一个命令。 This is also the correct way to do it, by writing an individual task for each operation. 通过为每个操作编写一个单独的task ,这也是正确的方法。

Use a sequence plugin to perform the tasks one after another and use another gulp.src to get the combined file. 使用序列插件一个接一个地执行任务,并使用另一个gulp.src获取组合文件。 Something like this: 像这样:

gulp.task('sass', function(){
    // Compile Sass
    return gulp.src(['scss/style.scss', 'scss/variable.scss', 'scss/app.scss'])
    ... bla bla ...

});

gulp.task('minify', function(){
    // Minify
    return gulp.src(['app.scss'])
    ... bla bla ...
});

gulp.task('all', function(cb){
    gulpSequence(['sass', 'minify'], cb);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM