简体   繁体   English

如何删除 babel 添加的全局“use strict”

[英]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.我正在使用“使用严格”的函数形式,并且不希望 Babel 在编译后添加的全局形式。 The problem is I'm using some libraries that aren't using "use strict" mode and it might throw error after scripts are concatenated问题是我正在使用一些不使用“使用严格”模式的库,并且在连接脚本后可能会抛出错误

As it has already been mentioned for Babel 6, it's the transform-es2015-modules-commonjs preset which adds strict mode.正如在 Babel 6 中已经提到的那样,它是transform-es2015-modules-commonjs预设,它添加了严格模式。 In case you want to use the whole es2015 preset without module transformations, put this in your .babelrc file:如果你想在没有模块转换的情况下使用整个es2015预设,请将其放在你的.babelrc文件中:

{
  "presets": [
    ["es2015", { "modules": false }]
  ]
}

This will disable modules and strict mode, while keeping all other es2015 transformations enabled.这将禁用模块和严格模式,同时保持所有其他 es2015 转换处于启用状态。

Babel 5通天塔 5

You'd blacklist "useStrict" ."useStrict"列入黑名单。 For instance here's an example in a Gruntfile:例如,这是 Gruntfile 中的一个示例:

babel: {
    options: {
        blacklist: ["useStrict"],
        // ...
    },
    // ...
}

Babel 6通天塔 6

Since Babel 6 is fully opt-in for plugins now , instead of blacklisting useStrict , you just don't include the strict-mode plugin .由于 Babel 6 现在完全选择加入插件,而不是将useStrict列入黑名单,你只是不包括strict-mode插件 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.如果您使用包含它的预设,我认为您必须创建自己的包含所有其他的预设,但不是那个。

There's now a babel plugin that you can add to your config that will remove strict mode: babel-plugin-transform-remove-strict-mode .现在有一个 babel 插件,你可以添加到你的配置中来移除严格模式: 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.这有点难看, "use strict"被添加然后删除,但它使配置更好。

Docs are in the GitHub repo: https://github.com/genify/babel-plugin-transform-remove-strict-mode文档在 GitHub 存储库中: https ://github.com/genify/babel-plugin-transform-remove-strict-mode

Your .babelrc ends up looking like this:你的 .babelrc 最终看起来像这样:

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

我也遇到了这个相当荒谬的限制,您不能禁用或覆盖现有预设中的设置,而是使用此预设: https ://www.npmjs.com/package/babel-preset-es2015-without-strict

You can tell babel that your code is a script with:你可以告诉 babel 你的代码是一个脚本:

sourceType: "script"

in your babel config file.在你的 babel 配置文件中。 This will not add use strict .这不会添加use strict See sourceType option docs请参阅sourceType 选项文档

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

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

Babel 6 + es2015通天塔 6 + es2015

We can disabled babel-plugin-transform-es2015-modules-commonjs to require babel-plugin-transform-strict-mode .我们可以禁用babel-plugin-transform-es2015-modules-commonjs以要求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所以在node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js的 151 行注释下面的代码

//inherits: require("babel-plugin-transform-strict-mode"),

just change .babelrc solution只需更改.babelrc解决方案

if you don't want to change any npm modules, you can use .babelrc ignore like this如果您不想更改任何 npm 模块,可以像这样使用.babelrc ignore

{
  "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!不能使用'use strict'的被忽略文件是旧代码,不需要使用babel改造!

Personally, I use the gulp-iife plugin and I wrap IIFEs around all my files.就个人而言,我使用 gulp-iife 插件并将 IIFE 包装在我的所有文件周围。 I noticed that the babel plugin (using preset es2015) adds a global "use strict" as well.我注意到 babel 插件(使用预设 es2015)也添加了一个全局“use strict”。 I run my post babel code through the iife stream plugin again so it nullifies what babel did.我再次通过 iife 流插件运行我的 post babel 代码,这样它就取消了 babel 所做的事情。

 gulp.task("build-js-source-dev", function () { return gulp.src(jsSourceGlob) .pipe(iife()) .pipe(plumber()) .pipe(babel({ presets: ["es2015"] }))// compile ES6 to ES5 .pipe(plumber.stop()) .pipe(iife()) // because babel preset "es2015" adds a global "use strict"; which we dont want .pipe(concat(jsDistFile)) // concat to single file .pipe(gulp.dest("public_dist")) });

这在语法上是不正确的,但基本上适用于 Babel 5 和 6,而无需安装删除另一个模块的模块。

code.replace(/^"use strict";$/, '')

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).从 babel 6 开始,您可以首先安装 babel-cli(如果您想从 CLI 使用 Babel)或 babel-core(使用 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:并在 .babelrc 文件中添加已安装的模块,如下所示:

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

See details here: https://babeljs.io/blog/2015/10/31/setting-up-babel-6在此处查看详细信息: https ://babeljs.io/blog/2015/10/31/setting-up-babel-6

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 .对于 babel 6 而不是猴子修补预设和/或分叉并发布它,您也可以包装原始插件并将strict选项设置为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;

Please use "es2015-without-strict" instead of "es2015".请使用“es2015-without-strict”而不是“es2015”。 Don't forget you need to have package "babel-preset-es2015-without-strict" installed.不要忘记你需要安装包“babel-preset-es2015-without-strict”。 I know it's not expected default behavior of Babel, please take into account the project is not mature yet.我知道这不是 Babel 的默认行为,请考虑项目尚未成熟。

I just made a script that runs in the Node and removes "use strict";我刚刚制作了一个在节点中运行并删除“use strict”的脚本; in the selected file.在选定的文件中。

file: script.js:文件:script.js:

let fs = require('fs');
let file = 'custom/path/index.js';
let data = fs.readFileSync(file, 'utf8');
let regex = new RegExp('"use\\s+strict";');
if (data.match(regex)){
    let data2 = data.replace(regex, '');
    fs.writeFileSync(file, data2);
    console.log('use strict mode removed ...');
}
else {
    console.log('use strict mode is missing .');
}

node ./script.js

if you are using https://babeljs.io/repl ( v7.8.6 as of this writing), you can remove "use strict";如果您使用的是https://babeljs.io/repl (撰写本文时为v7.8.6 ),您可以删除"use strict"; by selecting Source Type -> Module .通过选择Source Type -> Module

Using plugins or disabling modules and strict mode as suggested in the @rcode's answer didn't work for me.按照@rcode 的回答中的建议使用插件或禁用模块和严格模式对我不起作用。

But, changing the target from es2015 |但是,从es2015更改目标 | es6 to es5 in tsconfig.json file as suggested by @andrefarzart in this GitHub answer fixed the issue. es6此 GitHub 答案中建议的tsconfig.json文件中的 es6 到es5解决了该问题。

// tsconfig.json file
{
  // ...
  "compilerOptions": {
    // ...
    "target": "es5", // instead of "es2015"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM