2

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?

Doug
  • 14,387
  • 17
  • 74
  • 104
  • Try to put it in a function and call it (instead of letting Bebel's strict scope to run it) – Shmuel H. Jan 30 '17 at 20:44
  • Can you provide an example of how to do that? – Doug Jan 30 '17 at 20:51
  • I'm no JS programmer, however: `Function ('return 1+1')()`. `Function`'s constructor doesn't inherit strictness (see [here](https://stackoverflow.com/questions/6020178/can-i-disable-ecmascript-strict-mode-for-specific-functions#6039163)). – Shmuel H. Jan 30 '17 at 20:56
  • Anyway, just to make it a little less "hackish", it would be better to create another `CreateNoStrictFunction(code) { return Function(code); }` function. – Shmuel H. Jan 30 '17 at 20:59
  • Wrapping the code in a function seems to have no effect since Babel is parsing the JS, – Doug Jan 30 '17 at 21:05
  • Oh, OK sorry. However, you can pass the `strictMode` flag as an option: `var output = Babel.transform(code, { strictMode: false, presets: [['es2015', {"loose": true}]] }).code;` (see [here](https://babeljs.io/docs/plugins/transform-strict-mode/)) – Shmuel H. Jan 30 '17 at 21:16
  • This gives this error: `Uncaught ReferenceError: [BABEL] unknown: Unknown option: base.strictMode. Check out http://babeljs.io/docs/usage/options/ for more information about options. A common cause of this error is the presence of a configuration options object without the corresponding preset name. Example: Invalid: `{ presets: [{option: value}] }` Valid: `{ presets: [['presetName', {option: value}]] }` – Doug Jan 30 '17 at 21:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134413/discussion-between-shmuel-h-and-doug). – Shmuel H. Jan 30 '17 at 21:31

2 Answers2

3

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

var output = Babel.transform(code, 
{ 
   presets: ['es2015'], 
   parserOpts: { strictMode: false } 
});
Doug
  • 14,387
  • 17
  • 74
  • 104
1

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' });
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251