简体   繁体   English

关闭编译ADVANCED_OPTIMIZATIONS抱怨使用此方法

[英]closure compilation ADVANCED_OPTIMIZATIONS complaining about use of this

I have a Gruntfile.js as below with closure compiler config , 我有一个如下的Gruntfile.js关闭编译器config

module.exports = function(grunt) {

grunt.initConfig({

    pkg: grunt.file.readJSON('package.json'),
     'closure-compiler': {
         jstracker: {
              closurePath: '/usr/local/',
              js: [
                'src/js/banner.js',
                'src/js/lib/json.js',
                'src/js/lib/jstz.js',
                'src/js/init.js',
                'src/js/helpers.js',
                'src/js/lib/sha1.js',
                'src/js/lib/murmur.js',
                'src/js/lib/base64.js',
                'src/js/tracker.js',
                'src/js/prayagupd.js',
                'src/js/constructor.js'
               ],
              jsOutputFile: 'dist/<%= pkg.name %>.min.js',
              options: {
                      compilation_level: 'ADVANCED_OPTIIZATIONS',
                      warning_level:"DEFAULT",
                      language_in: 'ECMASCRIPT5_STRICT'
               }
          }
    },

    qunit: {
        files: ['tests/**/*.html']
    },
    jshint: {
        files: ['Gruntfile.js', 'src/**/*.js', 'tests/*.js'],
        options: {
            // options here to override JSHint defaults
            globals: {
                jQuery: true,
                console: true,
                module: true,
                document: true
            }
        }
    },
    watch: {
        files: ['<%= jshint.files %>'],
        tasks: ['jshint', 'qunit']
    }
});

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-closure-compiler');

grunt.registerTask('test', ['jshint', 'closure-compiler', 'qunit']);
grunt.registerTask('default', ['jshint', 'closure-compiler', 'qunit']);

}; };

When I execute grunt to generate minified js, I see minified version at target folder, 当我执行grunt生成缩小的js时,我在target文件夹中看到缩小的版本,

$ ls -lh dist/
total 44K
-rw-rw-r-- 1 prayagupd prayagupd  27K Jul  6 16:34 prayagupd.min.js
-rw-rw-r-- 1 prayagupd prayagupd  514 Jul  6 16:34 prayagupd.min.js.report.txt

But with following 4 warnings all pointing to use of this , that later pops up as error ( Uncaught ReferenceError: JSON2 is not defined ) while using in browser. 但是以下四个警告均指向this使用,后来在浏览器中使用时会弹出错误( Uncaught ReferenceError: JSON2 is not defined )。

src/js/lib/json.js:26: WARNING - dangerous use of the global this object
if (!this.JSON2) {
     ^
src/js/lib/json.js:27: WARNING - dangerous use of the global this object
    this.JSON2 = {};
    ^
src/js/leakers.js:1995: WARNING - dangerous use of the global this object
            this.setContextProperty("leaksId", leaksId);
            ^
src/js/leakers.js:2002: WARNING - dangerous use of the global this object
            this.setContextProperty("userId", userId);
            ^
0 error(s), 4 warning(s)

SIMPLE_OPTIMIZATIONS level works fine though. 虽然SIMPLE_OPTIMIZATIONS级别工作正常。

Offending source codes 违反源代码

//src/js/lib/json.js:26
// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.
if (!this.JSON2) {
    this.JSON2 = {};
}

and, 和,

   //src/js/leakers.js

   /**
    * Set context properties, properties which are used with every event
    */
    setContextProperty: function(name, value) {
        context[name] = value;
    },

    /**
    * Set leaks ID
    */
    setLeakesId: function(leakesId) {
        this.setContextProperty("leakesId", leakesId);
    },

    /**
    * Set user ID
    */
    setUserId: function(userId){
        this.setContextProperty("userId", userId);
    }

Two things I need help to fix; 我需要帮助解决的两件事;

1) ask closure to ignore this , which doesn't seem possible under ADVANCED_OPTIMIZATIONS 1)让闭包忽略this ,这在ADVANCED_OPTIMIZATIONS下似乎不可能

or 要么

2) Find ways to replace this to and get working. 2)想办法来替代this并获得工作。

The compiler is basically warning that you are using "this" in a way that it is unsure about (you may be accidentally be using the global this) and maybe incompatible with advanced compilation. 编译器基本上是在警告您以不确定的方式使用“ this”(您可能会意外地使用全局this),并且可能与高级编译不兼容。 You want to verify that that the "this" value is going to be correct and indicate that to the compiler by using a @this annotation. 您想验证“ this”值是否正确,并使用@this批注向编译器指示。

This warning is documented here: https://developers.google.com/closure/compiler/docs/error-ref 此警告记录在这里: https : //developers.google.com/closure/compiler/docs/error-ref

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

相关问题 是否可以使用闭包编译器ADVANCED_OPTIMIZATIONS与jQuery? - Is it possible to use Closure Compiler ADVANCED_OPTIMIZATIONS with jQuery? 具有Closure Compiler和ADVANCED_OPTIMIZATIONS的模块模式 - Module pattern with Closure Compiler and ADVANCED_OPTIMIZATIONS 如何不编译goog.closure中的某些代码ADVANCED_OPTIMIZATIONS - How to not compile certain code in goog.closure ADVANCED_OPTIMIZATIONS 使用闭包编译器 ADVANCED_OPTIMIZATIONS 的奇怪对象属性行为 - strange object property behavior with closure compiler ADVANCED_OPTIMIZATIONS Google Closure Compiler ADVANCED_OPTIMIZATIONS - 排除所有函数名称 - Google Closure Compiler ADVANCED_OPTIMIZATIONS - Exclude All function names Google Closure编译器的ADVANCED_OPTIMIZATIONS选项 - Google Closure Compiler's ADVANCED_OPTIMIZATIONS option 如何在ADVANCED_OPTIMIZATIONS中的Closure Compiler中导出公共类方法? - How to export public class methods in Closure Compiler in ADVANCED_OPTIMIZATIONS? 是否可以避免使用带有getter和setter的模式,而仍然使用Closure ADVANCED_OPTIMIZATIONS最小化JavaScript? - Can a pattern with getters and setters be avoided and still minify JavaScript with Closure ADVANCED_OPTIMIZATIONS? 具有高级优化和外部功能的Closure编译器 - Closure Compiler with Advanced Optimizations and Externs 闭包编译器,缺少具有高级优化功能的方法? - Closure Compiler, missing method with advanced optimizations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM