-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Description
Bug Report
Current Behavior
This is a very confusing bug, and I want to say first that I’m not sure where the bug is (in Babel or elsewhere), so I am hoping for feedback in order to refine this bug report and possibly move it. (Is it more likely a Babel bug, a Webpack bug, a minifier bug?)
The problem: Our project uses https://www.npmjs.com/package/clipboard, which is published to NPM in transpiled form. As such, it contains the _typeof helper:
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};After transformation via Webpack (4.17.1), Babel (6.26.3) uglifyjs-webpack-plugin or (currently) terser-webpack-plugin (1.0.2, terser 3.8.2), it seems that the typeof Symbol.iterator part has been transformed a second time, and resulted in a name collision. The minified code, reformatted for legibility:
r = "function" == typeof Symbol && "symbol" === r(Symbol.iterator) ?
function (e) {
return void 0 === e ? "undefined" : r(e)
} :
function (e) {
return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ?
"symbol" :
void 0 === e ? "undefined" : r(e)
}Since both typeof instances were minified, presumably via the _typeof helper, to the same name (here: r), the expression fails to evaluate because the r in r(Symbol.iterator) isn’t initialized yet.
Input Code
Unfortunately, I have been unable to reproduce this in a minimal repro case. Importing the same library (clipboard) in a minimal sample and minifying it with the same options did not reproduce the behaviour.
Babel Configuration (.babelrc, package.json, cli command)
{
"presets": ["babel-preset-env", {
"browsers": [
">5%",
"IE >= 11",
"last 2 Chrome versions",
"last 2 ChromeAndroid versions",
"last 2 Edge versions",
"last 2 Firefox versions",
"last 2 iOS versions"
]
}],
"plugins": [
"babel-plugin-lodash",
"babel-plugin-syntax-dynamic-import",
"babel-plugin-transform-decorators-legacy",
"babel-plugin-transform-class-properties",
"babel-plugin-transform-flow-comments",
"babel-plugin-transform-object-rest-spread",
"babel-plugin-transform-react-jsx"
]
}Environment
- Babel version(s): 6.26.3
- Node/npm version: Node 8.11.1/npm 6.4.0
- OS: Ubuntu 18.04
- Monorepo: No
- How you are using Babel:
babel-loader/Webpack