2

Due to certain limitations in babel's operation, I need to write workaround code to make part of my project work correctly when transpiled by it. I would like to make this code conditional, so it only runs if it has been transpiled by babel (as it is unnecessary in a native ES6 environment). Is there any way of doing this?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Periata Breatta
  • 468
  • 5
  • 14
  • 2
    Do you want to use feature-detection (check whether it's a real ES6 environment) or do you want babel to insert some special code on transpilation? Maybe you should post what your workaround is about (and possibly even the code itself) so that we can better help you. – Bergi Oct 30 '16 at 14:23
  • @Bergi I'm trying to work around a bug where the `eval` function can't access symbols that are defined at module scope. – Periata Breatta Oct 30 '16 at 15:22
  • @PeriataBreatta: In general you have to assume that variables will be renamed. If you have to use some kind of eval, use `Function`: `var f = new Function('var1, var2', ''); f(var1, var2)`. This way it doesn't matter what Babel does to `var1` and `var2`. – Felix Kling Oct 30 '16 at 17:08
  • 2
    Why don't use post the ACTUAL problem with `eval()` that you have an then you can solicit answers to the actual problem rather than to the work-around. – jfriend00 Oct 30 '16 at 17:22
  • @jfriend00 - the problem is that I have a number of classes which are imported into the module I am calling `eval` from using the `import { name1, name2, ... } from "source"` syntax. The code that is being executed by `eval` needs to be able to refer to those, but can't because they are (as helpfully commented by Felix above) renamed by Babel, and `eval` isn't aware of the renaming. I haven't posted a question on the problem with `eval` because there is already another question on that, where a Babel developer essentially stated that there is no actual solution possible. – Periata Breatta Oct 31 '16 at 22:23

1 Answers1

4

From Babel plugins docs:

Now, out of the box Babel doesn’t do anything. It basically acts like const babel = code => code; by parsing the code and then generating the same code back out again.

So generally you can't detect if a code has been transpiled by Babel, because without any plugins, it's no-op. However, you might be able to detect if code has been transipled with Babel with some plugins or presets.

Since you're talking about transpiling ES2015 code, I assume that you're using the es2015 preset. This preset for example transpiles ES2015 classes. Since you can easily detect if something is a native ES2015 class, you can check if a code has been transipiled by Babel.

For example, the following code snippet in modern browsers should output false:

const isBabel = !(class {}.toString().indexOf('class ') === 0);
console.log(isBabel);

While this one outputs true (I checked the "Use BabelJS / ES2015" option in code snippet options):

const isBabel = !(class {}.toString().indexOf('class ') === 0);
console.log(isBabel);
Community
  • 1
  • 1
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177