我目前正在使用usemin任务执行concat和uglify任务。但是,我没有从concat / uglify任务获得任何输出。没有看到任何错误或警告。如何获得结果输出 dist / app / myscript.js 脚本?
Gruntfile.js如下:
module.exports = function(grunt){
var paths = {
app: 'app',
dist: 'dist'
};
grunt.initConfig({
paths: paths,
clean: {
dist: {
src: ['<%= paths.dist %>']
}
},
copy: {
dist: {
files: [
{expand: true, cwd: '.', src: ['**'], dest: '<%= paths.dist %>/'}
]
}
},
useminPrepare: {
html: '<%= paths.dist %>/<%= paths.app %>/index.html'
},
usemin: {
html: '<%= paths.dist %>/<%= paths.app %>/index.html'
}
});
grunt.loadNpmTasks('grunt-usemin');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('default', ['clean','copy']);
grunt.registerTask('use', ['useminPrepare','usemin'])
}
HTML Build代码段:
<!-- build:js myscript.js -->
<script type="text/javascript" src='router.js'></script>
<script type="text/javascript" src='bootstrap.js'></script>
<!-- endbuild -->
控制台输出:
Running "useminPrepare:html" (useminPrepare) task
Going through dist/app/index.html to update the config
Looking for build script HTML comment blocks
Found a block:
<!-- build:js myscript.js -->
<!-- Globals -->
<script type="text/javascript" src='router.js'></script>
<script type="text/javascript" src='bootstrap.js'></script>
<!-- endbuild -->
Updating config with the following assets:
- dist/app/router.js
- dist/app/bootstrap.js
Configuration is now:
cssmin:
{}
concat:
{ 'dist/app/myscript.js':
[ 'dist/app/router.js',
'dist/app/bootstrap.js' ] }
uglify:
{ 'dist/app/myscript.js': 'dist/app/myscript.js' }
requirejs:
{}
Running "usemin:html" (usemin) task
Processing as HTML - dist/app/index.html
Update the HTML to reference our concat/min/revved script files
Update the HTML with the new css filenames
Update the HTML with the new img filenames
Update the HTML with data-main tags
Update the HTML with the data tags
Update the HTML with background imgs, case there is some inline style
Update the HTML with anchors images
Update the HTML with reference in input
Done, without errors.
答案 0 :(得分:6)
事实证明问题在于registerTask()。您需要显式添加concat / uglify。所以看起来应该是这样的:grunt.registerTask('use',['useminPrepare','usemin','concat','uglify'])
答案 1 :(得分:2)
技术上grunt-usemin
还不够成熟,如果失败就会给你错误,这是我迄今为止的观察结果。
它总是会显示Processing as HTML, Update the HTML .......
,即使它在某个地方失败了。
让我们回到解决方案,我认为你的HTML Build代码段中存在问题,你没有在myscript.js之前提到应该有app的正确路径 - <!-- build:js app/myscript.js -->
让我知道它是否仍未解决问题。