Reputation: 34627
Using Babel 6, I'm trying not to have "use strict"
in my compiled code.
I've found that it's the "transform-es2015-modules-commonjs" plugin (in "es2015" preset) which is what adds it.
In the source-code it seems to inherit "babel-plugin-transform-strict-mode"
, which if I remove it, it still works fine, i.e. it compiles the import "…"
into require(…)
without adding the "use strict".
So why does "transform-es2015-modules-commonjs" force strict mode?
Upvotes: 6
Views: 2293
Reputation: 161457
In the ES6 specification, there are two modes in which a file can be processed:
As a "script" which would generally be everything we are accustomed to in a standard JS environment
ES6 module syntax is not allowed, and for backward-compatibility reasons, content is only treated as strict if it has a prefix directive of "use strict";
.
As a "module"
ES6 module syntax is allowed, and all code is automatically strict mode in all cases.
Because ES6 module syntax is tied up with whether something is a module or a script, and if something is a "module" it is automatically strict, Babel uses the presences of transform-es2015-modules-commonjs
to enable both transformations at the same time.
Even if you were to enable just the module transformation itself and exclude strict mode, all code you write would technically be invalid and as soon as you tried to use your ES6 code in a real ES6 module environment, it would be strict whether you like it or not.
If you do not wish your code to be strict, I would suggest disabling the transform-es2015-modules-commonjs
transform and using CommonJS modules, since they have no such strict-mode requirement.
Upvotes: 8