简体   繁体   English

gulp:如何在 scss 文件之间通过管道传输 css 文件? gulp.src(['sassfile1.scss', 'cssfile.css','sassfile2.scss'])

[英]gulp: How do i pipe a css file in between scss files? gulp.src(['sassfile1.scss', 'cssfile.css' ,'sassfile2.scss'])

this is my current code part in gulpfile.js:这是我当前在 gulpfile.js 中的代码部分:

// Sass 
gulp.task('sass', function () {
  gulp.src(['./node_modules/bootstrap/scss/bootstrap.scss', './node_modules/startbootstrap-full-slider/css/full-slider.css' ,'./sass/**/*.scss'])
    .pipe(sourcemaps.init())
        .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(sourcemaps.write('./'))
    .pipe(concat('style.css'))
    .pipe(gulp.dest('./'));
});

I am trying to add "full-slider.css` file in between, to minify and combine (in that order).我正在尝试在两者之间添加“full-slider.css`文件,以缩小和组合(按该顺序)。

the content of full-slider.css is not added to the final style.css full-slider.css的内容不会添加到最终的style.css

edit:编辑:

Tried using gulp-cssnano : this way:尝试使用gulp-cssnano :这样:

var gulp = require('gulp');
var livereload = require('gulp-livereload')
var uglify = require('gulp-uglifyjs');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var fs = require('fs');
var concat = require('gulp-concat');
var cssnano = require('gulp-cssnano');

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

    gulp.src(['./node_modules/bootstrap/scss/bootstrap.scss', './sass/**/*.scss'])
    .pipe(sourcemaps.init())
        .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(sourcemaps.write('./'))
    .pipe(concat('style.css'))
    .pipe(gulp.dest('./'));

    gulp.src(['style.css', './node_modules/startbootstrap-full-slider/css/full-slider.css'])
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(cssnano())
    .pipe(concat('style.css'))
    .pipe(gulp.dest('./'));

});

gulp sass吞咽 sass

[16:11:45] Using gulpfile /var/www/test.dev/wp-content/themes/olympos/gulpfile.js [16:11:45] Starting 'sass'... [16:11:45] Finished 'sass' after 23 ms [16:11:45] 使用 gulpfile /var/www/test.dev/wp-content/themes/olympos/gulpfile.js [16:11:45] 开始 'sass'... [16:11:45] 23 毫秒后完成“sass”

events.js:141 throw er; events.js:141 抛出 er; // Unhandled 'error' event ^ CssSyntaxError: /var/www/test.dev/wp-content/themes/olympos/bootstrap.scss:1:0: Missed semicolon> 1 | // 未处理的“错误”事件 ^ CssSyntaxError: /var/www/test.dev/wp-content/themes/olympos/bootstrap.scss:1:0: Missed semicolon> 1 | /*! /*! | | ^ 2 | ^ 2 | * Bootstrap v4.0.0-alpha.6 ( https://getbootstrap.com ) 3 | * Bootstrap v4.0.0-alpha.6 ( https://getbootstrap.com ) 3 | * Copyright 2011-2017 The Bootstrap Authors * 版权所有 2011-2017 Bootstrap 作者

when i comment the part:当我评论该部分时:

    gulp.src(['style.css', './node_modules/startbootstrap-full-slider/css/full-slider.css'])
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(cssnano())
    .pipe(concat('style.css'))
    .pipe(gulp.dest('./'));

it compiles smoothly, where is my mistake?它编译顺利,我的错误在哪里?

(theory: might this happen because of the output for the sourcemap in the generated new css file? ) (理论:这可能是因为生成的新 css 文件中 sourcemap 的输出?)

Initially I thought that the best way would be to copy the CSS file to a new location in your project files (using gulp.dest() ), set the file name to have an .scss extension and then run the gulp.sass() pipe again.起初,我认为最好的办法是在CSS文件复制到一个新的位置,在您的项目文件(使用gulp.dest()设置文件名有一个.scss扩展,然后运行gulp.sass()再管。

However, this isn't really using gulp-sass as it's supposed to be used (it's not a file concatenator).然而,这并不是真正使用 gulp-sass,因为它应该被使用(它不是文件连接器)。 A better way would be to do something like this (bear in mind I haven't got time to practically test this so it may not work. Apologies.):更好的方法是做这样的事情(请记住,我没有时间对此进行实际测试,因此它可能无法正常工作。抱歉。):

gulp.task('sass', function () {
  gulp.src(['./node_modules/bootstrap/scss/bootstrap.scss', './sass/**/*.scss'])
  .pipe(sourcemaps.init())
  .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
  .pipe(sourcemaps.write('./'))
  .pipe(concat('style.css'))
  .pipe(gulp.dest('./'));
  gulp.src(['style.css', './node_modules/startbootstrap-full-slider/css/full-slider.css'])
  .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
  .pipe(cssnano())
  .pipe(concat('style.css'))
  .pipe(gulp.dest('./'));
});

That probably isn't perfect, and I also realised as I was writing this that you wanted the fullslider.css file to be included after bootstrap but before your project files.这可能并不完美,而且我在撰写本文时也意识到您希望在引导程序之后但在项目文件之前包含 fullslider.css 文件。 Further this doesn't use sourcemaps for the second sub-task.此外,这不会为第二个子任务使用源映射。 As you can probably tell, I haven't written gulp tasks for a while!您可能已经知道,我已经有一段时间没有编写 gulp 任务了! But if you have time, maybe something to play around with.但如果你有时间,也许可以玩玩。

PS cssnano() refers to gulp-cssnano . PS cssnano()指的是gulp-cssnano

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

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