Skip to content

Commit 98fbb45

Browse files
authored
Merge pull request netlify#63 from netlify/upgrade
Upgrade to Babel 7 and Webpack 4 (fully tested)
2 parents f12105b + 38443e0 commit 98fbb45

File tree

5 files changed

+1978
-1566
lines changed

5 files changed

+1978
-1566
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

+17-17
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,

package.json

+12-12
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",
@@ -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)