google / google/closure-compiler

Closure Compiler renames "self" to "this", breaking some platforms

Open
#2,890 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

I'm compiling, on Advanced, a library that needs to work on the browser but also other contexts like web worker, as an ES6 module, in Node, etc.

During compilation, `self` is transformed into `this`. From:

```javascript
var root = typeof self === 'object' && self.self === self && self ||
typeof global === 'object' && global.global === global && global ||
typeof window === 'object' && window.window === window && window || {};
```

To:

```javascript
var v,aa="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this,
```

Some contexts like Web Workers and ES6 modules do not have `this` defined, so this causes errors when compiled unless I change them back to `self` after compilation.

I cannot reproduce this issue on the closure compiler service. Is the closure compiler doing this merely because `this` is more common, so eliminating `self` allows for more gzipping?

I'm using:

`java -jar ./closure-compiler.jar --js ${closureInput} --js_output_file=${output} ${externs} --compilation_level ADVANCED_OPTIMIZATIONS ${format} --export_local_property_definitions=true --generate_exports --language_out=ECMASCRIPT5 --jscomp_off=deprecatedAnnotations --jscomp_off=uselessCode --output_wrapper "(function(window) { %output% })(self);"`

I know language out is ES5, but I'm trying to get a single compilation that's future compatible.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.