如何看一个导入其他的gulp文件?

时间:2017-07-03 20:24:19

标签: css gulp

我的sass结构看起来像这样。

    - /base
    -- _fonts.scss
    -- _all.scss
    - /components
    -- _all.scss
    -- _header.scss
    -- _footer.scss
    - main.scss

每个_all.scss导入当前文件夹中的所有文件,然后main.scss导入每个文件夹中的所有_all文件。

我面临的问题是我实际上需要保存main.scss以便gulp生成.css文件。如果我观看所有文件,它将生成许多我不需要的.css文件。

gulpfile.js

'use strict';

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

gulp.task('sass', function () {
  return gulp.src('./src/stylesheets/main.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./public/css/'));
});

gulp.task('sass:watch', function () {
  gulp.watch('./src/stylesheets/main.scss', ['sass']);
});

如何让gulp监视所有文件并在公共文件夹中只生成一个css文件?

1 个答案:

答案 0 :(得分:0)

我是这样做的。

<强> Main.scss

@import "your/relative/path/of/your/_partial-file1";
@import "your/relative/path/of/your/_partial-file2";

/*Other scss stylings if needed*/

在你的gulpfile.js中,添加sass任务

gulp.task('css', ()=>{

 return gulp.src('src/scss/*.scss') //target all the .css files in the specified directory

   .pipe(sass()) //gulp-sass module to convert scss files into css file

   .pipe(minifyCSS()) //method to minify the final css file

   .pipe(gulp.dest('build/css')); //outputs final css file into the specified directory

   console.log('CSS build');

});



gulp.task('default', [ 'css','your', 'other','tasks' ],()=>{

console.log("All done. Watching files...");



 //watch the files in the specified directory.

 //if any changes are made to the files, the specified task will be executed in the watch method

gulp.watch('src/**/*.scss',['css']);

});

在此处查找完整文章:http://todarman.com/gulp-configuration-build-html-css-js/

希望这有帮助。