An extension of the serverless-webpack plugin. This plugin bundles your Node.js Lambda functions with sensible defaults so you don't have to maintain your own Webpack configs.
- Linting via ESLint
- Caching for faster builds
- Use ES6
- Supports transpiling unit tests with babel-jest
- Source map support for proper error messages
And all this works without having to install Webpack, Babel, ESLint, etc. or manage any of their configs. Simply add serverless-bundle to your app and you are done!
- "eslint"- "webpack"- "@babel/core"- "babel-eslint"- "babel-loader"- "eslint-loader"- "@babel/runtime"- "@babel/preset-env"- "serverless-webpack"- "source-map-support"- "webpack-node-externals"- "eslint-config-strongloop"- "@babel/plugin-transform-runtime"- "babel-plugin-source-map-support"+ "serverless-bundle"
serverless-bundle plugin using:
$ npm install --save-dev serverless-bundle
Then add it to your
To run your tests using the same Babel config used in the plugin add the following to your
Once installed and added to your
serverless.yml, serverless-bundle will automatically package your functions using Webpack when you run the various serverless commands.
You can configure the following through your
custom:bundle:sourcemaps: true # Enable source mapscaching: true # Enable Webpack cachingstats: false # Don't print out any Webpack outputlinting: true # Enable linting as a part of the build processforceInclude: # Optional list of NPM packages that need to be included- mysql # Only necessary if packages are included dynamicallyignorePackages: # Ignore building any of the following packages- hiredis # For ex, hiredis needs to be ignored if using rediscopyFiles: # Copy any additional files to the generated package- from: 'public/*' # Where the files are currentlyto: './' # Where in the package should they gopackager: npm # Specify a packager, 'npm' or 'yarn'. Defaults to 'npm'.packagerOptions: # Run a custom script in the package processscripts: #- echo hello > test
This plugin uses eslint-config-strongloop. You can override this by placing your own
.eslintrc.jsonwith the rules you'd like to use. If you'd like to ignore specific files, you can use a
Customizing Babel and Webpack configs
This plugin does not support customizing the Babel and Webpack configs, since serverless-webpack does a pretty good job with that. However, if you think the default config is missing some key features, feel free to open an issue about it.
packagerOptions.scriptsoption allows serverless-webpack to run a custom script in the packaging process. This is useful for installing any platform specific binaries. See below for the
Usage with WebStorm
Here is some info on how to get this plugin to support running tests in WebStorm — https://github.com/AnomalyInnovations/serverless-bundle/issues/5#issuecomment-582237396
Alternative Jest Result Processor
For CI services (like Atlassian Bamboo CI) that do not work with Jest test results, start by installing jest-mocha-reporter.
To set the
"testResultsProcessor": "jest-mocha-reporter"to the Jest section in your
package.json. You should see the default command line output when running
npm run test, but you should also get a
To test the
"reporters": ["jest-mocha-reporter"]instead. This should result in the same file as above but without the command line output.
Package Specific Config
The packages below need some additional config to make them work.
The knex.js module is automatically excluded from the bundle since it's not compatible with Webpack. However, you need to force include the specific database provider package since these are dynamically included. Use the
forceInclude option to pass in a list of packages that you want included. For example, to include
mysql use the following:
The sharp package needs to include a specific binary before package. Use the
packagerOptions.scripts for this.
custom:bundle:packagerOptions:scripts:- rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux --target=10.15.0 sharp
The pg package optionally includes
pg-native that needs to be ignored from Webpack. Use the
ignorePackages option to do this.
The redis package optionally includes
hiredis that needs to be ignored from Webpack. Use the
ignorePackages option to do this.
And in your Lambda code:
const sequelize =processenvDB_NAMEprocessenvDB_USERNAMEprocessenvDB_PASSWORDhost: processenvDB_HOSTdialect: processenvDB_DIALECTdialectModule: pg;
It's common in Serverless monorepo setups that the plugins are installed at the root level and referenced in the individual services. Take the following project setup:
package.json // Here serverless-bundle is installed /service1 |- package.json // Can run npm test from here, referring to parent `package.json` |- handler.js |- handler.test.js |- serverless.yml // Uses serverless-bundle plugin /service2 |- package.json // Can run npm test from here, referring to parent `package.json` |- handler.js |- handler.test.js |- serverless.yml // Uses serverless-bundle plugin
Running Serverless commands (
package, etc.) from the services directories are supported out of the box. To get your tests to run correctly, you need to do the following.
In the root
package.json use the following
service1/package.json use this
This tells serverless-bundle (in the root) to only run the tests inside the
service1/ directory. As opposed to the entire project.
- Open a new issue if you've found a bug or have some suggestions.
- Or submit a pull request!
To run this project locally, clone the repo and initialize the project.
$ git clone https://github.com/AnomalyInnovations/serverless-bundle$ cd serverless-bundle$ npm install
Run the tests using.
$ npm test
To test the
serverless-bundle test command.
$ npm run test-scripts
To install locally in another project.
$ npm install /path/to/serverless-bundle