Possible babel error

I’m wondering if anyone else has come across this - ignoring that it’s a really weird pattern for a moment:

import thing from "./file.js";

class Whatever {
  anotherThing() {
    return thing.doSomething();
  }
  thing() {
    return thing;
  }
}

I get an error thing is undefined because it seems that babel (or perhaps meteor) converts:

  • return thing to return _thing
  • import thing ... to import _thing ...
  • but does NOT convert return thing.doSomething();

If you changed the name of the thing() property, does it work?

have you customized your .babelrc file?

Yeah - it’s for sure because of the “collision” in naming - just that I wouldn’t expect it to cause a collision, and if it did, I’d expect babel to fix all of the arguments.

I’m using two plugins:

{
  "plugins": [
    ["@babel/plugin-proposal-class-properties"],
    ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }]
  ]
}

I know that second one is experimental - this is the first issue I’ve come across though

Worth to report to the babel project

Out of curiosity, I tried to replicate this on a fresh meteor project and didn’t get your issue.

The transpiled code looks like this:

let thing;
module.link("./file.js", {
  default(v) {
    thing = v;
  }
}, 4);
Meteor.startup(() => {
  render( /*#__PURE__*/React.createElement(App, null), document.getElementById('react-target'));
});

class Whatever {
  anotherThing() {
    return thing.doSomething();
  }
  thing() {
    return thing;
  }
}
global.Whatever = Whatever;

I’ve stuck Whatever onto global so I could test it:

I tried with and without your babelrc config

So, um sorry I can’t be of any help ¯\(ツ)

Lol. I’ll try to replicate myself. I thought I knew the cause but it’s possible I simplified it too far. Or maybe its the presence of decorators. Thanks for looking though!

1 Like

@znewsham Are you sure you don’t have a dependency cycle there? It sounds like that. If your error is thing is not defined, then that’s most probably some transpilation problem. However, if the error is Cannot read properties of undefined, then the transpilation succeeded, but the thing was used too early, i.e., before the imports were resolved entirely.

Very sure - the problem was resolved purely by changing the name of the function