google / google/closure-compiler

ADVANCED compilation mode incorrectly removes non-dead code

Open
#4,143 7 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 am using webpack with closure compiler, I ran into an issue that some function calls from certain modules were removed by the compiler.

This is the minimal example for reproducing the issue, it's basically the webpack code for requiring a module

```
function __webpack_require__(moduleId) {
var module = {
id: moduleId,
exports: {},
};
__webpack_modules__[moduleId].call(
module.exports,
module,
module.exports,
__webpack_require__
);
return module.exports;
}

__webpack_require__.o = (obj, prop) => {
return Object.prototype.hasOwnProperty.call(obj, prop);
};

__webpack_require__.d = (exports, definition) => {
var key;
for (key in definition)
if (
__webpack_require__.o(definition, key) &&
!__webpack_require__.o(exports, key)
)
Object.defineProperty(exports, key, {
enumerable: true,
get: definition[key],
});
};

var __webpack_modules__ = {
123: (
__unused_webpack_module,
__webpack_exports__,
__webpack_require__
) => {
function myFunction() {
console.log('hello');
// do more stuff
}
__webpack_require__.d(__webpack_exports__, {
myFunction: () => {
return myFunction;
},
});
},
}

var myModule = __webpack_require__(123);

window['test'] = () => {
myModule.myFunction();
};
```
Calling window.test() should print "hello" in the console. However the ADVANCED compilation mode turns window['test'] into an empty function
```
function c(b) {
var a = { id: b, exports: {} };
e[b].call(a.exports, a, a.exports, c);
return a.exports;
}
c.g = (b, a) => Object.prototype.hasOwnProperty.call(b, a);
c.d = (b, a) => {
for (var d in a)
c.g(a, d) &&
!c.g(b, d) &&
Object.defineProperty(b, d, { enumerable: !0, get: a[d] });
};
var e = {
123: (b, a, d) => {
function f() {
console.log("hello");
}
d.d(a, { h: () => f });
},
};
c(123);
window.test = () => {};
```

Please help take a look, thanks!

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.