简体   繁体   English

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

[英]How do I remove some javascript during grunt-usemin compilation

I have code that makes it easy and fast to write/test code, that code does not belong in my production code (mostly it mocks out the server so I only need the grunt server). 我有代码,可以轻松快速地编写/测试代码,代码不属于我的生产代码(大多数情况下它会模拟服务器,所以我只需要grunt服务器)。

Two parts to this, one is how to I remove parts of a script 这两部分,一部分是如何删除部分脚本

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

] ]

and then a section of index.html that needs to go away 然后是一段需要消失的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 -->

So this might be 2 questions. 所以这可能是2个问题。 I don't see anything in the usemin documentation about this so I'm guessing there is some other tool, but I don't know what the name of that tool is. 我在usemin文档中没有看到任何关于此的内容,所以我猜测还有其他工具,但我不知道该工具的名称是什么。

The other possibility is I'm doing it wrong and rather than inject this mocking object, I should be doing it with the grunt server. 另一种可能性是我做错了而不是注入这个模拟对象,我应该用grunt服务器来做。 What is everyone else doing? 其他人在做什么?

Ok, so stumbled on the answer while looking for something else and since no one had yet responded. 好的,所以在寻找其他东西的时候偶然发现了答案,因为还没有人回复。 Here is how I solved it: 这是我解决它的方式:

You get a copy of Grunt Preprocess with 你得到了Grunt Preprocess的副本

npm install --save-dev grunt-preprocess

Then you modify your GruntFile.js like so (this is for an angular project, YMMV) 然后你修改你的GruntFile.js (这是一个角项目,YMMV)

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

add this in your list of subtasks 将此添加到子任务列表中

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

Modify your registered tasks (at the bottom of the file) like thils: 修改您的注册任务(在文件的底部),如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'
]);

Then modify your existing javascript code something like this: 然后修改现有的javascript代码,如下所示:

// @if DEBUG
'server_mocks',  // Won't be included in production builds
// @endif

and your existing html code something like this: 和你现有的HTML代码是这样的:

<!-- @if DEBUG -->
<script src='scripts/mock.js'></script>  <!-- Won't be included in production builds -->
<!-- @endif -->

Take a look at dom munger ( https://github.com/cgross/grunt-dom-munger ) you can give the elements that you want removed a specific attribute or IDs and have it remove them from the html file. 看一下dom munger( https://github.com/cgross/grunt-dom-munger )你可以给你想要删除特定属性的元素,并让它从html文件中删除它们。 But, what I like better is to have it inject, via append or prepend, the unwanted element when I specify a target of dev. 但是,我更喜欢的是当我指定dev的目标时,通过追加或前置注入不需要的元素。 It keeps the original HTML cleaner. 它保持原始HTML更清洁。 I haven't delt with removing portions of javascript though. 我还没有删除部分javascript。 Depending on what else is in your js file that you want changed, you could have it inject a different version of the file for your dev and release build. 根据您想要更改的js文件中的其他内容,您可以为开发和发布版本注入不同版本的文件。

Remove Examples: 删除示例:

  • 'script[data-remove="true"]' - all script elements with the attribute data-remove and a value of true. 'script [data-remove =“true”]' - 所有脚本元素的属性为data-remove,值为true。
  • '#removeMe' - the element with the ID removeMe '#removeMe' - ID为removeMe的元素

Append Examples: 附加示例:

  • { selector:'html',html:'<script src=\\'scripts/mock.js\\'></script> ' } - append the specified html to the html element {selector:'html',html:'<script src = \\'scripts / mock.js \\'> </ script>'} - 将指定的html附加到html元素

here is a solution where you don't need extra tools: 这是一个不需要额外工具的解决方案:

as stated in the documentation you could use a type which is not css/js and this will be removed during build 文档中所述,您可以使用不是css / js的类型,这将在构建期间删除

<!-- build:<type>(alternate search path) <path> -->
... HTML Markup, list of script / link tags.
<!-- endbuild -->
  • type: can be js, css or a custom type with a block replacement function defined type:可以是js,css或定义了块替换函数的自定义类型
  • If another type, the block will be ignored. 如果是另一种类型,则该块将被忽略。 Useful for "development only" blocks that won't appear in your build 对于不会出现在构建中的“仅开发”块有用

so you can just do something like this: 所以你可以这样做:

<!-- build:mockJs -->
<script type='text/javascript'>var Mocks = {};</script>
<script src='scripts/mocks/jobs.js'></script>
<script src='scripts/mock.js'></script>
<!-- endbuild -->

EDIT : for javascript you could use uglifies global definition 编辑 :对于javascript你可以使用uglifies全局定义

http://github.com/gruntjs/grunt-contrib-uglify/blob/master/docs/uglify-examples.md#conditional-compilation http://github.com/gruntjs/grunt-contrib-uglify/blob/master/docs/uglify-examples.md#conditional-compilation

just do an uglify config in you gruntfile: 只需在你的gruntfile中做一个uglify配置:

grunt.initConfig({
   ...
   uglify: { 
      options: { 
         compress: {
            global_defs: {
               "PROD": true
             }
         }
      }
   }
   ...
});

and in js do something like: 并在js做类似的事情:

var modules = ['ngCookies', 'ngResource',
   'ngSanitize',
   'ngRoute',
   'dialogs'
]

if(!PROD) {
  modules.push('server_mocks');
}

angular.module('nglaborcallApp', modules);

you could also add this global JS in your debug block 您还可以在调试块中添加此全局JS

<!-- build:mockJs -->
<script type='text/javascript'>var Mocks = {};</script>
<script src='scripts/mocks/jobs.js'></script>
<script src='scripts/mock.js'></script>
<script type='text/javascript'>
   PROD = false;
</script>
<!-- endbuild -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM