Loophole: problems with basic usage


#1

I’m not an expert so I’ll try to be as clear as possible:

I am trying loophole to fix an‘unsafe-eval’ error on my electron app.

So I install loophole via npm and then add this line to the top of my js sketch (as it says to do in the readme):
{allowUnsafeEval} = require 'loophole'

But this syntax doesn’t look correct to me and yes, indeed it does not compile:
Uncaught SyntaxError: Unexpected token =

So I modify so that it looks like the correct syntax for importing npm modules:
var {allowUnsafeEval} = require ('loophole')

And as expected, now it compiles. (why does the readme have instructions that do not compile?)

Moving on…

So now I add my problematic library using the weird pointer symbol:
var createGeometry = allowUnsafeEval -> require('three-bmfont-text')

And no, the weird pointer symbol does not compile:
Uncaught SyntaxError: Unexpected token >

So clearly I’m missing something obvious here. How do I correctly use loophole to fix my problematic library?

Much thanks in advance


#2

Are you using JavaScript or CoffeeScript? Because thin arrow functions are CS only.

Ah, that’s not a pointer symbol. We don’t think about pointers here in JS, you always use .. But I don’t think it’s pointers here at all.

Yep, that readme is written in CoffeeScript


#3

Ah thanks. I overlooked the coffeescript tag. My electron app is javascript - can I not use loophole for javascript? I just want to solve the unsafe-eval error I am getting, as explained here.


#4

CoffeeScript is just a different syntax. Use normal JS function syntax and it will be fine.


#5

Thanks @Aerijo
Can anyone tell me the proper javascript syntax for enabling loophole on a specific library? Something like this:
var createGeometry = allowUnsafeEval -> require('three-bmfont-text')


#6

You can use a decaffeinator to convert CS to JS. In this case, it shows var createGeometry = allowUnsafeEval -> require('three-bmfont-text') is equivalent to

const createGeometry = allowUnsafeEval(() => require('three-bmfont-text'));

Whether this is actually what you want, I don’t know.


#7

big thanks @Aerijo for the decaffeinator tip. I was able to get it all working finally.