Grunt更改index.html参考

时间:2013-10-03 20:52:13

标签: build reference gruntjs

使用grunt时,可以更改html文件中的引用。

例如,作为构建过程的一部分,我正在将文件名从style.css更改为style.min.css。

我想要做的是在我的index.html文件中更改对样式表的引用以使用缩小版本。

2 个答案:

答案 0 :(得分:3)

是的,请看grunt-usemin。自述文件非常详尽。 :)

https://github.com/yeoman/grunt-usemin

答案 1 :(得分:1)

另一个避免在html标记中定义块注释的可能解决方案是安装名为grunt-text-replace的插件

通过npm安装插件:

$ npm install grunt-text-replace --save-dev

然后将以下内容添加到Gruntfile

<强> Gruntfile.js:

grunt.initConfig({

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

    /* UPDATES CSS SRC REFERENCED IN YOUR THE HTML FILE */
    replace: {
        cssLink: {
            src: ['./src/index.html'], //<--- The path to your html file
            overwrite: true,
            replacements: [{
                // Subsitute src="css/ below with the path to your CSS file.
                from: 'src="css/style.css',
                // Subsitute src="css/ above with the path to your minified CSS file.
                to: 'src="css/style.min.css'
            }]
        }
    }

});

grunt.loadNpmTasks('grunt-text-replace');

grunt.registerTask('default', [
    'replace:cssLink'
]);

};

相关问题