Skip to content

Commit a796c6e

Browse files
authored
Merge branch 'master' into inject-client-context-to-dev-server
2 parents 705d30e + 714ace4 commit a796c6e

File tree

7 files changed

+2217
-1575
lines changed

7 files changed

+2217
-1575
lines changed

.github/stale.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Configuration for probot-stale - https://github.com/probot/stale
2+
3+
# Number of days of inactivity before an Issue or Pull Request becomes stale
4+
daysUntilStale: 60
5+
6+
# Number of days of inactivity before an Issue or Pull Request with the stale label is closed.
7+
# Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale.
8+
daysUntilClose: 7
9+
10+
# Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable
11+
exemptLabels:
12+
- pinned
13+
- security
14+
- "[Status] Maybe Later"
15+
16+
# Set to true to ignore issues in a project (defaults to false)
17+
exemptProjects: false
18+
19+
# Set to true to ignore issues in a milestone (defaults to false)
20+
exemptMilestones: false
21+
22+
# Label to use when marking as stale
23+
staleLabel: wontfix
24+
25+
# Comment to post when marking as stale. Set to `false` to disable
26+
markComment: >
27+
This issue has been automatically marked as stale because it has not had
28+
recent activity. It will be closed if no further activity occurs. Thank you
29+
for your contributions.
30+
31+
# Comment to post when removing the stale label.
32+
# unmarkComment: >
33+
# Your comment here.
34+
35+
# Comment to post when closing a stale Issue or Pull Request.
36+
# closeComment: >
37+
# Your comment here.
38+
39+
# Limit the number of actions per hour, from 1-30. Default is 30
40+
limitPerRun: 30
41+
42+
# Limit to only `issues` or `pulls`
43+
# only: issues
44+
45+
# Optionally, specify configuration settings that are specific to just 'issues' or 'pulls':
46+
# pulls:
47+
# daysUntilStale: 30
48+
# markComment: >
49+
# This pull request has been automatically marked as stale because it has not had
50+
# recent activity. It will be closed if no further activity occurs. Thank you
51+
# for your contributions.
52+
53+
# issues:
54+
# exemptLabels:
55+
# - confirmed

README.md

Lines changed: 154 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ This is a small CLI tool that helps with building or serving lambdas built with
44

55
The goal is to make it easy to work with Lambda's with modern ES6 without being dependent on having the most state of the art node runtime available in the final deployment environment and with a build that can compile all modules into a single lambda file.
66

7-
## Installation
7+
Since v1.0.0 the dependencies were upgraded to Webpack 4 and Babel 7.
88

9-
We recommend installing locally rather than globally: `yarn add -D netlify-lambda`
9+
## Installation
1010

11-
At the present moment you may have to also install peer dependencies [as documented here](https://github.com/netlify/netlify-lambda/issues/35) - we will correct this for the next release when we update our [webpack and babel versions](https://github.com/netlify/netlify-lambda/pull/15).
11+
**We recommend installing locally** rather than globally: `yarn add -D netlify-lambda`. This will ensure your build scripts don't assume a global install which is better for your CI/CD (for example with Netlify's buildbot).
1212

1313
## Usage
1414

@@ -19,7 +19,7 @@ netlify-lambda serve <folder>
1919
netlify-lambda build <folder>
2020
```
2121

22-
Both depends on a `netlify.toml` file being present in your project and configuring functions for deployment.
22+
**IMPORTANT**: Both commands depend on a `netlify.toml` file being present in your project and configuring functions for deployment.
2323

2424
The `serve` function will start a dev server and a file watcher for the specified folder and route requests to the relevant function at:
2525

@@ -29,38 +29,182 @@ http://localhost:9000/hello -> folder/hello.js (must export a handler(event, con
2929

3030
The `build` function will run a single build of the functions in the folder.
3131

32+
There are additional options, introduced later:
33+
```bash
34+
-h --help
35+
-c --config
36+
-p --port
37+
-s --static
38+
```
39+
40+
## Using with `create-react-app`, Gatsby, and other development servers
41+
42+
`react-scripts` (the underlying library for `create-react-app`) and other popular development servers often set up catchall serving for you; in other words, if you try to request a route that doesn't exist, the dev server will try to serve you `/index.html`. This is problematic when you are trying to hit a local API endpoint like `netlify-lambda` sets up for you - your browser will attempt to parse the `index.html` file as JSON. This is why you may see this error:
43+
44+
`Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0`
45+
46+
If this desribes your situation, then you need to proxy for local development. Read on. Don't worry it's easier than it looks.
47+
3248
### Proxying for local development
3349

34-
When your function is deployed on Netlify, it will be available at `/.netlify/functions/function-name` for any given deploy context. It is advantageous to proxy the `netlify-lambda serve` development server to the same path on your primary development server.
50+
> ⚠️IMPORTANT! PLEASE READ THIS ESPECIALLY IF YOU HAVE CORS ISSUES⚠️
3551
36-
Say you are running `webpack-serve` on port 8080 and `netlify-lambda serve` on port 9000. Mounting `localhost:9000` to `/.netlify/functions/` on your `webpack-serve` server (`localhost:8080/.netlify/functions/`) will closely replicate what the final production environment will look like during development, and will allow you to assume the same function url path in development and in production.
52+
When your function is deployed on Netlify, it will be available at `/.netlify/functions/function-name` for any given deploy context. It is advantageous to proxy the `netlify-lambda serve` development server to the same path on your primary development server.
3753

38-
See [netlify/create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/3b5fac5fcbcba0e775b755311d29242f0fc1d68e/package.json#L19) for an example of how to do this.
54+
Say you are running `webpack-serve` on port 8080 and `netlify-lambda serve` on port 9000. Mounting `localhost:9000` to `/.netlify/functions/` on your `webpack-serve` server (`localhost:8080/.netlify/functions/`) will closely replicate what the final production environment will look like during development, and will allow you to assume the same function url path in development and in production.
55+
56+
- If you are using with `create-react-app`, see [netlify/create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/f0e94f1d5a42992a2b894bfeae5b8c039a177dd9/src/setupProxy.js) for an example of how to do this with `create-react-app`. [setupProxy is partially documented in the CRA docs](https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually).
57+
- If you are using Gatsby, see [their Advanced Proxying docs](https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying). This is implemented in the [JAMstack Hackathon Starter](https://github.com/sw-yx/jamstack-hackathon-starter).
58+
- If you are using Next.js, see [this issue for how to proxy](https://github.com/netlify/netlify-lambda/pull/28#issuecomment-439675503).
59+
- If you are using with Angular CLI, see the instructions below.
3960

4061
[Example webpack config](https://github.com/imorente/netlify-functions-example/blob/master/webpack.development.config):
4162

4263
```js
4364
module.exports = {
44-
mode: 'development',
65+
mode: "development",
4566
devServer: {
4667
proxy: {
4768
"/.netlify": {
4869
target: "http://localhost:9000",
49-
pathRewrite: {"^/.netlify/functions" : ""}
70+
pathRewrite: { "^/.netlify/functions": "" }
5071
}
5172
}
5273
}
74+
};
75+
```
76+
77+
The serving port can be changed with the `-p`/`--port` option.
78+
79+
<details>
80+
<summary>
81+
**Using with `Angular CLI`**
82+
</summary>
83+
84+
CORS issues when trying to use netlify-lambdas locally with angular? you need to set up a proxy.
85+
86+
Firstly make sure you are using relative paths in your app to ensure that your app will work locally and on Netlify, example below...
87+
88+
```js
89+
this.http.get('/.netlify/functions/jokeTypescript')
90+
```
91+
92+
Then place a `proxy.config.json` file in the root of your project, the contents should look something like...
93+
94+
```json
95+
{
96+
"/.netlify/functions/*": {
97+
"target": "http://localhost:9000",
98+
"secure": false,
99+
"logLevel": "debug",
100+
"changeOrigin": true
101+
}
53102
}
54103
```
55104

105+
- The `key` should match up with the location of your Transpiled `functions` as defined in your `netlify.toml`
106+
- The `target` should match the port that the lambdas are being served on (:9000 by default)
107+
108+
When you run up your Angular project you need to pass in the proxy config with the flag `--proxy-config` like so...
109+
110+
```bash
111+
ng serve --proxy-config proxy.config.json
112+
```
113+
114+
To make your life easier you can add these to your `scripts` in `package.json`
115+
116+
```json
117+
"scripts": {
118+
"start": "ng serve --proxy-config proxy.config.json",
119+
"build": "ng build --prod --aot && yarn nlb",
120+
"nls": "netlify-lambda serve src_functions",
121+
"nlb": "netlify-lambda build src_functions"
122+
}
123+
```
124+
125+
Obviously you need to run up `netlify-lambda` & `angular` at the same time.
126+
</details>
127+
56128
## Webpack Configuration
57129

58130
By default the webpack configuration uses `babel-loader` to load all js files. Any `.babelrc` in the directory `netlify-lambda` is run from will be respected. If no `.babelrc` is found, a [few basic settings are used](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L11-L15a).
59131

60-
If you need to use additional webpack modules or loaders, you can specify an additional webpack config with the `-c` option when running either `serve` or `build`.
132+
If you need to use additional webpack modules or loaders, you can specify an additional webpack config with the `-c`/`--config` option when running either `serve` or `build`. See this issue for an example of [how to write a webpack override file](https://github.com/netlify/netlify-lambda/issues/64).
61133

62134
The additional webpack config will be merged into the default config via [webpack-merge's](https://www.npmjs.com/package/webpack-merge) `merge.smart` method.
63135

136+
### Babel configuration
137+
138+
The default webpack configuration uses `babel-loader` with a [few basic settings](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L19-L33).
139+
140+
However, if any `.babelrc` is found in the directory `netlify-lambda` is run from, it will be used instead of the default one. If you need to run different babel versions for your lambda and for your app, [check this issue](https://github.com/netlify/netlify-lambda/issues/34) to override your webpack babel-loader.
141+
142+
### Use with TypeScript
143+
144+
We added `.ts` and `.mjs` support recently - [check here for the PR and usage tips](https://github.com/netlify/netlify-lambda/pull/76).
145+
146+
1. Install `@babel/preset-typescript`
147+
148+
```bash
149+
npm install --save-dev @babel/preset-typescript
150+
```
151+
152+
You may also want to add `typescript @types/node @types/aws-lambda`.
153+
154+
2. Create a custom `.babelrc` file:
155+
156+
```diff
157+
{
158+
"presets": [
159+
"@babel/preset-typescript",
160+
[
161+
"@babel/preset-env",
162+
{
163+
"targets": {
164+
"node": "6.10.3"
165+
}
166+
}
167+
]
168+
],
169+
"plugins": [
170+
"@babel/plugin-proposal-class-properties",
171+
"@babel/plugin-transform-object-assign",
172+
"@babel/plugin-proposal-object-rest-spread"
173+
]
174+
}
175+
```
176+
177+
3. (Optional) if you have `@types/aws-lambda` installed, your lambda functions can use the community typings for `Handler, Context, Callback`. See the typescript instructions in [create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/master/README.md#typescript) for an example.
178+
179+
### --static option
180+
181+
If you need an escape hatch and are building your lambda in some way that is incompatible with our build process, you can skip the build with the `-s` or `--static` flag. [More info here](https://github.com/netlify/netlify-lambda/pull/62).
182+
183+
## Debugging
184+
185+
To debug lambdas, prepend the `serve` command with [npm's package runner npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b) `npx --node-arg=--inspect netlify-lambda serve ...`. Additionally:
186+
187+
1. make sure that sourcemaps are built along the way (e.g. in the webpack configuration and the `tsconfig.json` if typescript is used)
188+
2. webpack's uglification is turned off with `optimization: { minimize: false }`. If using VSCode, it is likely that the `sourceMapPathOverrides` have to be adapted for breakpoints to work.
189+
190+
Netlify Functions [run in Node v8.10](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) and you may need to run the same version to mirror the environment locally. Also make sure to check that you aren't [committing one of these common Node 8 mistakes in Lambda!](https://serverless.com/blog/common-node8-mistakes-in-lambda/)
191+
192+
Don't forget to search our issues in case someone has run into a similar problem you have!
193+
194+
## Netlify Identity
195+
196+
Netlify Identity is [not supported at the moment](https://github.com/netlify/netlify-lambda/issues/51) inside `netlify-lambda` function emulation, but for now you can [read the docs](https://www.netlify.com/docs/functions/#identity-and-functions) on how they should work.
197+
198+
## Other community approaches
199+
200+
If you wish to serve the full website from lambda, [check this issue](https://github.com/netlify/netlify-lambda/issues/36).
201+
202+
If you wish to run this server for testing, [check this issue](https://github.com/netlify/netlify-lambda/issues/49).
203+
204+
If you wish to emulate more Netlify functionality locally, [check this repo](https://github.com/8eecf0d2/netlify-local).
205+
206+
All of the above are community maintained and not officially supported by Netlify.
207+
64208
## License
65209

66210
[MIT](LICENSE)

bin/cmd.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ program.version(pkg.version);
1818
program
1919
.option("-c --config <webpack-config>", "additional webpack configuration")
2020
.option("-p --port <port>", "port to serve from (default: 9000)")
21+
.option("-s --static", "serve pre-built lambda files")
2122

2223
program
2324
.command("serve <dir>")
2425
.description("serve and watch functions")
2526
.action(function(cmd, options) {
26-
console.log("Starting server");
27-
var server = serve.listen(program.port || 9000);
27+
console.log("netlify-lambda: Starting server");
28+
var static = Boolean(program.static);
29+
var server = serve.listen(program.port || 9000, static);
30+
if (static) return // early terminate, don't build
2831
build.watch(cmd, program.config, function(err, stats) {
2932
if (err) {
3033
console.error(err);
@@ -43,7 +46,7 @@ program
4346
.command("build <dir>")
4447
.description("build functions")
4548
.action(function(cmd, options) {
46-
console.log("Building functions");
49+
console.log("netlify-lambda: Building functions");
4750
build
4851
.run(cmd, program.config)
4952
.then(function(stats) {

0 commit comments

Comments
 (0)