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


Solution 1:

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.

Solution 2:

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.

Solution 3:

There's now a babel plugin that you can add to your config that will remove strict mode: babel-plugin-transform-remove-strict-mode. It's a little ugly in that the "use strict" gets added and then removed, but it makes the config much nicer.

Docs are in the GitHub repo: https://github.com/genify/babel-plugin-transform-remove-strict-mode

Your .babelrc ends up looking like this:

{
  "presets": ["env"],
  "plugins": ["transform-remove-strict-mode"]
}

Solution 4:

I also came accross this rather ridiculous limitation that you cannot disable or overwrite settings from an existing preset, and have resorted to using this preset instead: https://www.npmjs.com/package/babel-preset-es2015-without-strict

Solution 5:

plugins: [
    [
        require("@babel/plugin-transform-modules-commonjs"), 
        {
            strictMode: false
        }
    ],
]