Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Babel 7 and Webpack 4 (fully tested) #63

Merged
merged 2 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ This is a small CLI tool that helps with building or serving lambdas built with

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.

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

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

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).
**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).

## Usage

Expand All @@ -19,7 +19,7 @@ netlify-lambda serve <folder>
netlify-lambda build <folder>
```

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

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

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

### Proxying for local development

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

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

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.

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

```js
module.exports = {
mode: 'development',
mode: "development",
devServer: {
proxy: {
"/.netlify": {
target: "http://localhost:9000",
pathRewrite: {"^/.netlify/functions" : ""}
pathRewrite: { "^/.netlify/functions": "" }
}
}
}
}
};
```

## Webpack Configuration
Expand Down
6 changes: 3 additions & 3 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ program.version(pkg.version);

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

program
.command("serve <dir>")
.description("serve and watch functions")
.action(function(cmd, options) {
console.log("Starting server");
console.log("netlify-lambda: Starting server");
var server = serve.listen(program.port || 9000);
build.watch(cmd, program.config, function(err, stats) {
if (err) {
Expand All @@ -43,7 +43,7 @@ program
.command("build <dir>")
.description("build functions")
.action(function(cmd, options) {
console.log("Building functions");
console.log("netlify-lambda: Building functions");
build
.run(cmd, program.config)
.then(function(stats) {
Expand Down
34 changes: 17 additions & 17 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@ var webpack = require("webpack");
var merge = require("webpack-merge");

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

function webpackConfig(dir, additionalConfig) {
var config = conf.load();
var envConfig = config.build.environment || config.build.Environment || {};
var babelOpts = {cacheDirectory: true};
if (!fs.existsSync(path.join(process.cwd(), '.babelrc'))) {
var babelOpts = { cacheDirectory: true };
if (!fs.existsSync(path.join(process.cwd(), ".babelrc"))) {
babelOpts.presets = [
["env", {
targets: {
node: getBabelTarget(envConfig)
}
}]
["@babel/preset-env", { targets: { node: getBabelTarget(envConfig) } }]
];

babelOpts.plugins = [
"transform-class-properties",
"transform-object-assign",
"transform-object-rest-spread"
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-object-assign",
"@babel/plugin-proposal-object-rest-spread"
];
}

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

if (dirPath === functionsPath) {
throw new Error("Function source and publish folder should be in different locations");
throw new Error(
"Function source and publish folder should be in different locations"
);
}

// Include environment variables from config if available
var defineEnv = {};
Object.keys(envConfig).forEach((key) => {
Object.keys(envConfig).forEach(key => {
defineEnv["process.env." + key] = JSON.stringify(envConfig[key]);
});

var webpackConfig = {
mode: "production",
module: {
rules: [
{
Expand All @@ -64,7 +64,7 @@ function webpackConfig(dir, additionalConfig) {
target: "node",
plugins: [
new webpack.IgnorePlugin(/vertx/),
new webpack.DefinePlugin(defineEnv),
new webpack.DefinePlugin(defineEnv)
],
output: {
path: functionsPath,
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netlify-lambda",
"version": "0.4.0",
"version": "1.0.0",
"description": "Build and serve lambda function with webpack compilation",
"homepage": "https://github.com/netlify/netlify-lambda#readme",
"main": "bin/cmd.js",
Expand All @@ -20,19 +20,19 @@
"url": "https://github.com/netlify/netlify-lambda/issues"
},
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-object-assign": "^7.0.0",
"babel-loader": "^8.0.0",
"base-64": "^0.1.0",
"body-parser": "^1.18.2",
"commander": "^2.11.0",
"express": "^4.16.2",
"body-parser": "^1.18.3",
"commander": "^2.17.1",
"express": "^4.16.3",
"express-logging": "^1.1.1",
"toml": "^2.3.3",
"webpack": "^3.8.1",
"webpack-merge": "^4.1.1"
"webpack": "^4.17.1",
"webpack-merge": "^4.1.4"
}
}
Loading