- use VSCode as an editor.
- Use TypeScript for AltJS. DO NOT WRITE RAW JavaScript at all.
- Use Yarn instead of npm.
- Deploy via firebse tools.
- use latest version package if available. Especially if you use firestore packages for "firebase-admin": "^5.0.0", "firebase-functions": "^0.7.0" or higher is required.
Prepare tools and yarn.
npm install -g firebase-tools
npm instakk -g yarn
-
initialize firebase project
firebase init
-
move to
functions
directory.functions
directory will be created by firebase init.
-
create
function/src
directory and movefunctions/index.js
tofunctions/src/index.ts
as for TypeScript. -
add
functions/package.json
file. -
install dependencies with
yarn
ornpm
. I recommend use yarn.yarn install
-
create a
tsconfig.json
.
{
"name": "function",
"version": "0.0.0",
"description": "cloud functions for twitter authorize",
"main": "index.js",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"logs": "firebase functions:log",
"build": "tsc",
"watch": "tsc --watch",
"deploy": "tsc && firebase deploy --only functions"
},
"author": "guitarrapc",
"license": "",
"dependencies": {
"@google-cloud/firestore": "^0.10.0",
"@types/firebase": "^2.4.32",
"firebase-admin": "~5.5.1",
"firebase-functions": "^0.7.3"
},
"devDependencies": {
"typescript": "^2.2.2"
}
}
{
"compilerOptions": {
"lib": ["es6", "es2015.promise"],
"module": "commonjs",
"outDir": "./",
"noImplicitAny": false,
"strictNullChecks": true,
"sourceMap": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src/**/*.ts",
"spec/**/*.ts"
],
"exclude": [
"node_modeles"
]
}
{
"dependencies": {}
}
you can specify which is entry point .js of function with define in 3 way.
- index.js : export 1 or more function.
- app.js : export 1 or more functions with package.json file that contains
"main" : "app.js"
- index.js : import one or more functions from goo.js file and then export one or more functions.
In this scenario, use TypeScript to define functions then transcompile to js.
these .ts files should be under functions/src
directory.
- index.ts : this handles export functions. no logics at all.
- main.ts : this handles initializer and import package.
- db.ts : this handles firebase realtimedatabase reference to be used in each functions.
- store.ts : this handles firestore reference to be used in each function.
- ****.ts : each functions. you will load index.ts and (db.ts | store.ts) for package reference. Make sure return promise.
if needed, run following.
firebase login
Specify GCP Project-Id at
.firebaserc
{
"projects": {
"default": "fir-sample-12345"
}
}
this is required to transcompile .ts to .js using tsc.
npm run build
npm run serve
npm run deploy
or
firebase deploy --only functions
firebase deploy --only functions:functionName