Babel 6预设不使用Firebase功能?

时间:2017-11-07 19:41:23

标签: javascript babel

我正在尝试在我的firebase函数中使用spread运算符,但它无效。

的package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "axios": "^0.16.2",
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "case": "^1.5.3",
    "change-case": "^3.0.1",
    "cheerio": "^1.0.0-rc.2",
    "emailjs": "^1.0.12",
    "firebase-admin": "~5.2.1",
    "firebase-functions": "^0.7.2",
    "lodash": "^4.17.4",
    "moment": "^2.18.1"
  },
  "babel": {
    "presets": [
      "env"
    ]
  },
  "private": true
}

featured.js:

"use strict";
const moment = require("moment");

const genDateWithStatus = (startDate, endDate) => ({
  startDate: moment(startDate).valueOf(),
  endDate: moment(endDate).valueOf(),
  status: moment(startDate).valueOf() > moment().valueOf()
    ? "upcoming"
    : moment(endDate).valueOf() > moment().valueOf() ? "active" : "recent"
});

module.exports = [
  {
    url: "https://blah.io/",
    ...genDateWithStatus("11/06/2017", "12/06/2017") // <---------- this part fails
  },
];

这是我尝试启动服务器时出现的错误

/Users/edmundmai/Documents/src/myapp/functions/src/config/featured.js:19
    ...genDateWithStatus("11/06/2017", "12/06/2017")
    ^^^

SyntaxError: Unexpected token ...
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)

1 个答案:

答案 0 :(得分:1)

对象传播运营商目前(2017年11月)在节点和浏览器中不受支持,因此您可以使用Object.assign语法

module.exports = [
  Object.assign({}, { url: "https://blah.io/" }, genDateWithStatus("11/06/2017", "12/06/2017")),
];

或明确添加babel-plugin-transform-object-rest-spread转换插件。

关于后者,另见Github问题:https://github.com/babel/babel-preset-env/issues/49