简体   繁体   中英

Babel 6 presets not working with Firebase functions?

I'm trying to use the spread operator in my firebase function and it's not working.

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
  },
];

This is the error that shows up when I try to start my server

/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)

Object spread operator is currently (November 2017) not supported in Node and browsers, so you could either use Object.assign syntax

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

or add the babel-plugin-transform-object-rest-spread transform plugin explicitly.

On the latter, see also Github issue: https://github.com/babel/babel-preset-env/issues/49 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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