Skip to content

Commit e9e1770

Browse files
committed
2 parents 9b6aafb + 0de12ac commit e9e1770

37 files changed

Lines changed: 324 additions & 3845 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ You can [read more about this over on Serverless Stack](https://serverless-stack
5858
- [Support for pem, txt, and other raw files](#support-for-pem-txt-and-other-raw-files)
5959
- [Externals](#externals)
6060
- [Externals vs forceExclude](#externals-vs-forceexclude)
61+
- [Generating a stats file](#generating-stats-files)
6162
- [Support](#support)
6263
- [Running Locally](#running-locally)
6364

@@ -105,6 +106,7 @@ custom:
105106
concurrency: 5 # Set desired concurrency, defaults to the number of available cores
106107
stats: false # Don't print out any Webpack output
107108
linting: true # Enable linting as a part of the build process
109+
generateStatsFiles: false # Creates stats files that could be used for bundle analyzing, more below
108110
esbuild: false # Use esbuild-loader instead of babel or ts for faster builds
109111
disableForkTsChecker: false # Disable the ForkTsChecker plugin, more below
110112
tsConfig: "tsconfig.json" # Path to your 'tsconfig.json', if it's not in the root
@@ -454,6 +456,12 @@ The three options (`externals`, `forceExclude`, and `excludeFiles`) look similar
454456

455457
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`.
456458

459+
### Generating stats files
460+
461+
Use the `generateStatsFiles` option if you want to analyze your bundle size. This option, if set to `true`, will
462+
enable the generation of a `bundle_stats.json` and a `bundle_stats.html` in the output directory, using the
463+
[webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) plugin.
464+
457465
## Support
458466

459467
- Open a [new issue](https://github.com/AnomalyInnovations/serverless-bundle/issues/new) if you've found a bug or have some suggestions.

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function applyWebpackOptions(custom, config) {
3535
),
3636
},
3737
excludeFiles: config.options.excludeFiles,
38+
excludeRegex: /bundle_stats\.(html|json)$/,
39+
keepOutputDirectory: config.options.generateStatsFiles,
3840
};
3941
}
4042

package-lock.json

Lines changed: 192 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"tsconfig-paths-webpack-plugin": "^3.5.1",
7777
"typescript": "^4.2.4",
7878
"webpack": "^5.33.2",
79+
"webpack-bundle-analyzer": "^4.5.0",
7980
"webpack-node-externals": "^2.5.2",
8081
"webpack-permissions-plugin": "^1.0.8"
8182
},

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
excludeFiles: null,
1919
ignorePackages: [],
2020
packagerOptions: {},
21+
generateStatsFiles: false,
2122
tsConfig: "tsconfig.json",
2223
// Exclude aws-sdk since it's available in the Lambda runtime
2324
forceExclude: ["aws-sdk"],

src/webpack.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const nodeExternals = require("webpack-node-externals");
1010
const CopyWebpackPlugin = require("copy-webpack-plugin");
1111
const { ESBuildMinifyPlugin } = require("esbuild-loader");
1212
const ConcatTextPlugin = require("concat-text-webpack-plugin");
13+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
1314
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
1415
const PermissionsOutputPlugin = require("webpack-permissions-plugin");
1516
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
@@ -46,6 +47,7 @@ const ENABLE_LINTING = config.options.linting;
4647
const ENABLE_SOURCE_MAPS = config.options.sourcemaps;
4748
const ENABLE_TYPESCRIPT = fs.existsSync(tsConfigPath);
4849
const ENABLE_TSCHECKER = !config.options.disableForkTsChecker;
50+
const GENERATE_STATS_FILES = config.options.generateStatsFiles;
4951
const ENABLE_CACHING = isLocal ? config.options.caching : false;
5052

5153
// Handle the "all" option in externals
@@ -272,6 +274,18 @@ function loaders() {
272274
function plugins() {
273275
const plugins = [];
274276

277+
if (GENERATE_STATS_FILES) {
278+
plugins.push(
279+
new BundleAnalyzerPlugin({
280+
openAnalyzer: false,
281+
analyzerMode: "static",
282+
generateStatsFile: true,
283+
statsFilename: "bundle_stats.json",
284+
reportFilename: "bundle_stats.html",
285+
})
286+
);
287+
}
288+
275289
if (ENABLE_TYPESCRIPT && ENABLE_TSCHECKER) {
276290
const forkTsCheckerWebpackOptions = {
277291
typescript: {

tests/aliases-jest/package-lock.json

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)