简体   繁体   English

如何删除(function(){'use strict';} and()); 与grunt-contrib-concat串联时

[英]How to remove (function(){'use strict'; } and ()); when concatenating with grunt-contrib-concat

I can concatenate my files in the proper order and I can add (function(){'use strict'; to the top and }()); 我可以按正确的顺序连接文件,并可以在顶部添加(function(){'use strict';}()); to the bottom of the output file but I don't know how to remove the (function(){'use strict'; and }()); 到输出文件的底部,但我不知道如何删除(function(){'use strict';}()); from the individual files before concatenating. 串联之前从单个文件中删除。

I read through the docs and I tried using the custom process example and I know I need to make some changes to this line src.replace(/(^|\\n)[ \\t]*('use strict'|"use strict");?\\s*/g, '$1'); 我通读了文档,并尝试使用自定义流程示例 ,我知道我需要对此行src.replace(/(^|\\n)[ \\t]*('use strict'|"use strict");?\\s*/g, '$1'); but unfortunately I don't understand that line or how to change it. 但不幸的是,我不了解这条线或如何更改它。

Lastly, I don't know if it even matters to make this change. 最后,我不知道进行更改是否重要。 When I concat and minify my code, leave it as is, and don't add the banner and footer everything works fine. 当我合并并缩小代码时,请保持不变,不要添加横幅和页脚,一切正常。 Is there any benefit to replacing the individual use-stricts with just the one? 用单个使用限制代替单个使用限制有什么好处吗?

From my Gruntfile 从我的Gruntfile

concat: {
    options: {
        banner: "(function(){'use strict';\n",
        footer: '}());',
        process: function(src, filepath) {
            return '// Source: ' + filepath + '\n' +
                src.replace(/(^|\n)[ \t]*('use strict'|"use strict");?\s*/g, '$1');
        }
    },
    nonMin: {
        src: ['src/angular-gmap-gplace.js', 'src/modules/*.js'],
        dest: 'dist/angular-gmap-gplace.js'
    },
    min: {
        src: ['src/**/*.js'],
        dest: '.tmp/concat.js'
    }
}

Is there any benefit to replacing the individual use-stricts with just the one? 用单个使用限制代替单个使用限制有什么好处吗?

No. There are only drawbacks. 不。只有弊端。

Using an IIFE in each file means that each file has it's own scope and, unless you deliberately create a global, can't interfere with the other files. 在每个文件中使用IIFE意味着每个文件都有其自己的作用域,并且除非您有意创建全局文件,否则它不会干扰其他文件。

If you merge them into a single IIFE then that is no longer true, and they share their scopes with each other. 如果将它们合并为一个IIFE,则不再是正确的,它们彼此共享范围。 You risk accidentally overwriting a variable used by one with file an identically named variable in a different file. 您可能会不小心覆盖一个文件所使用的变量,而该文件在另一个文件中的名称相同。

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

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