0

I forked a repo of an application that is packaged with webpack. I made a few changes to the index.js and now when I build using webpack, one of my variables in the webpack has a _ in front of it in the declaration, but anytime my code calls that variable it does not have a _ in front of it, thus throwing an error at runtime.

Here is the original from my index.js:

let log_stream_name = ctx.data.LOG_STREAM || ctx.data.AUTH0_DOMAIN;

Here is the result in my bundle.js:

var _log_stream_name = ctx.data.LOG_STREAM || ctx.data.AUTH0_DOMAIN;

Here is a snippet where this variable is used in my bundle.js:

lawger.log(log_stream_name, body.message);

It appears as though this _ is added during the transpiling process from ES6 to ES5 that Babel is doing during the webpack. Any idea why it is doing this? This is also the only variable it is doing it to. There are other similar let statements in the same code block in my index.js that are net getting the _ prefix.

Brian
  • 2,702
  • 5
  • 37
  • 71
  • 1
    Can you post (part of) the code in question? There could simply be an error in your code. For example, you cannot reference `let` variables before they are declared/initialized. – Felix Kling Oct 11 '17 at 18:28
  • 1
    Afaik, Babel mangles variables only when there are collisions in other scopes that become the same ES5 scope. Or when there's a bug. Try to make a minimal reproducible example and file an issue. – Bergi Oct 11 '17 at 18:28
  • @Bergi I think I found the issue related to scope. For this one variable, the `let` statement is inside a function, but the use of the variable was further into a deeply nested function. I think Babel saw that as a collision. I simply added the `_` to both the `let` and the usage and the bundle shows the same. – Brian Oct 11 '17 at 18:35
  • @Brian In that case, it's definitely a bug. Please report it to the Babel team. – Bergi Oct 11 '17 at 18:37
  • @Bergi I will work on a simplified example to file as a bug with Babel – Brian Oct 11 '17 at 18:40
  • @FelixKling I will post the reproducing example in this question for demonstration of the issue. – Brian Oct 11 '17 at 18:40
  • @Bergi I actually had the same problem when I added the `_` myself in the declaration and usage. This time the bundle.js had `var _log_stream_name2` while the usage was still `_log_stream_name`. When I changed my code in index.js to us `var` instead of `let` it worked properly. – Brian Oct 11 '17 at 18:52

0 Answers0