Skip to content

Commit

Permalink
add babel plugins and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Oct 11, 2018
1 parent 9c4336c commit 38443e0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 23 deletions.
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
28 changes: 18 additions & 10 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +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 = [
["@babel/preset-env", {targets: {node: getBabelTarget(envConfig)}}],
["@babel/preset-env", { targets: { node: getBabelTarget(envConfig) } }]
];

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

Expand All @@ -28,15 +34,17 @@ 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: {
Expand All @@ -56,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
5 changes: 4 additions & 1 deletion 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 @@ -22,6 +22,9 @@
"dependencies": {
"@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.3",
Expand Down
74 changes: 74 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@
"@babel/template" "^7.0.0"
"@babel/types" "^7.0.0"

"@babel/helper-function-name@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.1.0.tgz#a0ceb01685f73355d4360c1247f582bfafc8ff53"
dependencies:
"@babel/helper-get-function-arity" "^7.0.0"
"@babel/template" "^7.1.0"
"@babel/types" "^7.0.0"

"@babel/helper-get-function-arity@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0.tgz#83572d4320e2a4657263734113c42868b64e49c3"
Expand Down Expand Up @@ -151,6 +159,15 @@
"@babel/traverse" "^7.0.0"
"@babel/types" "^7.0.0"

"@babel/helper-replace-supers@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.1.0.tgz#5fc31de522ec0ef0899dc9b3e7cf6a5dd655f362"
dependencies:
"@babel/helper-member-expression-to-functions" "^7.0.0"
"@babel/helper-optimise-call-expression" "^7.0.0"
"@babel/traverse" "^7.1.0"
"@babel/types" "^7.0.0"

"@babel/helper-simple-access@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/helper-simple-access/-/helper-simple-access-7.0.0.tgz#ff36a27983ae4c27122da2f7f294dced80ecbd08"
Expand Down Expand Up @@ -193,6 +210,10 @@
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.0.0.tgz#697655183394facffb063437ddf52c0277698775"

"@babel/parser@^7.1.0", "@babel/parser@^7.1.2":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.1.2.tgz#85c5c47af6d244fab77bce6b9bd830e38c978409"

"@babel/plugin-proposal-async-generator-functions@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.0.0.tgz#5d1eb6b44fd388b97f964350007ab9da090b1d70"
Expand All @@ -201,6 +222,17 @@
"@babel/helper-remap-async-to-generator" "^7.0.0"
"@babel/plugin-syntax-async-generators" "^7.0.0"

"@babel/plugin-proposal-class-properties@^7.0.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.1.0.tgz#9af01856b1241db60ec8838d84691aa0bd1e8df4"
dependencies:
"@babel/helper-function-name" "^7.1.0"
"@babel/helper-member-expression-to-functions" "^7.0.0"
"@babel/helper-optimise-call-expression" "^7.0.0"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/helper-replace-supers" "^7.1.0"
"@babel/plugin-syntax-class-properties" "^7.0.0"

"@babel/plugin-proposal-json-strings@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-json-strings/-/plugin-proposal-json-strings-7.0.0.tgz#3b4d7b5cf51e1f2e70f52351d28d44fc2970d01e"
Expand Down Expand Up @@ -236,6 +268,12 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-class-properties@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-class-properties/-/plugin-syntax-class-properties-7.0.0.tgz#e051af5d300cbfbcec4a7476e37a803489881634"
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-json-strings@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.0.0.tgz#0d259a68090e15b383ce3710e01d5b23f3770cbd"
Expand Down Expand Up @@ -381,6 +419,12 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-transform-object-assign@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-assign/-/plugin-transform-object-assign-7.0.0.tgz#fca6d7500d9675c42868b8f3882979201b9a5ad8"
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-transform-object-super@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-super/-/plugin-transform-object-super-7.0.0.tgz#b8587d511309b3a0e96e9e38169908b3e392041e"
Expand Down Expand Up @@ -496,6 +540,14 @@
"@babel/parser" "^7.0.0"
"@babel/types" "^7.0.0"

"@babel/template@^7.1.0":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.1.2.tgz#090484a574fef5a2d2d7726a674eceda5c5b5644"
dependencies:
"@babel/code-frame" "^7.0.0"
"@babel/parser" "^7.1.2"
"@babel/types" "^7.1.2"

"@babel/traverse@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0.tgz#b1fe9b6567fdf3ab542cfad6f3b31f854d799a61"
Expand All @@ -510,6 +562,20 @@
globals "^11.1.0"
lodash "^4.17.10"

"@babel/traverse@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.1.0.tgz#503ec6669387efd182c3888c4eec07bcc45d91b2"
dependencies:
"@babel/code-frame" "^7.0.0"
"@babel/generator" "^7.0.0"
"@babel/helper-function-name" "^7.1.0"
"@babel/helper-split-export-declaration" "^7.0.0"
"@babel/parser" "^7.1.0"
"@babel/types" "^7.0.0"
debug "^3.1.0"
globals "^11.1.0"
lodash "^4.17.10"

"@babel/types@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0.tgz#6e191793d3c854d19c6749989e3bc55f0e962118"
Expand All @@ -518,6 +584,14 @@
lodash "^4.17.10"
to-fast-properties "^2.0.0"

"@babel/types@^7.1.2":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.1.2.tgz#183e7952cf6691628afdc2e2b90d03240bac80c0"
dependencies:
esutils "^2.0.2"
lodash "^4.17.10"
to-fast-properties "^2.0.0"

"@webassemblyjs/[email protected]":
version "1.5.13"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.5.13.tgz#81155a570bd5803a30ec31436bc2c9c0ede38f25"
Expand Down

0 comments on commit 38443e0

Please sign in to comment.