From c0636d2f43b09ab3d4270a1c02b7a450cace060e Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 30 Jun 2021 20:58:41 -0500 Subject: [PATCH] docs: add docs for esm support Signed-off-by: Grant Timmerman --- docs/README.md | 35 ++----------------------------- docs/esm.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 docs/esm.md diff --git a/docs/README.md b/docs/README.md index 2f5735b0..36590fcf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,39 +6,8 @@ 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) ## TODO Docs -- TODO: Run Multiple Cloud Functions [#23](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/23) - -## Debugging functions - -The Functions Framework works with standard tooling that you might use when writing a function for a Node.js environment. You can attach a debugger to your function by following these steps. - -1. Write an `index.js` file containing your Node.js function: - - ```js - exports.helloWorld = (req, res) => { - res.send('Hello, World'); - }; - ``` - -2. Install the Functions Framework: - - ```sh - npm install @google-cloud/functions-framework - ``` - -3. Run `node`, enable the inspector and run the Functions Framework: - - ```sh - node --inspect node_modules/@google-cloud/functions-framework --target=helloWorld - ... - Debugger listening on ws://127.0.0.1:9229/5f57f5e9-ea4b-43ce-be1d-6e9b838ade4a - For help see https://nodejs.org/en/docs/inspector - Serving function... - Function: helloWorld - URL: http://localhost:8080/ - ``` - -You can now use an IDE or other tooling to add breakpoints, step through your code and debug your function. +- Run Multiple Cloud Functions [#23](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/23) diff --git a/docs/esm.md b/docs/esm.md new file mode 100644 index 00000000..3fd898cb --- /dev/null +++ b/docs/esm.md @@ -0,0 +1,57 @@ +# 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 + +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!