即使没有进行任何更改,如何强制Grunt编译Sass?

即使没有进行任何更改,如何强制Grunt编译Sass?,sass,gruntjs,grunt-contrib-watch,Sass,Gruntjs,Grunt Contrib Watch,如果我的sass文件没有更改,我希望grunt在每次执行grunt时都编译sass。有时,观察者无法检测编译后的结果是否与现有CSS文件不同,而强制其编译的唯一方法是编辑一个Sass文件 Grunt文件: /** * @file */ module.exports = function(grunt) { // This is where we configure each task that we'd like to run. grunt.initConfig({ pkg:

如果我的sass文件没有更改,我希望grunt在每次执行grunt时都编译sass。有时,观察者无法检测编译后的结果是否与现有CSS文件不同,而强制其编译的唯一方法是编辑一个Sass文件

Grunt文件:

/**
 * @file
 */
module.exports = function(grunt) {

  // This is where we configure each task that we'd like to run.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
      // This is where we set up all the tasks we'd like grunt to watch for changes.
      scripts: {
        files: ['js/source/{,*/}*.js'],
        tasks: ['uglify'],
        options: {
          spawn: false,
        },
      },
      images: {
        files: ['images/source/{,*/}*.{png,jpg,gif}'],
        tasks: ['imagemin'],
        options: {
          spawn: false,
        }
      },
      vector: {
        files: ['images/source/{,*/}*.svg'],
        tasks: ['svgmin'],
        options: {
          spawn: false,
        }
      },
      css: {
        files: ['sass/{,*/}*.{scss,sass}'],
        tasks: ['sass']
      }
    },
    uglify: {
      // This is for minifying all of our scripts.
      options: {
        sourceMap: true,
        mangle: false
      },
      my_target: {
        files: [{
          expand: true,
          cwd: 'js/source',
          src: '{,*/}*.js',
          dest: 'js/build'
        }]
      }
    },
    imagemin: {
      // This will optimize all of our images for the web.
      dynamic: {
        files: [{
          expand: true,
          cwd: 'images/source/',
          src: ['{,*/}*.{png,jpg,gif}' ],
          dest: 'images/optimized/'
        }]
      }
    },
    svgmin: {
      options: {
        plugins: [{
          removeViewBox: false
        }, {
          removeUselessStrokeAndFill: false
        }]
      },
      dist: {
        files: [{
          expand: true,
          cwd: 'images/source/',
          src: ['{,*/}*.svg' ],
          dest: 'images/optimized/'
        }]
      }
    },
    sass: {
      // This will compile all of our sass files
      // Additional configuration options can be found at https://github.com/sindresorhus/grunt-sass
      options: {
        sourceMap: true,
        // This controls the compiled css and can be changed to nested, compact or compressed.
        outputStyle: 'expanded',
        precision: 5
      },
      dist: {
        files: {
          'css/base/base.css': 'sass/base/base.sass',
          'css/components/components.css': 'sass/components/components.sass',
          'css/components/tabs.css': 'sass/components/tabs.sass',
          'css/components/messages.css': 'sass/components/messages.sass',
          'css/layout/layout.css': 'sass/layout/layout.sass',
          'css/theme/theme.css': 'sass/theme/theme.sass',
          'css/theme/print.css': 'sass/theme/print.sass'
        }
      }
    },
    browserSync: {
      dev: {
        bsFiles: {
          src : [
            'css/**/*.css',
            'templates/{,*/}*.twig',
            'images/optimized/{,*/}*.{png,jpg,gif,svg}',
            'js/build/{,*/}*.js',
            '*.theme'
          ]
        },
        options: {
          watchTask: true,
          // Change this to "true" if you'd like the css to be injected rather than a browser refresh. In order for this to work with Drupal you will need to install https://drupal.org/project/link_css keep in mind though that this should not be run on a production site.
          injectChanges: false
        }
      }
    },
  });
  // This is where we tell Grunt we plan to use this plug-in.
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-imagemin');
  grunt.loadNpmTasks('grunt-svgmin');
  grunt.loadNpmTasks('grunt-sass');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-browser-sync');
  // Now that we've loaded the package.json and the node_modules we set the base path
  // for the actual execution of the tasks
  // grunt.file.setBase('/')
  // This is where we tell Grunt what to do when we type "grunt" into the terminal.
  // Note: if you'd like to run and of the tasks individually you can do so by typing 'grunt mytaskname' alternatively
  // you can type 'grunt watch' to automatically track your files for changes.
  grunt.registerTask('default', ['browserSync','watch']);
};
只要将
sass
注册为要在键入
grunt
时运行的任务即可

如果执行此操作后,sass文件仍无法提供所需的结果,则需要重新访问
sass
任务,并确保将文件传送到所需的位置

更酷的选择:

newer
:运行
grunt
时,仅当新CSS和旧CSS之间存在差异时,才希望sass编译为CSS。那样的话,试试看。追加
newer:taskyouwanttorun:option
将起作用


watch:sass
:您希望sass在
watch
期间编译,除了更改一个sass文件之外,还需要基于其他内容。简单,只需设置一个监视任务,查找您想要修改的任何文件,image/javascript/html/which,并将任务设置为
sass

“when sass!=css”在本文中的含义是什么?如果所有文件都没有更改,为什么还需要watcher来编译更改呢?为了弄清楚,当我在当前状态下运行grunt时,它不会做任何事情,尽管sass充满了代码,而css文件没有。当我更新sass文件时,它会进行更改,但我想告诉grunt,sass文件的当前状态与css文件不同,所以请施展您的魔法,而不编辑sass文件。简单明了的事实是。。。你说得对。由于我碰巧先回答了错误的两个问题,而不是实际上被问到的正确的、简单得多的问题,所以它也击中了两个要点。谢谢你的回答:我的母语不是英语,我不想把别人弄糊涂。
grunt.registerTask('default', [
    'browserSync',
    'sass',
    'watch',
]);