使用babel env预设时,散布运算符上的SyntaxError [英] SyntaxError on spread operator, while using babel env preset

查看:251
本文介绍了使用babel env预设时,散布运算符上的SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过替换babel mern.io入门级bolerplate >和stage-0env预设.但是,似乎env预设无法识别

I am trying to "modernize" mern.io starter bolerplate by replacing babel es2015 and stage-0 presets with env. However, it seems that env preset does not recognize the following snippet in client/modules/Intl/IntlReducer.js:9:

import { enabledLanguages, localizationData } from '../../../Intl/setup';
import { SWITCH_LANGUAGE } from './IntlActions';

const initLocale = global.navigator && global.navigator.language || 'en';

const initialState = {
  locale: initLocale,
  enabledLanguages,
  ...(localizationData[initLocale] || {}),
// ^ 
// SyntaxError: client/modules/Intl/IntlReducer.js: Unexpected token
};


const IntlReducer = (state = initialState, action) => {
  switch (action.type) {
    case SWITCH_LANGUAGE: {
      const { type, ...actionWithoutType } = action; // eslint-disable-line
      return { ...state, ...actionWithoutType };
    }

    default:
      return state;
  }
};

export default IntlReducer;

localizationData对象填充在

这个传播运算符对我来说似乎很正常,我无法发现语法错误. 另外,似乎在此文件之前的其他代码也可以很好地进行编译,因此我猜想env预设已正确应用.

This spread operator seems quite normal to me, I cannot spot the syntax error. Also it seems that other code preceding to this file, is being transpiled just fine, so I guess env preset is applied correctly.

此失败的原因可能是什么?从第0阶段到当前版本的ES中是否不接受这种特殊的传播算子风格?我该如何进一步调查这个问题?

What may be the reason of this failure? Was this particular flavor of spread operator not accepted from stage-0 into current version of ES? How can I investigate this issue further?

完整的错误消息(剥离了项目根路径):

Full error message (project root path stripped):

node_modules/babel-core/lib/transformation/file/index.js:590
      throw err;
      ^

SyntaxError: client/modules/Intl/IntlReducer.js: Unexpected token (9:2)
   7 |   locale: initLocale,
   8 |   enabledLanguages,
>  9 |   ...localizationData[initLocale]
     |   ^
  10 | };
  11 | 
  12 | const IntlReducer = (state = initialState, action) => {
    at Parser.pp$5.raise (node_modules/babylon/lib/index.js:4454:13)
    at Parser.pp.unexpected (node_modules/babylon/lib/index.js:1761:8)
    at Parser.pp$3.parseIdentifier (node_modules/babylon/lib/index.js:4332:10)
    at Parser.pp$3.parsePropertyName (node_modules/babylon/lib/index.js:4156:96)
    at Parser.parsePropertyName (node_modules/babylon/lib/index.js:6229:23)
    at Parser.pp$3.parseObj (node_modules/babylon/lib/index.js:4045:12)
    at Parser.pp$3.parseExprAtom (node_modules/babylon/lib/index.js:3719:19)
    at Parser.parseExprAtom (node_modules/babylon/lib/index.js:7238:22)
    at Parser.pp$3.parseExprSubscripts (node_modules/babylon/lib/index.js:3494:19)
    at Parser.pp$3.parseMaybeUnary (node_modules/babylon/lib/index.js:3474:19)
    at Parser.pp$3.parseExprOps (node_modules/babylon/lib/index.js:3404:19)
    at Parser.pp$3.parseMaybeConditional (node_modules/babylon/lib/index.js:3381:19)
    at Parser.pp$3.parseMaybeAssign (node_modules/babylon/lib/index.js:3344:19)
    at Parser.parseMaybeAssign (node_modules/babylon/lib/index.js:6474:20)
    at Parser.pp$1.parseVar (node_modules/babylon/lib/index.js:2340:24)
    at Parser.pp$1.parseVarStatement (node_modules/babylon/lib/index.js:2169:8)

推荐答案

对象传播尚不在规范中,它目前处于阶段3,这意味着env预设将不提供此功能.

Object spread is not in the specification yet, it is currently in stage 3, which means that the env preset won't provide this feature.

如果要与Babel一起使用此功能,则需要添加

If you want to use this feature with Babel, you will need to add the babel-plugin-transform-object-rest-spread transform.

您可以在此回购中检查ES提案的状态 https://github.com/tc39/proposals

You can check the status of the ES proposals in this repo https://github.com/tc39/proposals

这篇关于使用babel env预设时,散布运算符上的SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆