Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove global "use strict" added by babel

I'm using function form of "use strict" and don't want global form which Babel adds after transpilation. The problem is I'm using some libraries that aren't using "use strict" mode and it might throw error after scripts are concatenated

like image 284
ani h Avatar asked Nov 20 '15 07:11

ani h


People also ask

How do I get rid of use strict?

No, you can't disable strict mode per function. Notice how we can define function outside of strict code and then pass it into the function that's strict. You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function.

Does Webpack use strict mode?

Also note that if you use ES6 syntax (like import instead of require), webpack will automatically add "use strict" as all ES6 modules are expected to be strict mode code.


2 Answers

Babel 5

You'd blacklist "useStrict". For instance here's an example in a Gruntfile:

babel: {     options: {         blacklist: ["useStrict"],         // ...     },     // ... } 

Babel 6

Since Babel 6 is fully opt-in for plugins now, instead of blacklisting useStrict, you just don't include the strict-mode plugin. If you're using a preset that includes it, I think you'll have to create your own that includes all the others, but not that one.

like image 41
T.J. Crowder Avatar answered Oct 13 '22 13:10

T.J. Crowder


As it has already been mentioned for Babel 6, it's the transform-es2015-modules-commonjs preset which adds strict mode. In case you want to use the whole es2015 preset without module transformations, put this in your .babelrc file:

{   "presets": [     ["es2015", { "modules": false }]   ] } 

This will disable modules and strict mode, while keeping all other es2015 transformations enabled.

like image 92
rcode Avatar answered Oct 13 '22 13:10

rcode