2
votes

Using Babel-Standalone, I am trying to disable use strict so that I may use the deprecated with statement per the es2015 preset reference.

var code = "with (p) {  // do something }";
var output = Babel.transform(code, { presets: [['es2015', {"loose": true}]] }).code;

this gives me this error:

babel.js:17955 Uncaught SyntaxError: unknown: 'with' in strict mode (1:5)

How do i disable strict mode using Babel Standalone?

2
Try to put it in a function and call it (instead of letting Bebel's strict scope to run it) - Shmuel H.
Can you provide an example of how to do that? - Doug
I'm no JS programmer, however: Function ('return 1+1')(). Function's constructor doesn't inherit strictness (see here). - Shmuel H.
Anyway, just to make it a little less "hackish", it would be better to create another CreateNoStrictFunction(code) { return Function(code); } function. - Shmuel H.
Wrapping the code in a function seems to have no effect since Babel is parsing the JS, - Doug

2 Answers

3
votes

The answer is the parserOpts property which corresponds to options.js in Babylon

var output = Babel.transform(code, 
{ 
   presets: ['es2015'], 
   parserOpts: { strictMode: false } 
});
1
votes

By default Babel parses files as ES6 modules. You'll need to tell it not to do that, with something like

var output = Babel.transform(code, { sourceType: 'script' });