使用任意tsconfig.json在VS代码调试中的打字稿AWS lambda的调用

发布于 2025-02-06 14:02:35 字数 794 浏览 2 评论 0 原文

我正在尝试通过SAM在VS Code Debugger下通过SAM编写的Lambda函数来运行代码,该功能已安装了AWS工具包。该代码确实成功地构建和部署到云中,但是在调试器下运行它的错误失败,并且在这个问题

我的构建指定 eSmoduleInterop:true in tsconfig.json ,而SAM构建忽略它,构建就像 Esmoduleop:false 。它似乎使用了以下任意 tsconfig.json ,而不是项目目录中的一个。如何使用我的 tsconfig.json ,或者至少将特定的编译器选项传递给 tsc

{
"compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "typeRoots": [
        "node_modules/@types"
    ],
    "types": [
        "node"
    ],
    "rootDir": ".",
    "inlineSourceMap": true
}

I'm trying to run code for a Lambda function written in Typescript via SAM under the VS Code debugger, which has AWS Toolkit installed. The code does build and deploy successfully to the cloud, but running it under the debugger fails with error described in this question.

My build specifies esModuleInterop: true in tsconfig.json, and the SAM build ignores it, building instead as if esModuleInterop: false. It appears to use an arbitrary tsconfig.json given below, instead of the one in the project directory. How can I get it to use my tsconfig.json, or at least pass specific compiler options to tsc?

{
"compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "typeRoots": [
        "node_modules/@types"
    ],
    "types": [
        "node"
    ],
    "rootDir": ".",
    "inlineSourceMap": true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2025-02-13 14:02:35

您可以分享您的启动配置吗?如果您使用 InvoketArget.target.template 启动config(它可以在lambda项目中期望sam template.yaml 文件),它是否有效?

它似乎使用以下给出的任意tsconfig.json,而不是项目目录中的一个

当您使用 InvoketArget.target.target.target.target.target.code:Code (a code (a)时,当前是aws工具包的限制。 功能请求将不胜感激!):

Can you share your launch config? Does it work if you use a invokeTarget.target:template launch config (which expects a SAM template.yaml file in your lambda project)?

It appears to use an arbitrary tsconfig.json given below, instead of the one in the project directory

That's currently a limitation of AWS Toolkit for VSCode when you invoke a typescript lambda with invokeTarget.target:code (a feature request would be appreciated!):

https://github.com/aws/aws-toolkit-vscode/blob/acbd172d8e0de799e880ba83aff1f6a5562f11af/src/shared/sam/debugger/typescriptSamDebug.ts#L137-L138

用心笑 2025-02-13 14:02:35

我使用vscodes中的一些脚本魔术启动& tasks.json 并稍微修改 tsconfig.json ,并在SAM模板中启用Sourcemap选项。

  • 回顾我在此

.vscode/laining.json

  • 添加了配置:添加了新的调试配置代码:helloworldfunction 。此配置:
    • 直接针对代码( target:“代码” ),指定lambda处理程序( lambdahandler:“ app.lambdahandler” )和项目root。
    • 将架构设置为 x86_64 ,并将运行时指定为 nodejs20.x ,确保与最新的node.js功能和性能改进。
    • 使用预先发布的任务(观看所有


.gitignore

  • 更新条目:添加了 build> build/ ** aws-toolkit-tsconfig.json的条目通过Git跟踪构建工件和特定的配置文件。这可以使存储库保持清洁,并避免由环境特定文件引起的潜在冲突或差异。

tasks.json in .vscode/

  • 添加了任务 npm安装的新任务,Typescript编译和帖子添加了补偿任务。这些任务确保安装了所有依赖项,将打字稿汇编为JavaScript,并在编译后执行任何必要的清理或其他设置。
  • 有组织的任务:任务分为组和依赖项,简化构建过程并确保以正确的顺序执行任务。

tsconfig.json

  • 启用源地图:设置 sourcemap true true允许生成源地图,这对于在生产环境中在生产环境中调试打字条至关重要将JavaScript编译回原始打字稿源代码。
  • 模块系统:更改为commonjs,它与node.js兼容,并简化了与AWS lambda的集成,该集成期望这种模块格式。
  • 输出目录:指定 OUTDIR ,与源文件分开组织编译的文件,这有助于干净的部署。

template.yaml

  • 启用源地图:启用了源地图设置,允许AWS SAM在构建过程中使用源地图。理想情况下,我希望这样做才能自动处理一切。

  • 从运行和调试菜单中选择“代码:helloworldfunction”,并享受调试

I worked around this limitations using some scripting magic in vscodeslaunch.json & tasks.json and slightly modifying the tsconfig.json and enabling the sourcemap option in the SAM template.

  • Review the changes I made in this commit and do the needed changes accordingly. Here is a brief explanation for each important change

.vscode/launch.json

  • Added Configuration: A new debugging configuration code:HelloWorldFunction was added. This configuration:
    • Targets the code directly (target: "code"), specifying the lambda handler (lambdaHandler: "app.lambdaHandler") and the project root.
    • Sets the architecture to x86_64 and specifies the runtime as nodejs20.x, ensuring compatibility with the latest Node.js features and performance improvements.
    • Uses a pre-launch task (Watch All), likely set up to compile TypeScript to JavaScript and watch for changes, ensuring that the latest code is always deployed and debugged.

.gitignore

  • Updated Entries: Added entries for build/ and **aws-toolkit-tsconfig.json, preventing build artifacts and specific configuration files from being tracked by Git. This keeps the repository clean and avoids potential conflicts or discrepancies caused by environment-specific files.

tasks.json in .vscode/

  • Added Tasks: New tasks for npm install, TypeScript compilation, and a post-compilation task were added. These tasks ensure that all dependencies are installed, the TypeScript is compiled to JavaScript, and any necessary cleanup or additional setup is performed after compilation.
  • Organized Tasks: Tasks are organized into groups and dependencies, streamlining the build process and ensuring that tasks are executed in the correct order.

tsconfig.json

  • Source Maps Enabled: Setting sourceMap to true allows generation of source maps, which are crucial for debugging TypeScript in production environments as they map the compiled JavaScript back to the original TypeScript source code.
  • Module System: Changed to CommonJS, which is compatible with Node.js and simplifies the integration with AWS Lambda, which expects this module format.
  • Output Directory: Specified an outDir, organizing compiled files separately from source files, which helps in clean deployments.

template.yaml

  • Enabled Source Maps: The source map setting was enabled, allowing AWS SAM to use source maps during the build process. Ideally, I expected just doing this to handle everything automatically.

  • Select the code:HelloWorldFunction from the Run and Debug Menu and enjoy debugging ????
    enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文