Gathering detailed insights and metrics for serverless-bundle
Gathering detailed insights and metrics for serverless-bundle
Gathering detailed insights and metrics for serverless-bundle
Gathering detailed insights and metrics for serverless-bundle
serverless-python-requirements
Serverless Python Requirements Plugin
@serverless/utils
Serverless CLI utilities
serverless
[![Serverless Framework AWS Lambda AWS DynamoDB AWS API Gateway](https://github.com/serverless/serverless/assets/2752551/66a8c6a9-bc4a-4116-b139-90c12963337e)](https://serverless.com)
serverless-analyze-bundle-plugin
A Serverless plugin to analyze the bundle of a lambda function.
npm install serverless-bundle
54.8
Supply Chain
55.6
Quality
69.4
Maintenance
100
Vulnerability
90.7
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
536 Stars
512 Commits
157 Forks
8 Watching
11 Branches
43 Contributors
Updated on 10 Oct 2024
JavaScript (90.84%)
TypeScript (9.01%)
CSS (0.06%)
Shell (0.06%)
SCSS (0.04%)
Cumulative downloads
Total Downloads
Last day
-24.5%
7,054
Compared to previous day
Last week
-13.9%
34,759
Compared to previous week
Last month
-3.6%
172,683
Compared to previous month
Last year
-0.8%
2,723,386
Compared to previous year
47
1
serverless-bundle is a Serverless Framework plugin that optimally packages your ES6 or TypeScript Node.js Lambda functions with sensible defaults so you don't have to maintain your own Webpack configs. It uses the serverless-webpack plugin internally.
And all this works without having to install Webpack, Babel, ESLint, esbuild, etc. or manage any of their configs. Simply add serverless-bundle to your app and you are done!
1- "eslint" 2- "webpack" 3- "ts-loader" 4- "typescript" 5- "css-loader" 6- "graphql-tag" 7- "@babel/core" 8- "babel-eslint" 9- "babel-loader" 10- "eslint-loader" 11- "esbuild-loader" 12- "@babel/runtime" 13- "@babel/preset-env" 14- "serverless-webpack" 15- "source-map-support" 16- "webpack-node-externals" 17- "eslint-config-strongloop" 18- "tsconfig-paths-webpack-plugin" 19- "fork-ts-checker-webpack-plugin" 20- "@babel/plugin-transform-runtime" 21- "babel-plugin-source-map-support" 22 23+ "serverless-bundle"
You can read more about this over on the SST Guide.
💥 The serverless-bundle
team recently launched the SST. SST makes it easy to build serverless apps by letting you test your Lambda functions live. It's based on the many of ideas behind serverless-bundle
.
Install the serverless-bundle
plugin using:
1$ npm install --save-dev serverless-bundle
Then add it to your serverless.yml
.
1plugins: 2 - serverless-bundle
To run your tests using the same Babel config used in the plugin add the following to your package.json
:
1"scripts": { 2 "test": "serverless-bundle test" 3}
We also have a couple of template repos to help you get started:
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 serverless.yml
. Note that, these are all optional.
1custom: 2 bundle: 3 sourcemaps: true # Enable source maps 4 caching: true # Enable Webpack caching 5 concurrency: 5 # Set desired concurrency, defaults to the number of available cores 6 stats: false # Don't print out any Webpack output 7 linting: true # Enable linting as a part of the build process 8 generateStatsFiles: false # Creates stats files that could be used for bundle analyzing, more below 9 esbuild: false # Use esbuild-loader instead of babel or ts for faster builds 10 disableForkTsChecker: false # Disable the ForkTsChecker plugin, more below 11 tsConfig: "tsconfig.json" # Path to your 'tsconfig.json', if it's not in the root 12 forceInclude: # Optional list of NPM packages that need to be included 13 - mysql # Only necessary if packages are included dynamically 14 ignorePackages: # Ignore building any of the following packages 15 - hiredis # For ex, hiredis needs to be ignored if using redis 16 externals: # Set non Webpack compatible packages as externals 17 - isomorphic-webcrypto # They'll be included in the node_modules/, more below 18 forceExclude: # Don't include these in the package 19 - chrome-aws-lambda # Because it'll be provided through a Lambda Layer 20 excludeFiles: "**/*.test.ts" # Exclude files from Webpack that match the glob 21 fixPackages: # Include fixes for specific packages 22 - "formidable@1.x" # For ex, formidable@1.x doesn't work by default with Webpack 23 copyFiles: # Copy any additional files to the generated package 24 - from: 'public/*' # Where the files are currently 25 to: './' # Where in the package should they go 26 aliases: # Create an alias to 'import' modules easily with a custom path 27 - Lib: custom-lib/src/lib # For ex, replace the long 'custom-lib/src/lib' with 'Lib' 28 packager: npm # Specify a packager, 'npm' or 'yarn'. Defaults to 'npm'. 29 packagerOptions: # Run a custom script in the package process 30 scripts: # https://github.com/serverless-heaven/serverless-webpack#custom-scripts 31 - echo hello > test 32 nodeModulesRelativeDir: '../' # Useful for monorepos if you have your node_modules in the root directory 33 # https://github.com/serverless-heaven/serverless-webpack#node-modules--externals 34 rawFileExtensions: # An array of file extensions to import using the Webpack raw-loader. 35 - csv # Defaults to ['pem', 'txt'] 36 minifyOptions: # Options for ESBuildMinifyPlugin (https://esbuild.github.io/api/#simple-options) 37 keepNames: true # Disable symbol name mangling during minification 38 experiments: # Give the ability to activate and try out experimental features of Webpack 39
ESLint
This plugin uses eslint-config-strongloop. You can override this by placing your own .eslintrc.json
with the rules you'd like to use. If you'd like to ignore specific files, you can use a .eslintignore
file.
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.
Supporting specific packages
Certain packages like (formidable@1.x
) do not work with Webpack without customizing the config. To support these packages, we use the fixPackages
option. This allows us to customize the Webpack config without having folks learn about the internals of Webpack, or maintaining their own complicated configs. If a specific package doesn't work without customizing the Webpack config, add to the fixPackages
option and submit a PR.
Packager scripts
The packagerOptions.scripts
option 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 sharp
package.
Aliases
Import paths can get very long when dealing with complicated directory structures in monorepo apps. The aliases
option allows you to define a shorter version. So if you have an import that looks like:
1import Utility from '../../custom-lib/src/lib/utility';
Adding the following. Where src/utilities
is the path from the project root.
1custom: 2 bundle: 3 aliases: 4 - "Lib": custom-lib/src/lib
This would allow you to instead import using the following, from anywhere in your project.
1import Utility from 'Lib/utility';
To use aliases in your tests you'll need to use Jest's moduleNameMapper
. Add the following your package.json
:
1"jest": { 2 "moduleNameMapper": { 3 "Lib(.*)$": "<rootDir>/custom-lib/src/lib/$1" 4 } 5}
Excluding modules from bundling
In some cases it might be neccessary to exclude certain modules from bundling with Webpack. This can be achieved by setting the module alias to false
:
1custom: 2 bundle: 3 aliases: 4 - "module-name": false
The aliases
option is explained in detail in the Webpack documentation.
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
option, add "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 test-report.json
.
To test the reporters
option, add "reporters": ["jest-mocha-reporter"]
instead. This should result in the same file as above but without the command line output.
If serverless-bundle detects a tsconfig.json
in your service root, it'll enable TypeScript.
You can also change where your tsconfig is.
1custom: 2 bundle: 3 tsConfig: 'tsconfig.special.json'
Setting the module
to commonjs
or target
to es3
or es5
conflicts with the babel-plugin-source-map-support plugin that serverless-bundle uses for adding source maps. It'll cause imports in your code to error out with something like this:
1TypeError: fileName.functionName is not a function
So if serverless-bundle detects these in your tsconfig.json
, it'll print the following warning.
1serverless-bundle: CommonJS, ES3, or ES5 are not supported
More on this issue here.
By default serverless-bundle uses the ForkTsCheckerWebpackPlugin to speed up builds by running type checking in a separate process. However, this combined with Serverless Framework's package: individually: true
option means that to packages each Lambda function, a separate type checking process is started. Concurrently, starting many such processes can cause your build process to run out of memory.
To disabled this, add the following to your config.
1custom: 2 bundle: 3 disableForkTsChecker: true
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:
1custom: 2 bundle: 3 forceInclude: 4 - mysql
The sharp package needs to include a specific binary before package. Use the packagerOptions.scripts
for this.
1custom: 2 bundle: 3 packagerOptions: 4 scripts: 5 - 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.
1custom: 2 bundle: 3 ignorePackages: 4 - pg-native
The redis package optionally includes hiredis
that needs to be ignored from Webpack. Use the ignorePackages
option to do this.
1custom: 2 bundle: 3 ignorePackages: 4 - hiredis
To use the Sequelize package along with pg, you'll need to ignore it from Webpack and using the dialectModule
option. Read more here.
In your serverless.yml
:
1custom: 2 bundle: 3 ignorePackages: 4 - pg-native
And in your Lambda code:
1const sequelize = new Sequelize(
2 process.env.DB_NAME,
3 process.env.DB_USERNAME,
4 process.env.DB_PASSWORD,
5 {
6 host: process.env.DB_HOST,
7 dialect: process.env.DB_DIALECT,
8 dialectModule: pg
9 }
10);
Formidable 1.x doesn't work with Webpack by default. We have a fix that we apply to the Webpack config for it to work. To apply the fix use the following:
1custom: 2 bundle: 3 fixPackages: 4 - "formidable@1.x"
If enabled, Webpack adds the following definition to work with Formidable — { "global.GENTLY": false }
.
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 (deploy
, 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 test
script:
1"scripts": { 2 "test": "serverless-bundle test" 3}
And in service1/package.json
use this test
script:
1"scripts": { 2 "test": "npm --prefix ./../ test service1" 3},
This tells serverless-bundle (in the root) to only run the tests inside the service1/
directory. As opposed to the entire project.
Serverless Bundle automatically supports importing css and scss using the isomorphic-style-loader.
1import "./assets/style.css"; 2import "./assets/style.scss";
Serverless Bundle automatically supports importing .pem
and .txt
, using the Webpack raw-loader.
1import "./assets/key.pem"; 2import "./assets/text.txt";
If you need load additional files using the raw-loader, you can use the rawFileExtensions
config option.
1custom: 2 bundle: 3 rawFileExtensions: 4 - csv
The externals
option takes a list of packages or all
. By default this is set to ["knex", "sharp"]
.
Packages listed in externals
are ignored by Webpack. They are instead added in the node_modules/
directory of the Lambda .zip file. These usually include npm packages that are not supported by Webpack.
The all
option allows you to list all the packages in YOUR node_modules/
directory as externals. This might be useful in cases where they are just too many to list. Or you are using something like EJS that implicitly requires a long list of packages that are not supported by Webpack.
Note that, adding a package to the externals
list might make your Lambda .zip file larger. This is because the entire package directory is zipped. Instead of using Webpack to just include the code that is necessary. So it's advisable to avoid using the all
option.
If you think we should add to the default list of externals, open a PR.
Example of specifying a list of packages:
1custom: 2 bundle: 3 externals: 4 - knex 5 - sharp
Example of using the all
option:
1custom: 2 bundle: 3 externals: all
The three options (externals
, forceExclude
, and excludeFiles
) look similar but have some subtle differences. Let's look at them in detail:
externals
These are packages that need to be included in the Lambda package (the .zip file that's sent to AWS). But they are not compatible with Webpack. So they are marked as externals
to tell Webpack not bundle them.
forceExclude
These packages are available in the Lambda runtime. Either by default (in the case of aws-sdk
) or through a Lambda layer that you might be using. So these are not included in the Lambda package. And they are also marked as externals
. Meaning that packages that are in forceExclude
are automatically added to the externals
list as well. By default, aws-sdk
is listed in the forceExclude
when runtime
is lower than nodejs18.x
.
excludeFiles
These are a glob of files that can be excluded from the function resolution. This happens when you have multiple files that are in the same directory and Serverless Framework tries to use them as a function handler. For example, if you have a index.js
and a index.test.js
and your function is pointing to index
, you'll get a warning saying, WARNING: More than one matching handlers found for index. Using index.js
. To fix this, use excludeFiles: **/*.test.js
.
Use the generateStatsFiles
option if you want to analyze your bundle size. This option, if set to true
, will
enable the generation of a bundle_stats.json
and a bundle_stats.html
in the output directory, using the
webpack-bundle-analyzer plugin.
To run this project locally, clone the repo and initialize the project.
1$ git clone https://github.com/AnomalyInnovations/serverless-bundle 2$ cd serverless-bundle 3$ npm install
Run all the tests.
1$ npm test
To test the serverless-bundle test
command.
1$ npm run test scripts
To install locally in another project.
1$ npm install /path/to/serverless-bundle
breaking
, enhancement
, bug
, documentation
, or internal
npm run changelog
npm version <major|minor|patch>
git push origin <tag_name>
npm publish
This plugin would not be possible without the amazing serverless-webpack plugin and the ideas and code from Create React App.
This plugin is maintained by SST.
No vulnerabilities found.
Reason
25 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/8 approved changesets -- score normalized to 5
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
17 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More