我正在使用无服务器框架开发一些 Lambda 函数。无服务框架已在全球范围内安装。
我正在使用 Typescript 和 serverless-webpack。
我也在使用 serverless-offline 在本地进行测试。
一切正常,除非我尝试从 VSCode 中进行调试。问题是,一旦我从 VSCode 的调试工具启动serverless -offline,我所有的断点都会变灰。
这是我的配置文件
包.json
{
"name": "backend-serverless",
"version": "1.0.0",
"description": "serverless backend",
"main": "handler.js",
"scripts": {
"test": "mocha -r ts-node/register transform/src/**/*.spec.ts src/**/**/*.spec.ts",
"tsc": "tsc",
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/aws-lambda": "0.0.34",
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.0",
"chai": "^4.1.2",
"mocha": "^5.0.5",
"serverless-offline": "^3.18.0",
"serverless-webpack": "^5.1.1",
"ts-loader": "^4.1.0",
"ts-node": "^5.0.1",
"typescript": "^2.7.2",
"webpack": "^4.3.0"
}
}
webpack.config.ts
const path = require('path');
const slsw = require('serverless-webpack');
module.exports = {
devtool: 'source-map',
entry: slsw.lib.entries,
resolve: {
extensions: [
'.js',
'.json',
'.ts',
'.tsx'
]
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js'
},
target: 'node',
module: {
rules: [
{
test: /\.ts(x?)$/,
use: [
{
loader: 'ts-loader'
}
],
}
]
}
};
启动.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Current TS File",
"type": "node",
"request": "launch",
"args": ["${relativeFile}"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
},
{
"type": "node",
"request": "launch",
"name": "Debug - Offline",
"program": "/usr/local/bin/serverless",
"args": [
"offline",
"start",
"--lazy"
],
"env": {
"NODE_ENV": "development"
},
"outFiles": [
"${cwd}/.webpack/**/*"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts", "*.ts"
],
"exclude": [
"node_modules"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
}
顺便说一句,如果我尝试使用Current TS File
启动配置调试任何用 Typescript 编写的正常函数,我的所有断点都可以正常工作。如果我使用Debug - Offline
启动配置,那么所有断点都会变灰。