我试图在我的项目中使用grunt-usemin来缩小JS和CSS文件,但它不能很好地工作。所以我在grunt-usemin文档中创建了一个简单的例子,它也不起作用。
我的结构很简单:
.
├── Gruntfile.js
├── dist
│ └── js
├── index.html
├── js
│ ├── bar.js
│ └── foo.js
└── package.json
在index.html中,我有以下块参考2个JS文件:
<!-- build:js js/optimized.js -->
<script src="/js/foo.js"></script>
<script src="/js/bar.js"></script>
<!-- endbuild -->
我的Gruntfile.js:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
useminPrepare: {
html: 'index.html',
options: {
dest: 'dist'
}
},
usemin: {
html: ['index.html']
},
filerev: {
options: {
algorithm: 'md5',
length: 8
},
js: {
src: 'js/optimized.js',
dest: 'js/'
}
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-filerev');
grunt.loadNpmTasks('grunt-usemin');
// Register tasks
grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']);
// grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated', 'usemin']);
};
当我运行grunt deploy -v
时,我希望将创建js / optimized.js。但是我得到了这个错误:
>> Destination dist/js/optimized.js not written because src files were empty.
>> No files created.
useminPrepare生成的任务如下:
Going through index.html to update the config
Looking for build script HTML comment blocks
Configuration is now:
concat:
{ generated:
{ files:
[ { dest: '.tmp/concat/js/optimized.js',
src: [ 'js/foo.js', 'js/bar.js' ] } ] } }
uglify:
{ generated:
{ files:
[ { dest: 'dist/js/optimized.js',
src: [ '.tmp/concat/js/optimized.js' ] } ] } }
有人可以帮助我,为什么不创建dist / js / optimized.js?
谢谢!
答案 0 :(得分:0)
问题出在grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']);
。我忘了打电话给concat:generated。
所以解决方案是:
grunt.registerTask('deploy', ['useminPrepare', 'concat:generated', 'uglify:generated']);