@sanjo/serverless-webpack-plugin
v0.5.0
Published
Serverless Webpack Plugin - Significantly reduces Lambda file size and improves performance
Downloads
6
Maintainers
Readme
Serverless Webpack Plugin
Bundles your Serverless handlers with Webpack.
Note: Requires Serverless v1.x
Setup
- Install the plugin and webpack in the root of your Serverless Project:
npm install @sanjo/serverless-webpack-plugin webpack@^1.x webpack-node-externals babel-loader json-loader babel-polyfill source-map-support --save-dev
Usage
- Add the plugin to the
plugins
array inserverless.yml
:
plugins:
- serverless-webpack-plugin
Webpack config
This plugin allows you to completely customize how your code is optimized by specifying your own webpack config. Here is an example webpack.config.js
for the common use case:
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals')
module.exports = {
// The lambda entry will be provided and added by serverless
entry: [
'source-map-support/register',
'babel-polyfill'
],
// output: provided by serverless
target: 'node',
node: {
__dirname: true,
__filename: true,
},
externals: [
nodeExternals(),
],
resolve: {
extensions: ['', '.js', '.json']
},
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
},
{
test: /\.json$/,
loader: 'json-loader',
},
]
}
};
Loading additional modules before the lambda function module
If you need to load modules before your lambda function module is loaded,
you can specify those modules with entry option in your webpack config.
For example if you need to load the babel-polyfill, you can do that
by adding entry: ['babel-polyfill']
to your webpack config.
This will first load the babel-polyfill module and then your lambda function module.