Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Korzunin committed May 13, 2020
0 parents commit 0815efb
Show file tree
Hide file tree
Showing 14 changed files with 1,910 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
19 changes: 19 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
env:
node: true
extends:
- eslint:recommended
- plugin:@typescript-eslint/eslint-recommended
- plugin:@typescript-eslint/recommended
rules:
semi: 'off'
quotes: 'off'
no-use-before-define: 'off'
'@typescript-eslint/explicit-function-return-type': 'off'
'@typescript-eslint/semi': error
'@typescript-eslint/quotes':
- error
- single
'@typescript-eslint/no-use-before-define':
- error
- functions: false
classes: false
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.DS_Store
.idea
.serverless
dist
*.log
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- '12'
- '10'
- '8'

# after_success:
# - npm i -g semantic-release
# - semantic-release

branches:
except:
- /^v\d+\.\d+\.\d+$/

notifications:
email: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Victor Korzunin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# serverless-esbuild
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![npm version](https://badge.fury.io/js/serverless-esbuild.svg)](https://badge.fury.io/js/serverless-esbuild) [![Build Status](https://travis-ci.org/floydspace/serverless-esbuild.svg?branch=master)](https://travis-ci.org/floydspace/serverless-esbuild)

Serverless plugin for zero-config JavaScript and TypeScript code bundling using promising fast & furious [`esbuild`](https://github.com/evanw/esbuild) bundler and minifier

## Features

* Zero-config: Works out of the box without the need to install any other compiler or plugins
* Supports ES2015 syntax + features (`export`, `import`, `async`, `await`, `Promise`, ...)
* Supports `sls package`, `sls deploy` and `sls deploy function`
* Supports `sls invoke local`
* Integrates nicely with [`serverless-offline`](https://github.com/dherault/serverless-offline)

## Install

```sh
yarn add --dev serverless-esbuild
# or
npm install -D serverless-esbuild
```

Add the following plugin to your `serverless.yml`:

```yaml
plugins:
- serverless-esbuild
```
## Configure
By default, no configuration required, but you can change esbuild behavior in custom `esbuild` section in `serverless.yaml` config:

```yml
custom:
esbuild:
bundle: true
minify: false
```

Check [esbuild](https://github.com/evanw/esbuild#command-line-usage) documentation for the full list of available options. Note that some options like `entryPoints` or `outdir` cannot be overwritten.

See [example folder](example) for a minimal example.

### Including extra files

All files from `package/include` will be included in the final build file. See [Exclude/Include](https://serverless.com/framework/docs/providers/aws/guide/packaging#exclude--include)


## Usage

### Automatic compilation

The normal Serverless deploy procedure will automatically compile with `esbuild`:

- Create the Serverless project with `serverless create -t aws-nodejs`
- Install Serverless esbuild plugin as above
- Deploy with `serverless deploy`

### Usage with serverless-offline

The plugin integrates very well with [serverless-offline](https://github.com/dherault/serverless-offline) to
simulate AWS Lambda and AWS API Gateway locally.

Add the plugins to your `serverless.yml` file and make sure that `serverless-esbuild`
precedes `serverless-offline` as the order is important:
```yaml
plugins:
...
- serverless-esbuild
...
- serverless-offline
...
```

Run `serverless offline` or `serverless offline start` to start the Lambda/API simulation.

In comparison to `serverless offline`, the `start` command will fire an `init` and a `end` lifecycle hook which is needed for `serverless-offline` and e.g. `serverless-dynamodb-local` to switch off resources (see below)

#### serverless-dynamodb-local

Configure your service the same as mentioned above, but additionally add the `serverless-dynamodb-local`
plugin as follows:
```yaml
plugins:
- serverless-esbuild
- serverless-dynamodb-local
- serverless-offline
```

Run `serverless offline start`.

### Run a function locally

To run your compiled functions locally you can:

```bash
$ serverless invoke local --function <function-name>
```

Options are:

- `--function` or `-f` (required) is the name of the function to run
- `--path` or `-p` (optional) path to JSON or YAML file holding input data
- `--data` or `-d` (optional) input data

## Author

[Victor Korzunin](https://floydspace.github.io/)

Inspired by [serverless-plugin-typescript](https://github.com/prisma-labs/serverless-plugin-typescript)
21 changes: 21 additions & 0 deletions example/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as _ from 'lodash';

// modern module syntax
export async function hello(event, context, callback) {

// dependencies work as expected
console.log(_.VERSION);

// async/await also works out of the box
await new Promise((resolve, reject) => setTimeout(resolve, 500));

const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};

callback(null, response);
}
11 changes: 11 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"main": "handler.js",
"dependencies": {
"lodash": "^4.17.4"
},
"devDependencies": {
"@types/lodash": "4.14.91",
"@types/node": "^11.13.0",
"serverless-esbuild": "1.0.0"
}
}
16 changes: 16 additions & 0 deletions example/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
service: serverless-example

plugins:
- serverless-esbuild

provider:
name: aws
runtime: nodejs12.x

functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
Loading

0 comments on commit 0815efb

Please sign in to comment.