0

I have written my firebase functions in ES2017 code, which I had to transpile since I cannot deploy functions with ES2017 JavaScript. The way I understand it is firebase serve serves up the functions to run in my local environment (which uses ES2017) and firebase deploy deploys the functions to the cloud (no ES2017).

Before I deploy, I npm run prepare and babel grabs my index.js file in the main folder, transpiles it and puts in in a /dist folder, along with some config files.

In order to get firebase to deploy from the ./dist folder, I set up the entry point "main": "./dist/index.js" in the package.json. However, this means when I want to firebase serve for my local environment, I serve up the transpiled functions from the ./dist folder. So I have to keep changing between "main": "index.js" and "main": "./dist/index.js" depending on whether I want to serve the functions locally or deploy the transpiled functions.

This is despite having specified where I want to serve and deploy from, like so:

  "scripts": {
    "lint": "./node_modules/.bin/eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions/dist",
    "logs": "firebase functions:log"
  }

My entire package.json file:

{
  "main": "./dist/index.js",
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "./node_modules/.bin/eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions/dist",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "babel-runtime": "^6.26.0",
    "cors": "^2.8.4",
    "cross-fetch": "^2.1.0",
    "firebase-admin": "^5.8.2",
    "firebase-functions": "^1.0.1",
    "moment": "^2.22.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "scripts": {
    "prepare": "babel ./*.js --retain-lines -d ./dist && cp -a ./private ./dist/private",
    "lint": "./node_modules/.bin/eslint --max-warnings=0 ."
  },
  "private": true
}
OctaviaLo
  • 1,268
  • 2
  • 21
  • 46
  • 1
    It doesn't seem like you have a question here. I will comment that I think it's not a good idea to emulate locally with a completely different environment than what Cloud Functions offers. You'll simplify everything if you only ever test with transpiled code that works in both environments. – Doug Stevenson May 06 '18 at 16:57
  • @DougStevenson ok, I'm still new to development so don't know what the best practice is. If that's so, I'd go with it, thanks for the comment :) – OctaviaLo May 06 '18 at 18:04

0 Answers0