How to remove global “use strict” added by babel

后端 未结 16 2174
不思量自难忘°
不思量自难忘° 2020-11-27 03:46

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 \"us

相关标签:
16条回答
  • 2020-11-27 04:18

    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

    0 讨论(0)
  • 2020-11-27 04:22

    You can tell babel that your code is a script with:

    sourceType: "script"
    

    This will not add use strict. See sourceType option docs

    Source: https://github.com/babel/babel/issues/7910#issuecomment-388517631

    0 讨论(0)
  • 2020-11-27 04:25

    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.

    0 讨论(0)
  • 2020-11-27 04:25

    Since babel 6 you can install firstly babel-cli (if you want to use Babel from the CLI ) or babel-core (to use the Node API). This package does not include modules.

    npm install --global babel-cli
    # or
    npm install --save-dev babel-core
    

    Then install modules that you need. So do not install module for 'strict mode' in your case.

    npm install --save-dev babel-plugin-transform-es2015-arrow-functions
    

    And add installed modules in .babelrc file like this:

    {
      "plugins": ["transform-es2015-arrow-functions"]
    }
    

    See details here: https://babeljs.io/blog/2015/10/31/setting-up-babel-6

    0 讨论(0)
  • 2020-11-27 04:30

    if you are using https://babeljs.io/repl (v7.8.6 as of this writing), you can remove "use strict"; by selecting Source Type -> Module.

    0 讨论(0)
  • 2020-11-27 04:31

    This is not grammatically correct, but will basically work for both Babel 5 and 6 without having to install a module that removes another module.

    code.replace(/^"use strict";$/, '')
    
    0 讨论(0)
提交回复
热议问题