【问题标题】:Grunt doesn't update main scss fileGrunt 不更新主 scss 文件
【发布时间】:2014-07-22 10:01:42
【问题描述】:

我有一个项目使用带有 grunt-contrib-sass、grunt-contrib-watch 和 grunt-newer 的 GruntJS。

我的主 app.scss 文件使用 @import 指令导入一些 .scss 文件,例如

@import "common/vars.scss";

如果我在主 app.scss 中进行更改,一切正常。问题是当我对正在导入的文件(如 vars.scss)进行更改时,app.scss 没有得到更新。

这是我的监视任务:

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

这就是 sass 任务本身:

sass: {
      dist: {
        options: {
          style: 'compressed',
          noCache: true
        },
        /* looks for every .scss file in the scss folder (checks nested folders too), 
         * compile it and create another file wit the same name in the style folder */
        files: [{
          expand: true,
          cwd: 'scss',
          src: ['**/*.scss'],
          dest: 'style',
          rename: function (dest, src) {
            var folder    = src.substring(0, src.lastIndexOf('/'));
            var filename  = src.substring(src.lastIndexOf('/'), src.length);

            filename  = filename.substring(0, filename.lastIndexOf('.'));

            return dest + '/' + folder + filename + '.css';
          }
        }]
      }
    }

知道如何更改 Gruntfile.js 以涵盖这种情况吗? :) 谢谢。

【问题讨论】:

  • 我不确定这是否可行,但我注意到 watch 的 'files' 成员和 sass 的 files:src 成员都是数组。也许你可以添加一个模式来匹配那里的其他文件?

标签: sass gruntjs grunt-contrib-watch


【解决方案1】:

注意:这是this comment in an issue on the grunt-newer GitHub page 的最小修改副本。


我想出了这个简单的委派任务,它可以解决您希望对 src 属性中指定的更改以外的更改做出反应的任务:

grunt.initConfig( {
    delegate: {
        some_other_TASK: {
            src: [ 'some/src/path/**/*.ext' ],
            dest: 'some/dest/path/'
        }
    }
} );

grunt.registerTask( 'delegate', function() {
    grunt.task.run( this.args.join( ':' ) );
} );

grunt-contrib-sass 的完整示例如下所示:

module.exports = function( grunt ) {
    require( 'load-grunt-tasks' )( grunt );

    grunt.initConfig( {
        delegate: {
            sass: {
                src: [ 'scss/**/*.scss' ],
                dest: 'style'
            }
        },

        sass: {
          dist: {
            options: {
              style: 'compressed',
              noCache: true
            },
            files: [{
              expand: true,
              cwd: 'scss',
              src: ['*.scss'],
              dest: 'style',
              ext: '.css'
            }]
          }
        }
    } );

    grunt.registerTask( 'delegate', function() {
        grunt.task.run( this.args.join( ':' ) );
    } );
};

您只需致电newer:delegate:sass(通过 CLI 或其他任务)。 grunt-newer 检查在根据 delegate 目标中给出的文件(我有 globbing 模式 **/ 包括在内)。如果找到新文件(子文件夹中还有.scss 文件),将调用相应的sass 任务(具有给定目标)。

不过,我不确定是否可以在这里使用单独的 grunt 插件。


编辑:我最近将上述代码(已清理)作为单独的 npm 包 grunt-delegate 发布。

【讨论】:

    【解决方案2】:

    我认为您的手表 tasks 变量是向后的,您可以通过打开终端检查,转到您通常运行 grunt watch 的目录,但尝试运行 newer:sass 如果不起作用,请尝试 sass:newer 如果后来的作品改变了你手表设置中的那一行。

    另外,如果您只有一个 sass 任务,您可以将其更改为:

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

    顺便说一句,您的 sass 似乎具有不必要的复杂重命名功能,如果您只是想将scss/**/*.scss 转换为style/**/*.css,您应该可以使用它(取自 grunt -contrib-sass 自述文件):

    grunt.initConfig({
      sass: {
        dist: {
          files: [{
            expand: true,
            cwd: 'scss',
            src: ['**/*.scss'],
            dest: 'styles',
            ext: '.css'
          }]
        }
      }
    });
    

    如果您有使用多个点的文件,例如:file.src.scssextDot: 'last' 添加到文件数组中

    【讨论】:

    • 这些都不起作用。如果我只运行“sass”而不是“newer:sass”,则生成所有文件,但我明确希望使用较新的插件,因此它只能生成那些需要生成的文件。即使不需要,我也可能每次都强制生成主 .scss 到 .css 文件。我的文件确实使用了多个点,这正是使用重命名功能的原因。我试图删除它并添加了你的建议(ext 和 extDot 选项),但它没有帮助 - file.src.scss 只是变成了 file.css。
    • grunt newer:sass:dist 有效吗? (我从未使用过较新的插件,但它可能需要您具体命名任务)
    • newer:sass 是正确的。 newer 并不意味着那样工作。请参阅更新的覆盖选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多