*** WARNING ***
This is a beta
release under heavy development, use with caution.
This plugin is intended to add flexibility to the serverless build process for nodejs, under serverless 1.0 including an 0.5 compatibility layer thanks to zyrorl.
Currently in testing state. To try it:
npm i
cd test
npm i
export AWS_PROFILE=default
sls deploy
sls deploy function -f one
sls invoke -f one --log
sls deploy function -f one --method=file
sls invoke -f one --log
After running the above you should have built the same function in each of the two methods successfully.
Run: npm i --save serverless-build-plugin
Add the plugin to serverless.yml
under plugins
eg.
service: my-service
provider:
name: aws
runtime: nodejs4.3
plugins:
- serverless-build-plugin
package:
exclude:
- node_modules
functions:
one:
handler: functions/one/handler.handler
Then create the serverless.build.yml
as per Configuration.
Run: npm i --save serverless-build-plugin
Modify s-project.json
to add serverless-build-plugin
to the plugins array.
ie.
{
"name": "example-serverless-project",
"plugins": [
"serverless-build-plugin"
],
"description": "My Serverless Project",
"version": "0.0.1",
"profile": "serverless-v0.5.5"
}
Then create the serverless.build.yml
as per Configuration.
There are two ways to build:
- Bundling
- This will preserve the existing source filestructure and potentially any non-excluded files.
- Optionally, each JS file in babel'ed and uglified individually
- Build Files
- With this method, specify a file (or let it default to a
webpack.config.js
) - The file can return a string, a stream or a
webpack.config.js
object - If the file exports a default function, its return value will instead be used, as above
- Webpack configs will be executed, and their output injected into the zip, including
externals
modules - Strings & streams will be piped into a
handler.js
- With this method, specify a file (or let it default to a
There are three ways to configure the plugin:
- Create a
serverless.build.yml
inside the project directory - Populate
custom.build
inserverless.yml
- The commandline
sls deploy function -f one --file=./buildFile.js
# ./serverless.build.yml
method: "bundle"
sourceMaps : true
babel : true # Will use ./.babelrc
uglify : true # Will use defaults
exclude:
- "*" # Ignores the root directory
modules:
exclude: # excluded from the root node_modules
- aws-sdk
deepExclude: # excluded from nested node_modules
- aws-sdk
functions:
one:
include:
- "functions/one/**"
- "lib/one/**"
exclude:
- "**/*.json"
babel
- Can be
true
, will search for a.babelrc
in the project directory - Can be a babelrc-like object
- Can be
uglify
- When
true
, will uglify source & modules - Can be an options object, passed to uglify
- When
method
- When
bundle
, will use bundling method - When
file
, will use a build file or webpack config
- When
file
- Path to a build file or webpack config
- serverless 1.0 still doesn't support packaging single functions
sls deploy
will currently take a while due to packaging everything for the first deployment.- This is a bug due to not being able to hook into it.
- [email protected] support
-
sls deploy
currently broken- Awaiting upstream bug fix
- Make ServerlessBuildPlugin work on a per-function workflow
- Awaiting upstream pull requests
- Bundling based builds
- Source code
- Minified with uglifyjs, includes .map
- Babelifie, includes .map
- node_modules
- Recursive resolution of non-dev dependencies
- Also includes "meta" packages
- Minified with uglifyjs
- Recursive resolution of non-dev dependencies
- Source code
- File based builds
- Webpack
- Partiy with
severless-webpack-plugin
- External node_modules are included
- Partiy with
- Build files
- Can inherit a webpack config
- Can pipe buffers and streams to a handler.js
- Webpack
- Performs cleanup
- Optimizations & refactors
- Refactor out the plugin inheritence dependency
- ModuleBundler
- SourceBundler
- FileBuild
- Webpack
- Babel
- Uglify
- Use serverless' built in yaml parser (which resolve recursively)
- Requires an intialization step
- Change transforms to write to file instead of memory
- This could bloat memory quite a bit as it stands
- Make
webpack
andbabel-core
peer dependencies - Inline source maps for uglifyjs
- Refactor out the plugin inheritence dependency