babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

Cannot read property 'add' of undefined #974

Open mblout opened 4 years ago

mblout commented 4 years ago

seeing " Cannot read property 'add' of undefined" with the latest

sample code, junk.js

const y = () => {};
function x(foo = y) {}

command: npx babel junk.js --presets=minify

package.json

{
  "name": "babelbug",
  "version": "1.0.0",
  "description": "",
  "devDependencies": {
    "@babel/core": "^7.7.0",
    "@babel/preset-env": "^7.7.0",
    "@babel/cli": "^7.7.0",
    "babel-preset-minify": "~0.5.0"
  }
}

stack

{ TypeError: C:\babelbug\junk.js: Cannot read property 'add' of undefined
    at ScopeTracker.addReference (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\scope-tracker.js:47:34)
    at ReferencedIdentifier (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:196:26)
    at newFn (C:\babelbug\node_modules\@babel\traverse\lib\visitors.js:220:17)
    at bfsTraverse (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\bfs-traverse.js:32:43)
    at Mangler.collect (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:229:7)
    at Mangler.run (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:54:12)
    at PluginPass.exit (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:558:19)
    at newFn (C:\babelbug\node_modules\@babel\traverse\lib\visitors.js:179:21)
    at NodePath._call (C:\babelbug\node_modules\@babel\traverse\lib\path\context.js:55:20)
    at NodePath.call (C:\babelbug\node_modules\@babel\traverse\lib\path\context.js:42:17) code: 'BABEL_TRANSFORM_ERROR' }
anthony-quiros commented 4 years ago

I have the same error with this code:

  const defaultLib = require('./default-lib');

  foo(lib = defaultLib) {
    this.lib = lib;
  }
mjakobis commented 4 years ago

The issue seems to be with default functional parameters set to a constant.

arantes555 commented 4 years ago

Seeing the same problem here... setting mangle: false in the options seems to work-around the problem.

arantes555 commented 4 years ago

Also, seems there is a similar old issue : https://github.com/babel/minify/issues/556

somewhatabstract commented 4 years ago

I see the same issue when using rollup-babel-minify. Dependabot tried to update to latest babel and couldn't due to this issue.

TypeError: unknown: Cannot read property 'add' of undefined
    at ScopeTracker.addReference (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34)
    at ReferencedIdentifier (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:196:26)
    at newFn (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/visitors.js:220:17)
    at bfsTraverse (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:32:43)
    at Mangler.collect (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:229:7)
    at Mangler.run (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:54:12)
    at PluginPass.exit (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:558:19)
    at newFn (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/visitors.js:179:21)
    at NodePath._call (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/path/context.js:42:17)
SalvatorePreviti commented 4 years ago

Same issue here.

mnosuk commented 4 years ago

same issue here :(

DiegoRBaquero commented 4 years ago

Same issue here to build webtorrent

kpierro914 commented 4 years ago

I'm having the same issue when I'm assigning a default value to a parameter. As soon as I remove the default value, the issue is gone.

monokee commented 4 years ago

Same issue

arantes555 commented 4 years ago

I have noticed a weird behaviour :

I think this means the issue is caused by one of @babel/core's dependencies. Not sure how to pinpoint which one :/

DeividVeloso commented 4 years ago

Same issue here.

alfokaha-taima commented 3 years ago

same issue :(

meduzen commented 3 years ago

The issue seems to be with default functional parameters set to a constant.

Same for me on both babel-preset-minify 0.4.3 and 0.5.1.

This doesn’t compile:

import { doc } from '../utils/Document';

const DEFAULT_DEBOUNCE_DELAY = 100

let resizeTimer = null

export default function debounceResize(callback, delay = DEFAULT_DEBOUNCE_DELAY) {
  clearTimeout(resizeTimer)
  doc.classList.add('resizing')

  resizeTimer = setTimeout(() => {
    doc.classList.remove('resizing')
    callback()
  }, delay)
}
Stack trace: ``` Module build failed (from ./node_modules/babel-loader/lib/index.js): TypeError: /resources/js/events/resize.js: Cannot read property 'add' of undefined at ScopeTracker.addReference (/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34) at ReferencedIdentifier (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:196:26) at newFn (/node_modules/@babel/traverse/lib/visitors.js:216:17) at bfsTraverse (/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:32:43) at Mangler.collect (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:229:7) at Mangler.run (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:54:12) at PluginPass.exit (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:526:19) at newFn (/node_modules/@babel/traverse/lib/visitors.js:175:21) at NodePath._call (/node_modules/@babel/traverse/lib/path/context.js:55:20) at NodePath.call (/node_modules/@babel/traverse/lib/path/context.js:42:17) at NodePath.visit (/node_modules/@babel/traverse/lib/path/context.js:101:8) at TraversalContext.visitQueue (/node_modules/@babel/traverse/lib/context.js:112:16) at TraversalContext.visitSingle (/node_modules/@babel/traverse/lib/context.js:84:19) at TraversalContext.visit (/node_modules/@babel/traverse/lib/context.js:140:19) at Function.traverse.node (/node_modules/@babel/traverse/lib/index.js:82:17) at traverse (/node_modules/@babel/traverse/lib/index.js:64:12) at transformFile (/node_modules/@babel/core/lib/transformation/index.js:107:29) at transformFile.next () at run (/node_modules/@babel/core/lib/transformation/index.js:35:12) at run.next () at Function.transform (/node_modules/@babel/core/lib/transform.js:27:41) at transform.next () at step (/node_modules/gensync/index.js:254:32) at /node_modules/gensync/index.js:266:13 at async.call.result.err.err (/node_modules/gensync/index.js:216:11) @ ./resources/js/events/index.js 1:89-126 1:217-231 @ ./resources/js/app.js ```

This compiles properly:

import { doc } from '../utils/Document';

let resizeTimer = null

export default function debounceResize(callback, delay = 100) {
  clearTimeout(resizeTimer)
  doc.classList.add('resizing')

  resizeTimer = setTimeout(() => {
    doc.classList.remove('resizing')
    callback()
  }, delay)
}
arantes555 commented 3 years ago

I'm still stuck on @babel/core@7.5.5 because of this. Any way we can help fix this ?

s100 commented 3 years ago

I was able to work around this problem by using @babel/plugin-transform-parameters to transpile away the default parameters syntax prior to minification. I would still be interested to see the bug fixed though.

fwextensions commented 3 years ago

I was seeing the same thing as @arantes555, where upgrading to the latest package started causing the issue, but rolling back to the previously working version still produced the bug, even after wiping and reinstalling node_modules. I eventually gave up and switched to terser, which has been working fine.

arantes555 commented 3 years ago

@fwextensions I ended up switching to terser too...

For those who, for some reason, cannot switch, the trick in this case is to wipe node_modules, and roll-back both your package.json and pacakge-lock.json, then re-installing.

ruymaster commented 3 years ago

I got same error.

dhlolo commented 2 years ago

Any update? Seem there is an PR checked but not merged.

TiagoSilvaPereira commented 1 year ago

I was able to work around this problem by using @babel/plugin-transform-parameters to transpile away the default parameters syntax prior to minification. I would still be interested to see the bug fixed though.

Confirming it works with @babel/plugin-transform-parameters Thank you very much @s100

ochodek commented 1 year ago

Any update? I found that it started to fail after a change in @babel/types@7.8.0 - https://github.com/babel/babel/pull/10917 Hopefuly https://github.com/babel/minify/pull/1024 will be merged soon.

jamonholmgren commented 9 months ago

@ochodek I used Yarn's built-in yarn patch feature to apply #1024's changes (if you're not using Yarn 3+, you can use patch-package instead). It fixed the issue for me.