Skip to content

Commit e76b9f6

Browse files
authored
docs: Functions ESM Sample (GoogleCloudPlatform#315)
docs: add docs for esm support Signed-off-by: Grant Timmerman <[email protected]>
1 parent 3c257d9 commit e76b9f6

File tree

7 files changed

+815
-1
lines changed

7 files changed

+815
-1
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/node_modules
22
build/
33
test/data/esm_*
4+
docs/

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This directory contains advanced docs around the Functions Framework.
66
- [Debugging Functions](debugging.md)
77
- [Running and Deploying Docker Containers](docker.md)
88
- [Writing a Function in Typescript](typescript.md)
9-
- [ES Modules](esm.md)
9+
- [ES Modules](esm/README.md)
1010

1111
## TODO Docs
1212

docs/esm/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Using ES Modules
2+
3+
The Functions Framework >= `1.9.0` supports loading your code as an ES Module.
4+
5+
ECMAScript modules (ES modules or ESM) are a TC39 standard, unflagged feature in Node >=14 for loading JavaScript modules. As opposed to CommonJS, ESM provides an asynchronous API for loading modules and provides a very commonly adopted syntax improvement via `import` and `export` statements.
6+
7+
## Example
8+
9+
Before:
10+
11+
```js
12+
exports.helloGET = (req, res) => {
13+
res.send('No ESM.');
14+
};
15+
```
16+
17+
After:
18+
19+
```js
20+
export const helloGET = (req, res) => {
21+
res.send('ESM!');
22+
};
23+
```
24+
25+
## Quickstart
26+
27+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
28+
29+
Create a `package.json` file:
30+
31+
```json
32+
{
33+
"type": "module",
34+
"scripts": {
35+
"start": "functions-framework --target=helloGET"
36+
},
37+
"main": "index.js",
38+
"dependencies": {
39+
"@google-cloud/functions-framework": "^1.9.0"
40+
}
41+
}
42+
```
43+
44+
Create a `index.js` file:
45+
46+
```js
47+
export const helloGET = (req, res) => {
48+
res.send('ESM!');
49+
};
50+
```
51+
52+
Install dependencies and start the framework:
53+
54+
```sh
55+
npm i
56+
npm start
57+
```
58+
59+
Go to `localhost:8080/` and see your function execute!

docs/esm/consts.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START functions_esm_const]
16+
export const MY_CONST = 'ESM';
17+
// [END functions_esm_const]

docs/esm/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START functions_esm]
16+
import {MY_CONST} from './consts.js';
17+
18+
export const esm = (req, res) => {
19+
res.send(`Hello, ${MY_CONST}!`);
20+
};
21+
// [END functions_esm]

0 commit comments

Comments
 (0)