Skip to content

Commit 38443e0

Browse files
author
sw-yx
committed
add babel plugins and update readme
1 parent 9c4336c commit 38443e0

File tree

5 files changed

+108
-23
lines changed

5 files changed

+108
-23
lines changed

README.md

+9-9
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

@@ -31,26 +31,26 @@ The `build` function will run a single build of the functions in the folder.
3131

3232
### Proxying for local development
3333

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.
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.
3535

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.
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.
3737

3838
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.
3939

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

4242
```js
4343
module.exports = {
44-
mode: 'development',
44+
mode: "development",
4545
devServer: {
4646
proxy: {
4747
"/.netlify": {
4848
target: "http://localhost:9000",
49-
pathRewrite: {"^/.netlify/functions" : ""}
49+
pathRewrite: { "^/.netlify/functions": "" }
5050
}
5151
}
5252
}
53-
}
53+
};
5454
```
5555

5656
## Webpack Configuration

bin/cmd.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ program.version(pkg.version);
1717

1818
program
1919
.option("-c --config <webpack-config>", "additional webpack configuration")
20-
.option("-p --port <port>", "port to serve from (default: 9000)")
20+
.option("-p --port <port>", "port to serve from (default: 9000)");
2121

2222
program
2323
.command("serve <dir>")
2424
.description("serve and watch functions")
2525
.action(function(cmd, options) {
26-
console.log("Starting server");
26+
console.log("netlify-lambda: Starting server");
2727
var server = serve.listen(program.port || 9000);
2828
build.watch(cmd, program.config, function(err, stats) {
2929
if (err) {
@@ -43,7 +43,7 @@ program
4343
.command("build <dir>")
4444
.description("build functions")
4545
.action(function(cmd, options) {
46-
console.log("Building functions");
46+
console.log("netlify-lambda: Building functions");
4747
build
4848
.run(cmd, program.config)
4949
.then(function(stats) {

lib/build.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -5,21 +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-
["@babel/preset-env", {targets: {node: getBabelTarget(envConfig)}}],
22+
["@babel/preset-env", { targets: { node: getBabelTarget(envConfig) } }]
23+
];
24+
25+
babelOpts.plugins = [
26+
"@babel/plugin-proposal-class-properties",
27+
"@babel/plugin-transform-object-assign",
28+
"@babel/plugin-proposal-object-rest-spread"
2329
];
2430
}
2531

@@ -28,15 +34,17 @@ function webpackConfig(dir, additionalConfig) {
2834
var dirPath = path.join(process.cwd(), dir);
2935

3036
if (dirPath === functionsPath) {
31-
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+
);
3240
}
33-
41+
3442
// Include environment variables from config if available
3543
var defineEnv = {};
36-
Object.keys(envConfig).forEach((key) => {
44+
Object.keys(envConfig).forEach(key => {
3745
defineEnv["process.env." + key] = JSON.stringify(envConfig[key]);
3846
});
39-
47+
4048
var webpackConfig = {
4149
mode: "production",
4250
module: {
@@ -56,7 +64,7 @@ function webpackConfig(dir, additionalConfig) {
5664
target: "node",
5765
plugins: [
5866
new webpack.IgnorePlugin(/vertx/),
59-
new webpack.DefinePlugin(defineEnv),
67+
new webpack.DefinePlugin(defineEnv)
6068
],
6169
output: {
6270
path: functionsPath,

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-lambda",
3-
"version": "0.4.0",
3+
"version": "1.0.0",
44
"description": "Build and serve lambda function with webpack compilation",
55
"homepage": "https://github.com/netlify/netlify-lambda#readme",
66
"main": "bin/cmd.js",
@@ -22,6 +22,9 @@
2222
"dependencies": {
2323
"@babel/core": "^7.0.0",
2424
"@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",
2528
"babel-loader": "^8.0.0",
2629
"base-64": "^0.1.0",
2730
"body-parser": "^1.18.3",

yarn.lock

+74
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@
8181
"@babel/template" "^7.0.0"
8282
"@babel/types" "^7.0.0"
8383

84+
"@babel/helper-function-name@^7.1.0":
85+
version "7.1.0"
86+
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.1.0.tgz#a0ceb01685f73355d4360c1247f582bfafc8ff53"
87+
dependencies:
88+
"@babel/helper-get-function-arity" "^7.0.0"
89+
"@babel/template" "^7.1.0"
90+
"@babel/types" "^7.0.0"
91+
8492
"@babel/helper-get-function-arity@^7.0.0":
8593
version "7.0.0"
8694
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0.tgz#83572d4320e2a4657263734113c42868b64e49c3"
@@ -151,6 +159,15 @@
151159
"@babel/traverse" "^7.0.0"
152160
"@babel/types" "^7.0.0"
153161

162+
"@babel/helper-replace-supers@^7.1.0":
163+
version "7.1.0"
164+
resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.1.0.tgz#5fc31de522ec0ef0899dc9b3e7cf6a5dd655f362"
165+
dependencies:
166+
"@babel/helper-member-expression-to-functions" "^7.0.0"
167+
"@babel/helper-optimise-call-expression" "^7.0.0"
168+
"@babel/traverse" "^7.1.0"
169+
"@babel/types" "^7.0.0"
170+
154171
"@babel/helper-simple-access@^7.0.0":
155172
version "7.0.0"
156173
resolved "https://registry.yarnpkg.com/@babel/helper-simple-access/-/helper-simple-access-7.0.0.tgz#ff36a27983ae4c27122da2f7f294dced80ecbd08"
@@ -193,6 +210,10 @@
193210
version "7.0.0"
194211
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.0.0.tgz#697655183394facffb063437ddf52c0277698775"
195212

213+
"@babel/parser@^7.1.0", "@babel/parser@^7.1.2":
214+
version "7.1.2"
215+
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.1.2.tgz#85c5c47af6d244fab77bce6b9bd830e38c978409"
216+
196217
"@babel/plugin-proposal-async-generator-functions@^7.0.0":
197218
version "7.0.0"
198219
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.0.0.tgz#5d1eb6b44fd388b97f964350007ab9da090b1d70"
@@ -201,6 +222,17 @@
201222
"@babel/helper-remap-async-to-generator" "^7.0.0"
202223
"@babel/plugin-syntax-async-generators" "^7.0.0"
203224

225+
"@babel/plugin-proposal-class-properties@^7.0.0":
226+
version "7.1.0"
227+
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.1.0.tgz#9af01856b1241db60ec8838d84691aa0bd1e8df4"
228+
dependencies:
229+
"@babel/helper-function-name" "^7.1.0"
230+
"@babel/helper-member-expression-to-functions" "^7.0.0"
231+
"@babel/helper-optimise-call-expression" "^7.0.0"
232+
"@babel/helper-plugin-utils" "^7.0.0"
233+
"@babel/helper-replace-supers" "^7.1.0"
234+
"@babel/plugin-syntax-class-properties" "^7.0.0"
235+
204236
"@babel/plugin-proposal-json-strings@^7.0.0":
205237
version "7.0.0"
206238
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-json-strings/-/plugin-proposal-json-strings-7.0.0.tgz#3b4d7b5cf51e1f2e70f52351d28d44fc2970d01e"
@@ -236,6 +268,12 @@
236268
dependencies:
237269
"@babel/helper-plugin-utils" "^7.0.0"
238270

271+
"@babel/plugin-syntax-class-properties@^7.0.0":
272+
version "7.0.0"
273+
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-class-properties/-/plugin-syntax-class-properties-7.0.0.tgz#e051af5d300cbfbcec4a7476e37a803489881634"
274+
dependencies:
275+
"@babel/helper-plugin-utils" "^7.0.0"
276+
239277
"@babel/plugin-syntax-json-strings@^7.0.0":
240278
version "7.0.0"
241279
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.0.0.tgz#0d259a68090e15b383ce3710e01d5b23f3770cbd"
@@ -381,6 +419,12 @@
381419
dependencies:
382420
"@babel/helper-plugin-utils" "^7.0.0"
383421

422+
"@babel/plugin-transform-object-assign@^7.0.0":
423+
version "7.0.0"
424+
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-assign/-/plugin-transform-object-assign-7.0.0.tgz#fca6d7500d9675c42868b8f3882979201b9a5ad8"
425+
dependencies:
426+
"@babel/helper-plugin-utils" "^7.0.0"
427+
384428
"@babel/plugin-transform-object-super@^7.0.0":
385429
version "7.0.0"
386430
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-super/-/plugin-transform-object-super-7.0.0.tgz#b8587d511309b3a0e96e9e38169908b3e392041e"
@@ -496,6 +540,14 @@
496540
"@babel/parser" "^7.0.0"
497541
"@babel/types" "^7.0.0"
498542

543+
"@babel/template@^7.1.0":
544+
version "7.1.2"
545+
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.1.2.tgz#090484a574fef5a2d2d7726a674eceda5c5b5644"
546+
dependencies:
547+
"@babel/code-frame" "^7.0.0"
548+
"@babel/parser" "^7.1.2"
549+
"@babel/types" "^7.1.2"
550+
499551
"@babel/traverse@^7.0.0":
500552
version "7.0.0"
501553
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0.tgz#b1fe9b6567fdf3ab542cfad6f3b31f854d799a61"
@@ -510,6 +562,20 @@
510562
globals "^11.1.0"
511563
lodash "^4.17.10"
512564

565+
"@babel/traverse@^7.1.0":
566+
version "7.1.0"
567+
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.1.0.tgz#503ec6669387efd182c3888c4eec07bcc45d91b2"
568+
dependencies:
569+
"@babel/code-frame" "^7.0.0"
570+
"@babel/generator" "^7.0.0"
571+
"@babel/helper-function-name" "^7.1.0"
572+
"@babel/helper-split-export-declaration" "^7.0.0"
573+
"@babel/parser" "^7.1.0"
574+
"@babel/types" "^7.0.0"
575+
debug "^3.1.0"
576+
globals "^11.1.0"
577+
lodash "^4.17.10"
578+
513579
"@babel/types@^7.0.0":
514580
version "7.0.0"
515581
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0.tgz#6e191793d3c854d19c6749989e3bc55f0e962118"
@@ -518,6 +584,14 @@
518584
lodash "^4.17.10"
519585
to-fast-properties "^2.0.0"
520586

587+
"@babel/types@^7.1.2":
588+
version "7.1.2"
589+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.1.2.tgz#183e7952cf6691628afdc2e2b90d03240bac80c0"
590+
dependencies:
591+
esutils "^2.0.2"
592+
lodash "^4.17.10"
593+
to-fast-properties "^2.0.0"
594+
521595
"@webassemblyjs/[email protected]":
522596
version "1.5.13"
523597
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.5.13.tgz#81155a570bd5803a30ec31436bc2c9c0ede38f25"

0 commit comments

Comments
 (0)