How to remove global “use strict” added by babel

后端 未结 16 2172
不思量自难忘°
不思量自难忘° 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:11
    plugins: [
        [
            require("@babel/plugin-transform-modules-commonjs"), 
            {
                strictMode: false
            }
        ],
    ]
    
    0 讨论(0)
  • 2020-11-27 04:11

    Babel 6 + es2015

    We can disabled babel-plugin-transform-es2015-modules-commonjs to require babel-plugin-transform-strict-mode.

    So comment the following code in node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js at 151 line

    //inherits: require("babel-plugin-transform-strict-mode"),
    
    0 讨论(0)
  • 2020-11-27 04:11

    For babel 6 instead of monkey patching the preset and/or forking it and publishing it, you can also just wrap the original plugin and set the strict option to false.

    Something along those lines should do the trick:

    const es2015preset = require('babel-preset-es2015');
    const commonjsPlugin = require('babel-plugin-transform-es2015-modules-commonjs');
    
    es2015preset.plugins.forEach(function(plugin) {
      if (plugin.length && plugin[0] === commonjsPlugin) {
        plugin[1].strict = false;
      }
    });
    
    module.exports = es2015preset;
    
    0 讨论(0)
  • 2020-11-27 04:15

    just change .babelrc solution

    if you don't want to change any npm modules, you can use .babelrc ignore like this

    {
      "presets": ["es2015"],
      "ignore": [
        "./src/js/directive/datePicker.js"
      ]
    }
    

    ignore that file, it works for me!

    the ignored file that can't use 'use strict' is old code, and do not need to use babel to transform it!

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

    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"]
    }
    
    0 讨论(0)
  • 2020-11-27 04:17

    Using plugins or disabling modules and strict mode as suggested in the @rcode's answer didn't work for me.

    But, changing the target from es2015|es6 to es5 in tsconfig.json file as suggested by @andrefarzart in this GitHub answer fixed the issue.

    // tsconfig.json file
    {
      // ...
      "compilerOptions": {
        // ...
        "target": "es5", // instead of "es2015"
    }
    
    0 讨论(0)
提交回复
热议问题