我试图在我的项目中使用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没有被创建?
谢谢!
Hmm问题是在grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']);
。我忘了调用concat:generated.
所以解是:
grunt.registerTask('deploy', ['useminPrepare', 'concat:generated', 'uglify:generated']);