Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/node_modules
build/
test/data/esm_*
docs/
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This directory contains advanced docs around the Functions Framework.
- [Debugging Functions](debugging.md)
- [Running and Deploying Docker Containers](docker.md)
- [Writing a Function in Typescript](typescript.md)
- [ES Modules](esm.md)
- [ES Modules](esm/README.md)

## TODO Docs

Expand Down
59 changes: 59 additions & 0 deletions docs/esm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Using ES Modules

The Functions Framework >= `1.9.0` supports loading your code as an ES Module.

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.

## Example

Before:

```js
exports.helloGET = (req, res) => {
res.send('No ESM.');
};
```

After:

```js
export const helloGET = (req, res) => {
res.send('ESM!');
};
```

## Quickstart

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

Create a `package.json` file:

```json
{
"type": "module",
"scripts": {
"start": "functions-framework --target=helloGET"
},
"main": "index.js",
"dependencies": {
"@google-cloud/functions-framework": "^1.9.0"
}
}
```

Create a `index.js` file:

```js
export const helloGET = (req, res) => {
res.send('ESM!');
};
```

Install dependencies and start the framework:

```sh
npm i
npm start
```

Go to `localhost:8080/` and see your function execute!
17 changes: 17 additions & 0 deletions docs/esm/consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START functions_esm_const]
export const MY_CONST = 'ESM';
// [END functions_esm_const]
21 changes: 21 additions & 0 deletions docs/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START functions_esm]
import {MY_CONST} from './consts.js';

export const esm = (req, res) => {
res.send(`Hello, ${MY_CONST}!`);
};
// [END functions_esm]
Loading