Skip to content

Commit ce3b8c8

Browse files
authored
Merge branch 'master' into master
2 parents 654973b + d739bb3 commit ce3b8c8

File tree

6 files changed

+2001
-1573
lines changed

6 files changed

+2001
-1573
lines changed

README.md

Lines changed: 25 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,53 @@ 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+
```
38+
3239
### Proxying for local development
3340

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.
41+
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.
3542

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.
43+
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.
3744

3845
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.
3946

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

4249
```js
4350
module.exports = {
44-
mode: 'development',
51+
mode: "development",
4552
devServer: {
4653
proxy: {
4754
"/.netlify": {
4855
target: "http://localhost:9000",
49-
pathRewrite: {"^/.netlify/functions" : ""}
56+
pathRewrite: { "^/.netlify/functions": "" }
5057
}
5158
}
5259
}
53-
}
60+
};
5461
```
5562

63+
The serving port can be changed with the `-p`/`--port` option.
64+
5665
## Webpack Configuration
5766

5867
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).
5968

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`.
69+
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`.
6170

6271
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.
6372

73+
### babel configuration
74+
75+
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).
76+
77+
However, if any `.babelrc` is found in the directory `netlify-lambda` is run from, it will be used instead of the default one.
78+
6479
## License
6580

6681
[MIT](LICENSE)

bin/cmd.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ program
2424
.command("serve <dir>")
2525
.description("serve and watch functions")
2626
.action(function(cmd, options) {
27-
console.log("Starting server");
27+
console.log("netlify-lambda: Starting server");
2828
var static = Boolean(program.static);
2929
var server = serve.listen(program.port || 9000, static);
30-
if(static) {
31-
return
32-
}
30+
if (static) return // early terminate, don't build
3331
build.watch(cmd, program.config, function(err, stats) {
3432
if (err) {
3533
console.error(err);
@@ -48,7 +46,7 @@ program
4846
.command("build <dir>")
4947
.description("build functions")
5048
.action(function(cmd, options) {
51-
console.log("Building functions");
49+
console.log("netlify-lambda: Building functions");
5250
build
5351
.run(cmd, program.config)
5452
.then(function(stats) {

lib/build.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,27 @@ var webpack = require("webpack");
55
var merge = require("webpack-merge");
66

77
// custom babel target for each node version
8-
function getBabelTarget(envConfig){
8+
function getBabelTarget(envConfig) {
99
var key = "AWS_LAMBDA_JS_RUNTIME";
1010
var runtimes = ["nodejs8.10", "nodejs4.3.2", "nodejs6.10.3"];
1111
var current = envConfig[key] || process.env[key] || "nodejs6.10.3";
1212
var unknown = runtimes.indexOf(current) === -1;
13-
return unknown ? "6.10" : current.replace(/^nodejs/, '');
13+
return unknown ? "6.10" : current.replace(/^nodejs/, "");
1414
}
1515

1616
function webpackConfig(dir, additionalConfig) {
1717
var config = conf.load();
1818
var envConfig = config.build.environment || config.build.Environment || {};
19-
var babelOpts = {cacheDirectory: true};
20-
if (!fs.existsSync(path.join(process.cwd(), '.babelrc'))) {
19+
var babelOpts = { cacheDirectory: true };
20+
if (!fs.existsSync(path.join(process.cwd(), ".babelrc"))) {
2121
babelOpts.presets = [
22-
["env", {
23-
targets: {
24-
node: getBabelTarget(envConfig)
25-
}
26-
}]
22+
["@babel/preset-env", { targets: { node: getBabelTarget(envConfig) } }]
2723
];
24+
2825
babelOpts.plugins = [
29-
"transform-class-properties",
30-
"transform-object-assign",
31-
"transform-object-rest-spread"
26+
"@babel/plugin-proposal-class-properties",
27+
"@babel/plugin-transform-object-assign",
28+
"@babel/plugin-proposal-object-rest-spread"
3229
];
3330
}
3431

@@ -37,16 +34,19 @@ function webpackConfig(dir, additionalConfig) {
3734
var dirPath = path.join(process.cwd(), dir);
3835

3936
if (dirPath === functionsPath) {
40-
throw new Error("Function source and publish folder should be in different locations");
37+
throw new Error(
38+
"Function source and publish folder should be in different locations"
39+
);
4140
}
42-
41+
4342
// Include environment variables from config if available
4443
var defineEnv = {};
45-
Object.keys(envConfig).forEach((key) => {
44+
Object.keys(envConfig).forEach(key => {
4645
defineEnv["process.env." + key] = JSON.stringify(envConfig[key]);
4746
});
48-
47+
4948
var webpackConfig = {
49+
mode: "production",
5050
module: {
5151
rules: [
5252
{
@@ -64,7 +64,7 @@ function webpackConfig(dir, additionalConfig) {
6464
target: "node",
6565
plugins: [
6666
new webpack.IgnorePlugin(/vertx/),
67-
new webpack.DefinePlugin(defineEnv),
67+
new webpack.DefinePlugin(defineEnv)
6868
],
6969
output: {
7070
path: functionsPath,

lib/serve.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ function promiseCallback(promise, callback) {
4545

4646
function createHandler(dir, static) {
4747
return function(request, response) {
48-
var func = request.path.split("/").filter(function(e) {
48+
// handle proxies without path re-writes (http-servr)
49+
var cleanPath = request.path.replace(/^\/.netlify\/functions/, '')
50+
51+
var func = cleanPath.split("/").filter(function(e) {
4952
return e;
5053
})[0];
5154
var module = path.join(process.cwd(), dir, func);
@@ -62,7 +65,7 @@ function createHandler(dir, static) {
6265

6366
var isBase64 =
6467
request.body &&
65-
!(request.headers["content-type"] || "").match(/text|application/);
68+
!(request.headers["content-type"] || "").match(/text|application|multipart\/form-data/);
6669
var lambdaRequest = {
6770
path: request.path,
6871
httpMethod: request.method,
@@ -82,8 +85,8 @@ exports.listen = function(port, static) {
8285
var config = conf.load();
8386
var app = express();
8487
var dir = config.build.functions || config.build.Functions;
85-
app.use(bodyParser.raw());
86-
app.use(bodyParser.text({type: "*/*"}));
88+
app.use(bodyParser.raw({limit: "6mb"}));
89+
app.use(bodyParser.text({limit: "6mb", type: "*/*"}));
8790
app.use(expressLogging(console, {
8891
blacklist: ['/favicon.ico'],
8992
}));

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-lambda",
3-
"version": "0.5.0",
3+
"version": "1.0.1",
44
"description": "Build and serve lambda function with webpack compilation",
55
"homepage": "https://github.com/netlify/netlify-lambda#readme",
66
"main": "bin/cmd.js",
@@ -20,19 +20,19 @@
2020
"url": "https://github.com/netlify/netlify-lambda/issues"
2121
},
2222
"dependencies": {
23-
"babel-core": "^6.26.0",
24-
"babel-loader": "^7.1.2",
25-
"babel-plugin-transform-class-properties": "^6.24.1",
26-
"babel-plugin-transform-object-assign": "^6.22.0",
27-
"babel-plugin-transform-object-rest-spread": "^6.26.0",
28-
"babel-preset-env": "^1.6.1",
23+
"@babel/core": "^7.0.0",
24+
"@babel/preset-env": "^7.0.0",
25+
"@babel/plugin-proposal-class-properties": "^7.0.0",
26+
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
27+
"@babel/plugin-transform-object-assign": "^7.0.0",
28+
"babel-loader": "^8.0.0",
2929
"base-64": "^0.1.0",
30-
"body-parser": "^1.18.2",
31-
"commander": "^2.11.0",
32-
"express": "^4.16.2",
30+
"body-parser": "^1.18.3",
31+
"commander": "^2.17.1",
32+
"express": "^4.16.3",
3333
"express-logging": "^1.1.1",
3434
"toml": "^2.3.3",
35-
"webpack": "^3.8.1",
36-
"webpack-merge": "^4.1.1"
35+
"webpack": "^4.17.1",
36+
"webpack-merge": "^4.1.4"
3737
}
3838
}

0 commit comments

Comments
 (0)