Grunt useminPrepare 和 usemin 不能处理多个任务和文件(concat 失败)

Grunt useminPrepare and usemin not working with multiple tasks and files (concat fails)

我想在我的项目中有两个目标。一个用于管理员,一个用于用户页面。出于性能原因,我不希望将管理插件包含在用户页面中。但是我在尝试 运行 构建时遇到此错误:

Running "useminPrepare:admin" (useminPrepare) task
Configuration changed for concat, uglify, cssmin

Running "useminPrepare:user" (useminPrepare) task
Configuration changed for concat, uglify, cssmin

No "concat" targets found.
Warning: Task "concat" failed. Use --force to continue.

我在 GruntFile.js 中找不到 concat 任务,看看有什么问题。我的 GruntFile.js usemin 任务如下:

    useminPrepare: {
        admin: {
            html: 'app/admin-index.html',
            options: {
                dest: 'dist'
            }
        },
        user: {
            html: 'app/index.html',
            options: {
                dest: 'dist'
            }
        },
    },
    usemin: {
        admin: {
            html: 'dist/admin-index.html'
        },
        user: {
            html: 'dist/index.html'
        }
    }

grunt.registerTask('build', [
    'clean:dist',
    'less',
    'useminPrepare:admin',
    'useminPrepare:user',
    'concat',
    'copy:dist',
    'cssmin',
    'uglify',
    'filerev',
    'usemin:admin',
    'usemin:user',
    'htmlmin'
]);

并且 html 同行正在关注 js 文件。首先是管理员-index.html:

<!-- build:js(.) scripts/vendor.js -->

<!-- jQuery and Bootstrap -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- MetsiMenu -->
<script src="bower_components/metisMenu/dist/metisMenu.min.js"></script>

<!-- Peace JS -->
<script src="bower_components/pace/pace.min.js"></script>

<!-- Angular scripts-->
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>

<!-- Angular Dependiences -->
<!--                      -->
<!--                      -->
<!--                      -->

<!-- Datatables -->
<script src="bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script src="bower_components/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
<script src="bower_components/jszip/dist/jszip.min.js"></script>
<script src="bower_components/pdfmake/build/pdfmake.min.js"></script>
<script src="bower_components/pdfmake/build/vfs_fonts.js"></script>
<script src="bower_components/datatables.net-buttons/js/buttons.html5.min.js"></script>
<script src="bower_components/datatables.net-buttons/js/buttons.print.min.js"></script>
<script src="bower_components/datatables.net-buttons/js/buttons.colVis.min.js"></script>

<!-- Datepicker -->
<script src="bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js"></script>
<script src="bower_components/bootstrap-datepicker/dist/locales/bootstrap-datepicker.tr.min.js"></script>
<script src="bower_components/datepair.js/dist/datepair.js"></script>

<!-- Timepicker -->
<script src="bower_components/jt.timepicker/jquery.timepicker.js"></script>

<!-- endbuild -->

<!-- build:js({.tmp,app}) scripts/scripts.js -->
<!-- Custom and plugin javascript -->
<script src="scripts/inspinia.js"></script>

<!-- Anglar App Script -->
<script src="scripts/app.js"></script>
<script src="scripts/config.js"></script>
<script src="scripts/directives.js"></script>
<script src="scripts/controllers.js"></script>
<!-- endbuild -->

现在 index.html 文件:

<!-- build:js(.) scripts/uivendor.js -->

<!-- jQuery and Bootstrap -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- Datepicker -->
<script src="bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js"></script>
<script src="bower_components/bootstrap-datepicker/dist/locales/bootstrap-datepicker.tr.min.js"></script>

<!-- endbuild -->

也许您在 Gruntfile.js 的 initConfig 设置中没有上面的 concat 参数,但在 "BUILD" 命令中设置了它。 Grunt 不知道连接字符串与什么有关,所以它会抛出一个错误

尝试像下面那样删除 'concat'

grunt.registerTask('build', [
   'clean:dist',
   'less',
   'useminPrepare:admin',
   'useminPrepare:user',
   'copy:dist',
   'cssmin',
   'uglify',
   'filerev',
   'usemin:admin',
   'usemin:user',
   'htmlmin'
]);

或在下面的配置设置示例中添加一个连接键

  concat: {
    options: {
      separator: ';',
    },
    dist: {
      src: ['src/file1.js', 'src/file2.js', 'src/file3.js'],
      dest: 'dist/concat-file.js',
    },
   },

合并这两个任务解决了我的问题。尽管像 Vijay 建议的那样显式声明 concat、cssmin、uglify 等任务也是一种选择,但这太麻烦了。我将 GruntFile.js 更改如下:

    useminPrepare: {
        html: ['app/admin-index.html', 'app/index.html'],
        options: {
            dest: 'dist'
        }
    },
    usemin: {
        html: ['dist/admin-index.html', 'dist/index.html'],
    }

grunt.registerTask('build', [
    'clean:dist',
    'less',
    'useminPrepare',
    'concat',
    'copy:dist',
    'cssmin',
    'uglify',
    'filerev',
    'usemin',
    'htmlmin'
]);