Skip to content

Commit 1a004e1

Browse files
committed
feat(debugging): enabled local debugging in vscode using webpack and sourcemaps
1 parent 184d7c3 commit 1a004e1

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

.vscode/launch.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
2-
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
3-
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
4-
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
52
"version": "0.2.0",
63
"configurations": [
7-
{
8-
"type": "node",
9-
"request": "launch",
10-
"name": "Launch Program",
11-
"program": "${workspaceFolder}\\src\\handler.js"
12-
}
4+
{
5+
"name": "Launch Program",
6+
"type": "node",
7+
"request": "launch",
8+
"cwd": "${workspaceRoot}",
9+
"program": "${workspaceRoot}/src/handler.js",
10+
"outFiles": [
11+
"${workspaceRoot}/dist/handler.js"
12+
],
13+
"sourceMaps": true
14+
}
1315
]
14-
}
16+
}

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,23 @@ curl -H "x-api-key: {API - KEY}" -H "Content-Type: application/json" https://xxx
175175
- [AWS Lambda](https://aws.amazon.com/lambda/features/)
176176
- [AWS S3](https://aws.amazon.com/s3/)
177177

178-
## Develop locally
178+
## Develop and debug locally
179179

180180
db.json file will be loaded directly from your local filesystem. No AWS access is needed.
181181

182-
#### 1. Start solution
182+
#### Start solution
183183

184184
```bash
185185
npm run start
186186
```
187187

188+
#### Debug solution
189+
If you want to debug locally in VS Code everything is already setup (using webpack with sourcemap support)
190+
191+
```bash
192+
npm run debug
193+
```
194+
188195
#### 2. Test your API
189196

190197
To test you can use e.g. [Postman](https://www.getpostman.com/)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "src/server.js",
66
"scripts": {
77
"test": "npm run build & NODE_ENV=local jest",
8+
"debug": "NODE_ENV=debug webpack --watch",
89
"start": "NODE_ENV=local webpack --watch",
910
"dev": "NODE_ENV=development webpack --watch",
1011
"diagnostic": "NODE_ENV=offline sls offline",

src/handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports.handler = async (event, context) => {
1919
};
2020

2121
if (require.main === module) {
22-
if (process.env.NODE_ENV === 'local') {
22+
if (process.env.NODE_ENV === 'local' || process.env.NODE_ENV === 'debug') {
2323
start(app.server, 3000);
2424
} else if (process.env.NODE_ENV === 'development') {
2525
(async () => {

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const request = async () => {
4848

4949
function init() {
5050
logger.info(`NODE_ENV: ${process.env.NODE_ENV}`);
51-
if (process.env.NODE_ENV === 'local') {
51+
if (process.env.NODE_ENV === 'local' || process.env.NODE_ENV === 'debug') {
5252
startLocal(3000);
5353
} else if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'offline') {
5454
logger.info('start development mode');

webpack.config.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CopyPlugin = require('copy-webpack-plugin');
44
const NodeEnvPlugin = require('node-env-webpack-plugin');
55
const NodemonPlugin = require('nodemon-webpack-plugin');
66
const TerserPlugin = require('terser-webpack-plugin');
7-
7+
const { join } = require('path');
88

99
module.exports = {
1010
mode: 'development',
@@ -15,17 +15,22 @@ module.exports = {
1515
new NodeEnvPlugin({
1616
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
1717
}),
18-
new NodemonPlugin({
19-
}),
18+
process.env.NODE_ENV === 'debug' ? new NodemonPlugin({ nodeArgs: ['--inspect-brk'] }) : new NodemonPlugin(),
2019
],
2120
entry: { 'src/handler': './src/handler.js' },
2221
optimization: {
2322
minimizer: [new TerserPlugin()],
2423
},
2524
devtool: 'source-map',
26-
devServer: {
27-
contentBase: './dist',
25+
output: {
26+
path: join(__dirname, 'dist'),
27+
filename: 'handler.js',
28+
29+
// Bundle absolute resource paths in the source-map,
30+
// so VSCode can match the source file.
31+
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
2832
},
33+
2934
target: 'node',
3035
externals: [nodeExternals()],
3136
module: {

0 commit comments

Comments
 (0)