babel / babel/minify

Unminified variable name used unexpectedly

Open
#866 6 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
4.4k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

`minify` unless you turn `mangle` off replaces the original variable names with one-letter names. But under some curcumstances it forgets to use the minified variable name and uses the unmodified variable name instead which leads to a `ReferenceError` since it's not defined.

**To Reproduce**

See configuration below. This does not occur in REPL.

* Source code
```js
String.prototype.toBlob = function() {
try {
let arr = this.split(',')
, mime = arr[0].match(/:(.*?);/)[1]
, bstr = atob(arr[1])
, n = bstr.length
, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}
catch (e) {
console.error(e)
return false
}
}
```

**Actual Output**

```js
String.prototype.toBlob = function() {
try {
for (var a = this.split(','), b = a[0].match(/:(.*?);/)[1], c = atob(a[1]), d = c.length, e = new Uint8Array(d); d--;)
e[d] = c.charCodeAt(d);
return new Blob([u8arr], { type: mime })
}
catch (a) {
return console.error(a), !1 }
}
```

This obviously produces a `ReferenceError: u8arr is not defined`.

**Expected Output**

Here's an actual ouput produced long time ago by `"babel-cli": "6.18.0"`, `"babel-preset-latest": "6.16.0" ` and `"babel-preset-babili": "0.0.9"`

```js
String.prototype.toBlob = function() {
try {
for (var a = this.split(','), b = a[0].match(/:(.*?);/)[1], d = atob(a[1]), g = d.length, h = new Uint8Array(g); g--;)
h[g] = d.charCodeAt(g);
return new Blob([h], {
type: b
})
} catch (a) {
return console.error(a), !1
}
};
```

**Configuration**

* package.json (excerpt):
```json
{
"scripts": {
"build": "cross-env BABEL_ENV=production babel src --out-dir build --source-maps"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-minify": "^0.4.3",
"cross-env": "^5.1.6"
}
}
```

* .babelrc:
```json
{
"presets": [["env", {"modules": false, "comments": false}]],
"env": {
"production": {
"presets": ["minify"]
}
}
}
```

To build, run `npm run build`.

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.