minify icon indicating copy to clipboard operation
minify copied to clipboard

Cannot read property 'add' of undefined

Open mblout opened this issue 5 years ago • 23 comments
trafficstars

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' }

mblout avatar Jan 17 '20 21:01 mblout

I have the same error with this code:

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

  foo(lib = defaultLib) {
    this.lib = lib;
  }

anthony-quiros avatar Jan 20 '20 11:01 anthony-quiros

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

mjakobis avatar Jan 21 '20 14:01 mjakobis

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

arantes555 avatar Feb 05 '20 14:02 arantes555

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

arantes555 avatar Feb 05 '20 16:02 arantes555

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)

somewhatabstract avatar Feb 17 '20 23:02 somewhatabstract

Same issue here.

SalvatorePreviti avatar Feb 20 '20 11:02 SalvatorePreviti

same issue here :(

mnosuk avatar Feb 26 '20 10:02 mnosuk

Same issue here to build webtorrent

DiegoRBaquero avatar Apr 01 '20 21:04 DiegoRBaquero

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.

kpierro914 avatar May 19 '20 14:05 kpierro914

Same issue

monokee avatar Jun 16 '20 10:06 monokee

I have noticed a weird behaviour :

  • I have @babel/[email protected], and I do not hit this issue
  • If I update @babel/core to any superior version, I start hitting the issue
  • If I then rollback @babel/core to 7.5.5, I still hit the issue.

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

arantes555 avatar Jun 16 '20 12:06 arantes555

Same issue here.

DeividVeloso avatar Aug 25 '20 21:08 DeividVeloso

same issue :(

alfokaha-taima avatar Nov 10 '20 14:11 alfokaha-taima

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 (<anonymous>)
        at run (/node_modules/@babel/core/lib/transformation/index.js:35:12)
        at run.next (<anonymous>)
        at Function.transform (/node_modules/@babel/core/lib/transform.js:27:41)
        at transform.next (<anonymous>)
        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)
}

meduzen avatar Nov 14 '20 21:11 meduzen

I'm still stuck on @babel/[email protected] because of this. Any way we can help fix this ?

arantes555 avatar Jan 28 '21 11:01 arantes555

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.

s100 avatar Feb 11 '21 14:02 s100

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.

fwextensions avatar Mar 29 '21 00:03 fwextensions

@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.

arantes555 avatar Mar 29 '21 08:03 arantes555

I got same error.

ruymaster avatar Apr 18 '21 09:04 ruymaster

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

dhlolo avatar Jul 25 '22 07:07 dhlolo

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

TiagoSilvaPereira avatar Jan 17 '23 20:01 TiagoSilvaPereira

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

ochodek avatar Mar 10 '23 09:03 ochodek

@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.

jamonholmgren avatar Dec 17 '23 01:12 jamonholmgren