Javascript 如何删除babel添加的全局“use strict”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33821312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove global "use strict" added by babel
提问by ani h
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
我正在使用“使用严格”的函数形式,并且不想要 Babel 在转译后添加的全局形式。问题是我正在使用一些未使用“使用严格”模式的库,并且在连接脚本后可能会抛出错误
采纳答案by T.J. Crowder
Babel 5
通天塔 5
You'd blacklist "useStrict"
. For instance here's an example in a Gruntfile:
你会加入黑名单"useStrict"
。例如,这是 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. 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.
由于 Babel 6 现在完全选择加入插件,而不是列入黑名单useStrict
,您只是不包含strict-mode
插件。如果您使用包含它的预设,我认为您必须创建自己的预设,其中包括所有其他预设,但不是那个预设。
回答by rcode
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:
正如已经在 Babel 6 中提到的那样,它是transform-es2015-modules-commonjs
添加严格模式的预设。如果您想在es2015
没有模块转换的情况下使用整个预设,请将其放入您的.babelrc
文件中:
{
"presets": [
["es2015", { "modules": false }]
]
}
This will disable modules and strict mode, while keeping all other es2015 transformations enabled.
这将禁用模块和严格模式,同时启用所有其他 es2015 转换。
回答by Alan Pierce
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.
现在有一个 babel 插件,您可以将其添加到您的配置中,该插件将删除严格模式:babel-plugin-transform-remove-strict-mode
. "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"]
}
回答by Adam Reis
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
我也遇到了这个相当荒谬的限制,即您无法禁用或覆盖现有预设中的设置,并转而使用此预设:https: //www.npmjs.com/package/babel-preset-es2015-without-strict
回答by Kley Andromorfia
plugins: [
[
require("@babel/plugin-transform-modules-commonjs"),
{
strictMode: false
}
],
]
回答by Brian Schermerhorn
Personally, I use the gulp-iife plugin and I wrap IIFEs around all my files. I noticed that the babel plugin (using preset es2015) adds a global "use strict" as well. I run my post babel code through the iife stream plugin again so it nullifies what babel did.
就个人而言,我使用 gulp-iife 插件并将 IIFE 包裹在我所有的文件中。我注意到 babel 插件(使用预设 es2015)也添加了全局“使用严格”。我再次通过 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"))
});
回答by shihongzhi
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
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
所以node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js
在 151 行注释以下代码
//inherits: require("babel-plugin-transform-strict-mode"),
回答by hisland
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 filethat can't use 'use strict'
is old code, and do not need to use babel to transform it!
不能使用的忽略文件'use strict'
是旧代码,不需要用babel来改造!
回答by Alina Poluykova
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.
从 babel 6 开始,您可以首先安装 babel-cli(如果您想从 CLI 使用 Babel)或 babel-core(使用 Node API)。这个包不包括模块。
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
回答by Joscha
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;