如何在grunt-usemin编译期间删除一些javascript

boa*_*der 13 javascript gruntjs grunt-usemin

我有代码,可以轻松快速地编写/测试代码,代码不属于我的生产代码(大多数情况下它会模拟服务器,所以我只需要grunt服务器).

这两部分,一部分是如何删除部分脚本

angular.module('nglaborcallApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'server_mocks',  // Don't want this line in the production build
'dialogs'
Run Code Online (Sandbox Code Playgroud)

]

然后是一段需要消失的index.html

<!-- build:js({.tmp,app}) scripts/mocks/mocks.js -->
<script type='text/javascript'>var Mocks = {};</script>
<script src='scripts/mocks/jobs.js'></script>
<script src='scripts/mock.js'></script>
<!-- endbuild -->
Run Code Online (Sandbox Code Playgroud)

所以这可能是2个问题.我在usemin文档中没有看到任何关于此的内容,所以我猜测还有其他工具,但我不知道该工具的名称是什么.

另一种可能性是我做错了而不是注入这个模拟对象,我应该用grunt服务器来做.其他人在做什么?

boa*_*der 26

好的,所以在寻找其他东西的时候偶然发现了答案,因为还没有人回复.这是我解决它的方式:

你得到的副本咕噜预处理

npm install --save-dev grunt-preprocess
Run Code Online (Sandbox Code Playgroud)

然后你修改你的GruntFile.js喜欢(这是一个角项目,YMMV)

module.exports = function (grunt) {
    grunt.loadNpmTasks('grunt-preprocess');      <-- Add this line near the top of the file
Run Code Online (Sandbox Code Playgroud)

在您的子任务列表中添加此项

    preprocess : {
        options: {
            inline: true,
            context : {
                DEBUG: false
            }
        },
        html : {
            src : [
                '<%= yeoman.dist %>/index.html', 
                '<%= yeoman.dist %>/views/*.html'
            ]
        },
        js : {
            src: '.tmp/concat/scripts/*.js'
        }
    },
Run Code Online (Sandbox Code Playgroud)

修改您的注册任务(在文件的底部),如thils:

grunt.registerTask('build', [
    'clean:dist',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'preprocess:js',  // Remove DEBUG code from production builds
    'preprocess:html',  // Remove DEBUG code from production builds
    'ngmin',
    'copy:dist',
    'cdnify',
    'cssmin',
    'uglify',
    'rev',
    'usemin'
]);
Run Code Online (Sandbox Code Playgroud)

然后修改现有的javascript代码,如下所示:

// @if DEBUG
'server_mocks',  // Won't be included in production builds
// @endif
Run Code Online (Sandbox Code Playgroud)

和你现有的HTML代码是这样的:

<!-- @if DEBUG -->
<script src='scripts/mock.js'></script>  <!-- Won't be included in production builds -->
<!-- @endif -->
Run Code Online (Sandbox Code Playgroud)