Skip to content

Commit e055b63

Browse files
author
Nicolas Garnier
committed
Change TypeScript getting started to use the new TypeScript support from the CLI.
Change-Id: Ic1fc6686dee79aadf4770f08a1b32fd4ae1a7a14
1 parent 1238e84 commit e055b63

File tree

8 files changed

+192
-577
lines changed

8 files changed

+192
-577
lines changed

typescript-getting-started/HOW-TO.md

Lines changed: 9 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
The following steps show how to set up TypeScript yourself starting
44
from scratch.
55

6-
7-
86
Steps:
97

108
1. Create a project in the [Firebase Console](https://console.firebase.google.com/)
@@ -21,87 +19,21 @@ firebase init
2119
```
2220
3. You will be prompted to select the Firebase project you just created in the
2321
Console UI and choose which Firebase features you want to use. Select
24-
Functions and whatever other features you want to use, then type "n" when
25-
prompted to install dependencies with npm:
26-
```
27-
You're about to initialize a Firebase project in this directory:
28-
29-
/Users/.../functions-typescript
30-
31-
? Which Firebase CLI features do you want to setup for this folder? Press Space to select features, then Enter
32-
to confirm your choices. Functions: Configure and deploy Cloud Functions
33-
34-
=== Project Setup
35-
36-
First, let's associate this project directory with a Firebase project.
37-
You can create multiple project aliases by running firebase use --add,
38-
but for now we'll just set up a default project.
39-
40-
i .firebaserc already has a default project, skipping
41-
42-
=== Functions Setup
43-
44-
A functions directory will be created in your project with a Node.js
45-
package pre-configured. Functions can be deployed with firebase deploy.
46-
47-
✔ Wrote functions/package.json
48-
✔ Wrote functions/index.js
49-
? Do you want to install dependencies with npm now? No
50-
51-
i Writing configuration info to firebase.json...
52-
i Writing project information to .firebaserc...
53-
54-
✔ Firebase initialization complete!
22+
Functions and whatever other features you want to use. Choose Typescript as the language you want to use for your functions and then type "n" when
23+
prompted to install dependencies with npm.
24+
4. The project will be automatically created with a ready-to-use Typescript enabled project. Your source code will go into the `functions/src` directory and it will be compiled to the `functions/lib` directory when running the Typescript compiler.
25+
5. To compile your source code you can run `npm run build` from the functions directory. Also note that a pre-deploy trigger has been added so that your source code is automatically compiled before each deployment when running `firebase deploy`.
26+
6. install dependencies with npm (still in `functions` directory)
5527
```
56-
4. create a `src` directory and move index.js into it (changing the suffix)
57-
```
58-
cd functions
59-
mkdir src
60-
mv index.js src/index.ts
61-
```
62-
5. add the following dev dependencies, scripts, and we'll be compiling the js
63-
files in a directory called `build`, so we can specify where to start loading
64-
JavaScript for Cloud Functions as `"main"` in functions/package.json
65-
```
66-
"devDependencies": {
67-
"typescript": "^2.3.2"
68-
},
69-
"scripts": {
70-
"build": "tsc",
71-
"watch": "tsc --watch",
72-
"deploy": "tsc && firebase deploy --only functions"
73-
},
74-
"main": "build/index.js",
75-
```
76-
77-
6. install dependencies with yarn (still in `functions` directory)
78-
```
79-
yarn install
80-
```
81-
7. create a `tsconfig.json` file:
82-
```
83-
{
84-
"compilerOptions": {
85-
"lib": ["es6", "es2015.promise"],
86-
"module": "commonjs",
87-
"noImplicitAny": false,
88-
"outDir": "build",
89-
"sourceMap": true,
90-
"target": "es6",
91-
},
92-
"include": [
93-
"src/**/*.ts",
94-
"spec/**/*.ts"
95-
]
96-
}
28+
npm install
9729
```
9830

9931
Note: if you want to just start using TypeScript gradually, you can target
10032
"es5" and the compiler will allow you to intermix old-school JavaScript
10133
and TypeScript.
10234

10335

104-
8. I like to exclude node_modules and compiled js files from git, so
36+
7. I like to exclude node_modules and compiled js files from git, so
10537
I add this to a root level `.gitignore` file
10638

10739
```
@@ -112,25 +44,13 @@ functions/**/*.js
11244
functions/**/*.js.map
11345
```
11446

115-
9. in `index.ts` update the syntax to TypeScript (`require` -> `import` and
116-
`exports.` -> `export let`)
117-
118-
```
119-
import * as functions from 'firebase-functions'
120-
121-
export let helloWorld = functions.https.onRequest((request, response) => {
122-
response.send("Hello from Firebase!\n\n");
123-
});
124-
```
125-
126-
9. in `functions` directory, use the npm build script created
127-
above to build TypeScript files and deploy:
47+
8. in `functions` directory, use the npm deploy script to deploy your functions:
12848
```
12949
npm run deploy
13050
```
13151
You will see a bunch of output and at the end it will show you the URL for your deployed function.
13252

133-
10. test YOUR function with curl. For mine I can do this:
53+
9. test YOUR function with curl. For mine I can do this:
13454
```
13555
curl https://us-central1-functions-typescript.cloudfunctions.net/helloWorld
13656
```

typescript-getting-started/README.md

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ development of JavaScript with optional types. When types are used,
1515
supported editors provide auto-suggest for methods and properties along
1616
with syntax highlighting of errors, which speeds development.
1717

18-
TypeScript supports targeting different browsers, and optimizes
18+
TypeScript supports targeting different browsers or node versions, and optimizes
1919
the resulting JavaScript. It is much easier to write clean, consistent code
2020
across a project and development team. TypeScript offers support for the
2121
latest and evolving JavaScript features like async functions and decorators,
@@ -25,73 +25,55 @@ For a nice intro to TypeScript, check out the [TypeScript PlayGround](https://ww
2525

2626
### What is different about TypeScript in this example?
2727

28-
The TypeScript source is in `functions/src` and then we need to do a build
29-
step before deploying (see steps below). The main Cloud Function entry
30-
point is `src/index.ts` which compiled to `src/index.js` and that is specified
28+
The TypeScript source is in `functions/src` and these files need to be compiled before deploying (see steps below). The main Cloud Function entry
29+
point is `src/index.ts` and it compiled to `lib/index.js` which is specified
3130
in `functions/package.json`.
3231

33-
There are two key differences to the example Cloud Function:
32+
There are two key differences between the example Cloud Function in Typescript and ES2015:
3433

3534
* `require` -> `import`
36-
* `exports.` -> `export let`
35+
* `exports.` -> `export const`
3736

3837
JavaScript:
3938
```
40-
var functions = require('firebase-functions');
39+
const functions = require('firebase-functions');
4140
4241
exports.helloWorld = functions.https.onRequest((request, response) => {
43-
response.send("Hello from Firebase!\n\n");
42+
response.send('Hello from Firebase!\n\n');
4443
});
4544
```
4645

4746
TypeScript:
4847
```
4948
import * as functions from 'firebase-functions'
5049
51-
export let helloWorld = functions.https.onRequest((request, response) => {
52-
response.send("Hello from Firebase!\n\n");
50+
export const helloWorld = functions.https.onRequest((request, response) => {
51+
response.send('Hello from Firebase!\n\n');
5352
});
5453
```
5554

5655

57-
5856
### Project Setup
5957

60-
This project was set up using [yarn](https://yarnpkg.com) which will ensure
61-
that your deployed Cloud Functions are using the exact same versions of
62-
npm modules that you are running locally. The specific version of each
63-
dependency is saved in [yarn.lock](functions/yarn.lock)
64-
65-
6658
This example has the default sample function. To use this example as a
6759
starter project:
6860

6961
1. `npm install -g firebase-tools`
70-
2. `git clone https://github.com/firebase/functions-samples.git`
7162
3. Create a Firebase Project using the Firebase Developer Console
72-
4. Make a directory for your project and cd into it. Then initialize
73-
the Firebase project and replace the default functions directory with
74-
this one. The directory name doesn't have to be the same as your
75-
Firebase project name -- that's just common practice. In the following
76-
steps a shell variable is used to make it simple to change the first line:
77-
```
78-
PROJECT=your-project-name
79-
mkdir $PROJECT; cd $PROJECT
80-
firebase init
81-
firebase use --add $PROJECT
82-
rm -rf functions
83-
cp -r ../functions-samples/typescript-getting-started/functions functions
84-
```
85-
5. Install the dependencies and deploy
63+
2. Download the source code of this sample: `git clone https://github.com/firebase/functions-samples`
64+
4. Enter the right sample directory: `cd functions-samples/typescript-getting-started`
65+
5. Configure this project to use the Firebase project you have created: `firebase use --add` and select your project in the list.
66+
6. Install the dependencies and deploy
8667
```
8768
cd functions
88-
yarn # or npm install
69+
npm install
8970
npm run deploy
9071
```
9172

9273
Note: with TypeScript you need to build the JavaScript files before
93-
deploying, so there's an npm script that does the steps. You can see
94-
that and a few other handy shortcuts in [package.json](functions/package.json)
74+
deploying, so an npm script does this steps. You can see
75+
that and a few other handy shortcuts in [package.json](functions/package.json).
76+
Also a pre-deploy trigger ensures that the code is always transpied before deploying. You can see this in the [firebase.json](firebase.json).
9577

9678
After the deploy is complete, you will see output with the URL of your
9779
Cloud Function endpoint. You can test the function with curl. The following
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{}
1+
{
2+
"functions": {
3+
"predeploy": "npm --prefix functions run build"
4+
}
5+
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{
22
"name": "functions",
3-
"description": "Cloud Functions for Firebase",
3+
"description": "TypeScript sample Cloud Functions for Firebase",
44
"dependencies": {
5-
"firebase-admin": "~4.2.1",
6-
"firebase-functions": "^0.5.7"
5+
"firebase-admin": "~5.5.0",
6+
"firebase-functions": "^0.7.1"
77
},
88
"devDependencies": {
99
"@types/express": "^4.0.37",
10-
"typescript": "^2.3.2"
10+
"tslint": "^5.8.0",
11+
"typescript": "^2.6.1"
1112
},
1213
"scripts": {
13-
"build": "tsc",
14-
"watch": "tsc --watch",
15-
"deploy": "tsc && firebase deploy --only functions"
14+
"build": "./node_modules/.bin/tslint -p tslint.json && ./node_modules/.bin/tsc",
15+
"serve": "npm run build && firebase serve --only functions",
16+
"shell": "npm run build && firebase experimental:functions:shell",
17+
"start": "npm run shell",
18+
"deploy": "firebase deploy --only functions",
19+
"logs": "firebase functions:log"
1620
},
17-
"main": "build/index.js",
21+
"main": "lib/index.js",
1822
"private": true
1923
}

typescript-getting-started/functions/src/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
118
import * as functions from 'firebase-functions'
219

320
// if you need to use the Firebase Admin SDK, uncomment the following:
@@ -9,6 +26,6 @@ import * as functions from 'firebase-functions'
926
// cd functions
1027
// npm run deploy
1128

12-
export let helloWorld = functions.https.onRequest((request, response) => {
13-
response.send("Hello from Firebase!\n\n");
29+
export const helloWorld = functions.https.onRequest((request, response) => {
30+
response.send('Hello from Firebase!\n\n');
1431
});
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"compilerOptions": {
3-
"lib": ["es6", "es2015.promise"],
3+
"lib": ["es6"],
44
"module": "commonjs",
5-
"noImplicitAny": false,
6-
"outDir": "build",
5+
"noImplicitReturns": true,
6+
"outDir": "lib",
77
"sourceMap": true,
88
"target": "es6"
99
},
10+
"compileOnSave": true,
1011
"include": [
11-
"src/**/*.ts",
12-
"spec/**/*.ts"
12+
"src"
1313
]
14-
}
14+
}

0 commit comments

Comments
 (0)