13

I looked at many posts but still cannot get this to work :(

I have .babelrc

{
  "comments": false,
  "presets": [
    ["@babel/env", {
      "targets": {
        "browsers": ["ios 7"]
      }
    }],
    ["minify"]
  ]
}

I want to tell babel to not add "use strict" (anywhere)

How is this done ?

kofifus
  • 17,260
  • 17
  • 99
  • 173

1 Answers1

27

Babel assumes by default that files being transformed are ES modules. Since that is not the case for you, you'll want to tell it that. You can check out the docs for the "sourceType" option, but essentially you want to add:

"sourceType": "script"

in your Babel options.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • Ty for the explanation. It works properly for me. – Jay Cui Jan 08 '19 at 23:59
  • This helped me regarding babel throwing errors when it came across code that wasn't strict-mode compliant. However, now it's adding "use strict" to the top of all my files. Any idea how to stop that? – yts May 15 '19 at 17:07
  • 3
    @yts Consider `'unambiguous'` instead of `'script'`. – loganfsmyth May 15 '19 at 17:37
  • @loganfsmyth Thanks for the suggestion, but that doesn't work for me either. Might have to do with the version of gulp-babel I'm using. I'll have to get back to working on it at a later time. – yts May 16 '19 at 21:31
  • @loganfsmyth Looks like changing the sourceType allowed babel (6) to read in my non strict compliant code, but I had to also add `modules: false` to the "env" presets options to prevent it from appending "use strict" to the top of the files. Basically like this https://stackoverflow.com/a/39225403/1825352, except I'm using "env" instead of "es2015" – yts May 23 '19 at 14:53
  • @yts Yup. See the duplicate flagged on this question. It also goes over using `modules: false`. – loganfsmyth May 23 '19 at 16:24
  • @loganfsmyth What's interesting is that `modules: false` alone did not solve this issue. Babel was still trying to read in the files in strict mode unless I also changed the `sourceType` – yts May 23 '19 at 16:39