-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwebpack-lambda.config.js
More file actions
87 lines (78 loc) · 2.47 KB
/
webpack-lambda.config.js
File metadata and controls
87 lines (78 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
// List of modules that are not essential for Lambda and can be ignored
// or assumed to be available in the Node.js runtime
const lazyImports = [
'@nestjs/microservices',
'@nestjs/websockets/socket-module',
'cache-manager',
'class-transformer',
// Add other non-essential lazy imports here as needed
];
module.exports = function (options) {
return {
// We target 'node' for a backend application
target: 'node',
// Set mode to 'production' for optimal bundling
mode: 'production',
// The entry point should be your lambda handler file
entry: ['./src/lambda.ts'], //
// Output configuration
output: {
// The path to the output directory
path: path.join(__dirname, 'dist'),
// The name of the bundled file
filename: 'lambda-bundle.js',
// Specifies the module format for CommonJS (necessary for AWS Lambda)
libraryTarget: 'commonjs2',
},
// Enable source maps for easier debugging
devtool: 'source-map',
// Configure how modules are resolved (e.g., handling TypeScript extensions)
resolve: {
extensions: ['.ts', '.js'],
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
alias: {
"@bansay": path.resolve("./src")
}
},
// Module rules (loaders)
module: {
rules: [
{
test: /\.ts?$/,
use: {
loader: 'ts-loader',
options: {
// Explicitly point to your tsconfig.json file
configFile: path.resolve(__dirname, 'tsconfig.build.json'),
},
},
exclude: /node_modules/,
},
],
},
// Plugins configuration
plugins: [
...(options.plugins || []),
// Ignore specific lazy-loaded modules to reduce bundle size
// Use a simple RegEx pattern for the IgnorePlugin
new webpack.IgnorePlugin({
checkResource(resource) {
if (lazyImports.find(l => resource.startsWith(l))) {
try {
require.resolve(resource);
} catch (err) {
return true;
}
}
return false;
},
}),
],
// Externals: you generally want to bundle all dependencies for Lambda,
// unless you use Lambda Layers. Setting to empty array ensures all node_modules are bundled.
externals:nodeExternals(),
};
};