A Serverless v1.0 plugin that uses Browserify to bundle your NodeJS Lambda functions.
Why? Lambda's with smaller code start and run faster. Lambda also has an account wide deployment package size limit.
aws-sdk-js now officially supports browserify. Read more about why this kicks ass on my blog.
With the example package.json
and javascript code below, the default packaging for NodeJs lambdas in Serverless produces a zip file that is 11.3 MB, because it blindly includes all of node_modules
in the zip.
This plugin with 2 lines of configuration produces a zip file that is 400KB!
...
"dependencies": {
"aws-sdk": "^2.6.12",
"moment": "^2.15.2",
"request": "^2.75.0",
"rxjs": "^5.0.0-rc.1"
},
...
const Rx = require('rxjs/Rx');
const request = require('request');
...
From your serverless project run:
npm install serverless-plugin-browserify --save-dev
Add the plugin to your serverless.yml
file and set package.individually
to true
:
plugins:
- serverless-plugin-browserify
package:
individually: true
package.individually
is required because it makes configuration more straight forward, and if you are not packaging individually size is not a concern of yours in the 1st place.
For most use cases you should NOT need to do any configuration. If you are a code ninja, read on.
The base config for browserify is read from the custom.browserify
section of serverless.yml
. All browserify options are supported (most are auto configured by this plugin). This plugin adds one special option disable
which if true
will bypass this plugin.
The base config can be over-ridden on a function by function basis. Again custom.browserify
is not required and should not even need to be defined in most cases.
custom:
browserify:
#any option defined in https://github.com/substack/node-browserify#browserifyfiles--opts
functions:
usersGet:
name: ${self:provider.stage}-${self:service}-pageGet
description: get user
handler: users/handler.hello
browserify:
noParse:
- ./someBig.json #browserify can't optimize json, will take long time to parse for nothing
Note: package.include
can be used with this plugin. All other options can be handled by leveraging browserify options in your serverless.yml
custom browserify
section.
When this plugin is enabled, and package.individually
is true
, running serverless deploy
and serverless deploy -f <funcName>
will automatically browserify your node lambda code.
If you want to see output of bundled file or zip simply set SLS_DEBUG
. Ex (using Fish Shell): env SLS_DEBUG=true sls deploy function -v -f usersGet
Also check out the examples directory
Run serverless browserify -f <functionName>
. You can optionally dictate where the bundling output dir is by using the -o
flag. Ex: sls browserify -o /tmp/test -f pageUpdate
.
- Should I use Webpack instead of this plugin? I prefer Browserify over webpack because I have found it supports more modules, optimizes better, and requires less configuration.
- Why is UglifyJS not built-in? No ES6 support. Issue been open since 2014.
- My code is not bundling correctly The bundled code is always stored in a tmp dir on your computer. Set
SLS_DEBUG=true
then re-run your command to output the directory. Fish Shell ex:env SLS_DEBUG=true sls browserify