【问题标题】:Gulp sass different folders into one CSS fileGulp 将不同的文件夹合并到一个 CSS 文件中
【发布时间】:2016-06-27 15:30:11
【问题描述】:

我知道 GULP-SASS 应该是微不足道和简单的,但如果我们可以把事情复杂化,为什么不做对呢?

嗯,我工作的项目似乎有一个“特殊”的结构,我必须习惯它。是这样的:

-static
  -app
    -components
      -component1
        -styles
          -component1.scss
      -component2
        -styles
          -component2.scss
    ...

在与应用程序相同的级别上,我挥动了一个子项目......我们称之为网络:

-static
  -web
    -res
      -static
        -app
           -components
              -component3
                -style
                   -component3.scss   

现在有趣的部分来了:我怎样才能从这个混乱中创建一个单独的 CSS 文件?我试过这个没有任何运气:

// Setting up the sass task
gulp.task('sass',  function (){
// Taking the path from the above object
    var sassApp = gulp.src(paths.styles.filesApp)
    // Sass options - make the output compressed and add the source map
    // Also pull the include path from the paths object
    .pipe(sass({
        outputStyle: 'compact', //compressed
        //sourceComments: 'map',
        includePaths : [paths.styles.srcApp]
    }))
    // If there is an error, don't stop compiling but use the custom displayError function
    .on('error', function(err){
        displayError(err);
    })
    // Pass the compiled sass through the prefixer with defined 
    .pipe(prefix(
        'last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'
    ))
    // Funally put the compiled sass into a css file
    .pipe(gulp.dest(paths.styles.dest));

    var sassWeb = gulp.src(paths.styles.filesWeb)
    // Sass options - make the output compressed and add the source map
    // Also pull the include path from the paths object
    .pipe(sass({
        outputStyle: 'compact',
        //sourceComments: 'map',
        includePaths : [paths.styles.srcWeb]
    }))
    // If there is an error, don't stop compiling but use the custom displayError function
    .on('error', function(err){
        displayError(err);
    })
    // Pass the compiled sass through the prefixer with defined
    .pipe(prefix(
        'last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'
    ))
    // Funally put the compiled sass into a css file
    .pipe(gulp.dest(paths.styles.dest));

    return
        gulpMerge(sassApp, sassWeb)
        .pipe(concat('all-css.css'))
        .pipe(gulp.dest(paths.styles.dest));
});

【问题讨论】:

标签: css sass gulp gulp-sass


【解决方案1】:

这就是我为 CSS 设置 gulp 文件的方式。

我在项目中使用了多个目的地,但这应该有助于为您指明正确的方向。

您还可以使用 SassGlob 将主目录中的所有 scss / sass / css 文件全局化。

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

    return gulp.src('src/sass/styles.scss')
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(sassGlob())
        .pipe(sass({
            compass: false,
            includePaths: [
                './bower_components/susy/sass',
                './bower_components/susy/lib',
                './bower_components/susy/sass/susy',
                './bower_components/breakpoint-sass/stylesheets',
                require('node-bourbon').includePaths
            ],
            outputStyle: 'compressed'
        }).on('error', sass.logError))
        .pipe(prefix())
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('dist/assets/css'))
        .pipe(gulp.dest('assets/css'))
        .pipe(reload({
            stream: true
        }));
});

【讨论】:

  • 感谢德里克的回答……但我的问题不是多个目的地而是多个来源。我想从多个不同的来源(甚至不同的文件夹,彼此相距很远 - 正如我在问题树中提出的那样)获取 scss 文件,并在目标文件夹中制作一个单独的 css 文件
  • 那么你需要做的是使用 includePaths 到有问题的文件夹,然后你可以在你的 style.scss 文件中用@import 指定文件名。
猜你喜欢
  • 1970-01-01
  • 2021-02-28
  • 2016-05-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2021-01-25
相关资源
最近更新 更多