What is serverless-webpack?
The serverless-webpack npm package is a Serverless Framework plugin that integrates Webpack to optimize and bundle your Serverless functions. It allows you to use Webpack's powerful features like tree shaking, code splitting, and module resolution to create optimized and efficient Serverless applications.
What are serverless-webpack's main functionalities?
Bundling
This feature allows you to bundle your Serverless functions using Webpack. The provided code sample demonstrates a basic Webpack configuration for bundling a handler.js file.
module.exports = {
entry: './handler.js',
target: 'node',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
},
output: {
libraryTarget: 'commonjs2',
path: path.resolve(__dirname, '.webpack'),
filename: 'handler.js'
}
};
Tree Shaking
Tree shaking is a feature that eliminates dead code from your bundle. The code sample shows how to enable tree shaking by setting the mode to 'production' and using the 'usedExports' optimization option.
module.exports = {
mode: 'production',
entry: './handler.js',
optimization: {
usedExports: true
},
output: {
libraryTarget: 'commonjs2',
path: path.resolve(__dirname, '.webpack'),
filename: 'handler.js'
}
};
Code Splitting
Code splitting allows you to split your code into separate bundles, which can be loaded on demand. The code sample demonstrates how to configure Webpack to create separate bundles for 'handler.js' and 'anotherModule.js'.
module.exports = {
entry: {
handler: './handler.js',
anotherModule: './anotherModule.js'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, '.webpack')
}
};
Module Resolution
Module resolution allows you to create aliases for directories, making it easier to import modules. The code sample shows how to create an alias '@utils' for the 'src/utils/' directory.
module.exports = {
resolve: {
alias: {
'@utils': path.resolve(__dirname, 'src/utils/')
}
},
entry: './handler.js',
output: {
libraryTarget: 'commonjs2',
path: path.resolve(__dirname, '.webpack'),
filename: 'handler.js'
}
};
Other packages similar to serverless-webpack
serverless-bundle
The serverless-bundle package is a Serverless Framework plugin that uses Webpack and Babel to bundle your functions. It simplifies the setup process by providing a pre-configured Webpack and Babel setup. Compared to serverless-webpack, serverless-bundle is more opinionated and easier to set up, but offers less flexibility for custom configurations.
serverless-plugin-typescript
The serverless-plugin-typescript package is a Serverless Framework plugin that compiles your TypeScript code to JavaScript before deployment. It uses the TypeScript compiler (tsc) instead of Webpack. Compared to serverless-webpack, it is more focused on TypeScript projects and does not offer the same level of optimization and bundling features.
serverless-esbuild
The serverless-esbuild package is a Serverless Framework plugin that uses esbuild to bundle and minify your functions. Esbuild is known for its speed and efficiency. Compared to serverless-webpack, serverless-esbuild offers faster build times but may lack some of the advanced features and plugins available in Webpack.
Serverless Webpack

A Serverless v1.0 plugin to build your lambda functions with Webpack.
This plugin is for you if you want to use the latest Javascript version with Babel;
use custom resource loaders;
try your lambda functions locally and much more!
Install
npm install serverless-webpack
Add the plugin to your serverless.yml
file:
plugins:
- serverless-webpack
Configure
By default the plugin will look for a webpack.config.js
in the service directory.
In alternative you can specify a different file or configuration in the serverless.yml
with:
custom:
webpack: ./folder/my-webpack.config.js
An base Webpack configuration might look like this:
module.exports = {
entry: './handler.js',
target: 'node',
module: {
loaders: [ ... ]
}
};
Note that, if the output
configuration is not set, it will automatically be
generated to write bundles in the .webpack
directory. If you set your own output
configuration make sure to add a libraryTarget
for best compatibility with external dependencies:
module.exports = {
output: {
libraryTarget: 'commonjs',
path: '.webpack',
filename: 'handler.js',
},
};
By default, the plugin will try to bundle all dependencies. However, you don't
want to include all modules in some cases such as selectively import, excluding
builtin package (ie: aws-sdk
) and handling webpack-incompatible modules.
In this case you might add external modules in
Webpack externals
configuration.
Those modules can be included in the Serverless bundle with the webpackIncludeModules
option in serverless.yml
:
var nodeExternals = require('webpack-node-externals')
modules.export = {
externals: [nodeExternals()],
}
custom:
webpackIncludeModules: true
All modules stated in externals
will be excluded from bundled files. If an excluded module
is stated as dependencies
in package.json
, it will be packed into the Serverless
artifact under the node_modules
directory.
By default, the plugin will use the package.json
file in working directory, If you want to
use a different package conf, set packagePath
to your custom package.json. eg:
custom:
webpackIncludeModules:
packagePath: '../package.json'
Note that only relative path is supported at the moment.
You can find an example setups in the examples
folder.
Usage
Automatic bundling
The normal Serverless deploy procedure will automatically bundle with Webpack:
- Create the Serverless project with
serverless create -t aws-node
- Install Serverless Webpack as above
- Deploy with
serverless deploy
Simulate API Gateway locally
To start a local server that will act like the API Gateway use the following command.
Your code will be reloaded upon change so that every request to your local server
will serve the latest code.
serverless webpack serve
Options are:
--port
or -p
(optional) The local server port. Defaults to 8000
The serve
command will automatically look for the local serverless.yml
and serve
all the http
events. For example this configuration will generate a GET enpoint:
functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello
Run a function locally
To run your bundled functions locally you can:
serverless webpack invoke --function <function-name>
Options are:
--function
or -f
(required) is the name of the function to run--path
or -p
(optional) is a JSON file path used as the function input event
Run a function locally on source changes
Or to run a function every time the source files change use watch
:
serverless webpack watch --function <function-name> --path event.json
Options are:
--function
or -f
(required) is the name of the function to run--path
or -p
(optional) is a JSON file path used as the function input event
Bundle with webpack
To just bundle and see the output result use:
serverless webpack --out dist
Options are:
--out
or -o
(optional) The output directory. Defaults to .webpack
.
Example with Babel
In the examples
folder there is a Serverless project using this
plugin with Babel. To try it, from inside the example folder:
npm install
to install dependenciesserverless webpack run -f hello
to run the example function