仅编译需要的sass文件whit grunt

时间:2017-01-19 14:50:04

标签: javascript sass gruntjs watch

使用contrib-sass和contrib-watching我想只编译所需的文件,因为我在生产服务器上有很多scss文件。

我写了这个Gruntfile,但是没有用。看着作品但没有编译任何东西。

module.exports = function(grunt) {

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    watch: {
     sass: {
         files: [
             'sites/**/scss/*.scss'
         ],
         tasks: ['sass'],
         options: {
             spawn: false
         }
     }
  },

  sass: {
    dist:{
      options: {
          style: 'compressed'
      },
      files: objDestSource
    }
  }

});

// On watch events configure jshint:all to only run on changed file
grunt.event.on('watch', function(action, filepath) {
  var filepath_scss = filepath;
  var filepath_css = filepath.replace("scss", "css");
  filepath_css = filepath_css.replace("scss", "css");
  filepath= '\''+filepath_css+'\' : \''+filepath_scss+'\'';
  console.log(filepath);
  grunt.config('sass.dist.files', filepath);
});

grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default',['watch']);

};

1 个答案:

答案 0 :(得分:0)

我解决了。 这是我的脚本代码:

module.exports = function(grunt){

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    watch: {
     sass: {
         files: [
             'pippo/*/scss/*.scss',
             'pippo/test/*/scss/*.scss'
         ],
         tasks: ['sass'],
         options: {
             spawn: false
         }
     }
  },

  sass: {
    dist:{
      options: {
          style: 'compressed'
      },
      files: {
        'pippo/test/statics/css/ie.css' : 'pippo/test/statics/scss/ie.scss'
      }
    }
  }

});

grunt.event.on('watch', function(action, filepath) {
  var filepath_scss = filepath;
  var filepath_css = filepath.replace("scss", "css");
  filepath_css = filepath_css.replace("scss", "css");
  var finalpath = {};
  finalpath[filepath_css] = filepath_scss;
  grunt.config('sass.dist.files', finalpath );
});

grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default',['watch']);

};