[英]babel - es2015 and es2017 preset not working together
I have two js files : play.js
and myStore.js
. 我有两个js文件:
play.js
和myStore.js
。
I want to import code from myStore.js
into play.js
and use it there. 我想将代码从
myStore.js
导入play.js
并在其中使用。 I'm using es2015
plugin for the import, But it makes my es2017
friendly code fail, even though I have es2017
setup. 我正在使用
es2015
插件进行导入,但这会导致我的es2017
友好代码失败,即使我具有es2017
设置也是如此。
play.js : play.js :
import G from '../functions/myStore.js'; // import needs es2015
// this works with es2017, but not when es2015 is also included
for(k in [1,2,3]) console.log(k)
myStore.js myStore.js
var G = {}
export default G
Output : 输出 :
If I did not import
anything, and just used the es2017
preset, this would run fine, but using es2015
along with es2017
makes this fail as below: 如果我不
import
任何东西,只是用es2017
预设,这会运行良好,但使用es2015
随着es2017
使得这款如下失败:
for (k in [1, 2, 3]) {
^
ReferenceError: k is not defined
I'm executing this from terminal via npm start
. 我正在通过
npm start
从终端执行此操作。 Here's my package.json : 这是我的package.json :
{
"name": "functions",
"version": "1.0.0",
"description": "",
"main": "play.js",
"scripts": {
"start": "babel-node play.js"
},
"author": "Somjit",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-es2015-node5": "^1.2.0",
"babel-preset-es2017": "^6.22.0"
}
}
and my babel.rc : 和我的babel.rc :
{ "presets": ["es2015", "es2017"] }
Ok. 好。 My short answer would be...
我的简短答案是...
1) Babel reads your code and if it sees some new js feature it translate it to regular js by using presets. 1)Babel读取您的代码,如果看到一些新的js功能,则会通过使用预设将其转换为常规js。 For example, if it sees
let a = 1
Babel uses preset-es2015
(that knows what let
is) and translate this line into var a = 1
so your browser could understand this line. 例如,如果看到
let a = 1
Babel使用preset-es2015
(知道let
是什么)并将此行转换为var a = 1
以便您的浏览器可以理解此行。
2) If you look to the docs of babel-preset-es2017 you'll see that it supports only two features. 2)如果查看babel-preset-es2017的文档 ,您会看到它仅支持两个功能。 You have not this features in your code.
您的代码中没有此功能。 So babel don't use this preset while reading code that you've provided.
因此,babel在阅读您提供的代码时不要使用此预设。 So es2017 don't matter in your problem.
因此es2017对您的问题无关紧要。
3) If you run your code without es2015 it allows you to declare variables without var (because you can do this in js without strict mode). 3)如果在不使用es2015的情况下运行代码,则允许您声明不带var的变量(因为您可以在js中不使用严格模式进行此操作)。 But when you use this preset Babel reads your code and trows an Error because according to new js standarts you need to declare variables with var, let or const and can't just write
a = 1
; 但是,当您使用此预设时,Babel会读取您的代码并抛出错误,因为根据新的js标准,您需要使用var,let或const声明变量,而不能只写
a = 1
;
In Your case, absolutely your problem is not babel-preset-es2017
. 就您而言,您的问题绝对不是
babel-preset-es2017
。 you must install babel-plugin-transform-runtime
and put it in your .babelrc
file as plugin. 您必须安装
babel-plugin-transform-runtime
并将其作为插件放入.babelrc
文件中。
installing: 安装:
npm install --save-dev babel-plugin-transform-runtime
setting .babelrc
file: 设置
.babelrc
文件:
{
"presets": [
"es2015",
"es2017"
],
"plugins": [
"transform-runtime"
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.