咕噜咕噜的斯莫舍什么都不做

Grunt Smoosher doesn't do anything

本文关键字:什么      更新时间:2023-09-26

我是Grunt的新手。在这个Gruntfile中使用smoosher,我正在尝试在我的HTML中插入所有CSS和Javascript资源:

module.exports = function ( grunt ) {
    grunt.initConfig({
        smoosher: {
            files: {
                'page1-dist.html': 'page1.html'
            }
        }
    });
    grunt.loadNpmTasks( 'grunt-html-smoosher' );
    grunt.registerTask( 'default', [ 'smoosher' ] );
};

但它什么也没做。它不会生成任何page1-dist.html文件。这是我在运行grunt时得到的唯一输出:

Running "smoosher:files" (smoosher) task
Done, without errors.

知道出了什么问题吗?

问题是grunt-html-smoosher就是Grunt中所谓的multitask。基本上,这意味着您需要将设置包装在目标中,即使您只指定一组选项。查看了此插件的源代码后,它在底部记录了一个文件已创建,因此它只是配置不正确(因为您没有获得预期的输出)。因此,您的 Gruntfile 应如下所示:

module.exports = function ( grunt ) {
    grunt.initConfig({
        smoosher: {
            dist: {
                files: {
                    'page1-dist.html': 'page1.html'
                }
            }
        }
    });
    grunt.loadNpmTasks( 'grunt-html-smoosher' );
    grunt.registerTask( 'default', [ 'smoosher' ] );
};

如果您愿意,可以用其他内容替换dist

许多 Grunt 任务都遵循这种模式,如果您在特定插件中根据您想要影响的文件有不同的选项,这很有用,但如果插件的行为方式不符合您的预期,您应该记住这一点。始终确保阅读 Grunt 插件的文档,稍后您会感谢自己。:-)