【发布时间】:2015-04-08 16:03:06
【问题描述】:
更新
我已经上传了我在这里遇到的问题的示例:
https://github.com/ianjamieson/grunt-usemin-issue
运行 grunt build 时,您会看到它生成了 dist 文件夹并在正确的 revved 文件中,但是我无法使用正确的文件路径更新 page.html!
我将问题的其余部分留在这里供参考,但是,您可以忽略它并仅使用 GitHub 存储库作为示例。
我正在尝试将 filerev 模块与 usemin 一起使用,但我认为可能存在一些问题,因为我也在使用 load-grunt-config。
usemin 上的文档说我应该只添加一个 revmap 并将值设置为:
<%= grunt.filerev.summary %>
但是,这返回的是未定义的。
GruntFile.js
module.exports = function(grunt) {
var config = {
buildTasks: [
'newer:copy', // copies the src directory to dist (htdocs)
'requirejs', // do an r.js build to concat modular dependencies
'fetchpages', // if there are any remote resources, fetch them here
'concat:head', // concats js in the head
'concat:foot', // concats js in the head
'uglify:head', // compresses js in the head
'uglify:foot', // compresses js in the foot
'cssmin', // minifies and concats css
'filerev', // changes the file name to include md5 hash and force cache refresh
'usemin', // runs through html and inputs minified js and css
'newer:htmlclean', // removes whitespace from html files where required
'newer:imagemin', // minify newer images
'clean:afterBuild' // deletes files that are not required for build
]
};
require('time-grunt')(grunt);
require('load-grunt-config')(grunt, {
jitGrunt: {
staticMappings: {
fetchpages: 'grunt-fetch-pages'
}
}
});
grunt.registerTask('build', config.buildTasks);
};
usemin.js
module.exports = function (grunt, options) {
return {
revmap: '<%= grunt.filerev.summary %>',
html: [
'path/to/page'
]
};
};
filerev.js
module.exports = function(grunt, options) {
return {
options: {
algorithm: 'md5',
length: 10
},
js: {
src: [
'path/to/js.js'
]
},
};
};
我收到了错误:
Running "usemin:revmap" (usemin) task
Verifying property usemin.revmap exists in config...OK
Files: [no src] -> revmap
Options: type="revmap"
Replaced 0 references to assets
【问题讨论】:
-
我认为您缺少
useminPrepare任务。以下是从他们的文档中复制的In a typical usemin setup you launch useminPrepare first, then call every optimization step you want through their generated subtask and call usemin in the end. -
嗨@Vishwanath,我尝试添加 useminPrepare 任务,但没有成功
标签: gruntjs grunt-usemin