我们第一次在我们的项目中使用grunt并且我花了相当多的时间在google和stackoverflow但是找不到它的解决方案,目前我已经缩小了JS和CSS文件但是在浏览器上我想添加版本到缩小了JS和CSS文件,它们是production.js
和production.css
因此,只要资产(js和css)文件被修改,就可以使用grunt或节点在浏览器缓存中将文件添加到文件中
Concat.js
module.exports = function(grunt) {
grunt.config.set('concat', {
dist: {
src: ['assets/styles/style.css','assets/styles/bootstrap.css', 'assets/styles/font-awesome.css','assets/styles/jquery-ui.css'],
dest: 'assets/concat/production.css'
},
js: {
src: ['assets/js/src/jquery-2.1.3.js',
assets/js/src/jquery-ui.js',
'assets/js/src/bootstrap.js',
'assets/js/src/bootstrap-switch.js',
'assets/js/src/moment.js',
'assets/js/src/angular.js',
'assets/js/jquery.gritter.min.js',
'assets/js/dependencies/elements.js',
'assets/js/dependencies/layout.js',
'assets/js/dependencies/init.js',
'assets/js/dependencies/js_importer.js',
'assets/js/dependencies/sib_common.js',
'assets/js/angular-sanitize.min.js',
'assets/js/angular/home.js'
],
dest: 'assets/concat/production.js'
}
});
Index.ejs
<html lang="en">
<script src="/concat/production.js"></script>
<head>
<link href="/concat/production.css" rel="stylesheet" />
</head>
</html>
修改
使用 filerev 实施,但未创建版本
module.exports = function(grunt) {
grunt.config.set('filerev',{
filerev: {
options: {
algorithm: 'md5',
length: 8
},
files: {
src: ['assets/concat/production.js', 'assets/concat/production.css']
}
}
});
grunt.loadNpmTasks('grunt-filerev');
};
default.js
module.exports = function (grunt) {
grunt.registerTask('default',
['compileAssets', 'linkAssets','filerev' , 'watch']);
};