[英]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
级别工作正常。
//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);
}
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.