【问题标题】:How to debug/step-into npm package with Typescript source code?如何使用 Typescript 源代码调试/步入 npm 包?
【发布时间】:2020-01-11 06:43:08
【问题描述】:

Okta(Angular NPM 依赖项)没有表现/返回我期望的值。我想在调试我的应用程序时进入它的源代码,以便了解正在发生的事情并解决问题。如何在我的 Angular 6 应用程序中执行此操作?

我正在使用ng serve --source-map --vendor-source-map (as shown here) 来启动 Angular,但这不起作用。当我尝试调试在OktaAuthService 类中定义的名为isAuthenticated() 的方法时,Chrome 的调试器显示此错误:

Could not load content for webpack:///../../../../src/okta/services/okta.service.ts : HTTP status code: 404

我在 Okta 的 npm 包中找不到这个文件,但我确实找到了它的类型定义文件 okta.service.d.ts。这是否意味着 Okta 的 NPN 包不包含 TS 源代码及其转译的 JS 代码?是否有一个 npm/Angular 选项可以让我下载 Okta 包及其源代码以进行调试?

我使用的是什么版本?

当我运行ng --version 时,我得到以下信息:

@ngtools/webpack                  6.1.5
@schematics/angular               0.7.5
@schematics/update                0.7.5
rxjs                              6.4.0
typescript                        2.9.2
webpack                           4.9.2

这是我的 package.json 文件中的内容

"dependencies": {
        "@angular/animations": "^6.1.6",
        "@angular/cdk": "^6.4.7",
        "@angular/common": "^6.1.6",
        "@angular/compiler": "^6.1.6",
        "@angular/core": "^6.1.6",
        "@angular/forms": "^6.1.6",
        "@angular/http": "^6.1.6",
        "@angular/material": "^6.4.7",
        "@angular/platform-browser": "^6.1.6",
        "@angular/platform-browser-dynamic": "^6.1.6",
        "@angular/platform-server": "^6.1.6",
        "@angular/router": "^6.1.6",
        "@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.5",
        "@okta/okta-angular": "^1.3.0",
        "@primer/octicons": "^9.1.1",
        "@types/dom-inputevent": "^1.0.4",
        "@types/jquery": "^3.3.29",
        "aspnet-prerendering": "^3.0.1",
        "bootstrap": "^4.3.1",
        "class-validator": "^0.9.1",
        "classlist.js": "^1.1.20150312",
        "core-js": "^2.4.1",
        "hammerjs": "^2.0.8",
        "jquery": "^3.4.1",
        "lodash": "^4.17.10",
        "masonry-layout": "^4.2.2",
        "ngx-dropzone-wrapper": "^7.1.0",
        "ngx-quill": "^7.2.0",
        "popper": "^1.0.1",
        "quill": "^1.3.6",
        "rxjs": "^6.4.0",
        "rxjs-compat": "^6.0.0-rc.0",
        "stackedit-js": "^1.0.7",
        "typescript-string-operations": "^1.3.1",
        "web-animations-js": "^2.3.1",
        "zone.js": "^0.8.26"
    }

【问题讨论】:

  • 一般来说,npm dist 包是你的应用程序正在运行的,它们是被缩小的并且不是人类可读的。源可能根本不在ts中。步入可能不是一个可行的选择。这里的问题可能不在 okta 中,您可以只记录事情或单步执行您自己的代码以找出问题所在。或者,如果一个包裹引起了如此多的头痛,那可能就不值得了。
  • 您使用的是哪个 Angular cli 版本?

标签: angular angular6


【解决方案1】:

我可能会在这里帮你玩一点。我也遇到过同样的情况,以前也有过,但我不确定我是否记得。

您是否有 Okta 的 .map 文件,如果您手动包含这些文件,它应该有助于 .js -> .ts 调试??

我也确实找到了有问题的文件。这可能就像下载源文件并将它们放在预期的 src 文件夹中一样简单(来自错误消息?)

https://github.com/okta/okta-oidc-js/tree/master/packages/okta-angular/src/okta/services

他们很可能不会在他们分发的“构建”或“捆绑”中包含 .ts 源代码文件,但看起来你可以从 github 中提取它们,所以也许这就是你的技巧需要吗?

如果你有任何运气,请告诉我?

您还可以编辑 .js 文件并添加调试器;声明

【讨论】:

  • 不幸的是,将源代码复制到okta-angular 没有奏效。我收到以下错误:ERROR in ./node_modules/@okta/okta-angular/dist/src/okta/components/index.ts Module build failed: Error: H:\@MyApp\ClientApp\node_modules\@okta\okta-angular\dist\src\okta\components\index.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property. The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library.
  • Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format at AngularCompilerPlugin.getCompiledFile (H:\@MyApp\ClientApp\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:749:23) at plugin.done.then (H:\@MyApp\ClientApp\node_modules\@ngtools\webpack\src\loader.js:41:31) at process._tickCallback (internal/process/next_tick.js:68:7)
  • 无论如何,我在无法使用 TS 源代码调试库的情况下找出了问题的根源。所以我有这个能力已经不重要了。我会奖励你的努力。
  • 非常感谢!
【解决方案2】:

尝试设置

module.exports = {
  mode: 'development'
};

在 angular.json 中

{
  "architect": {
    "build": {
       "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "customWebpackConfig": {
         HERE

或者只是使用旧的console.log

【讨论】:

  • 对不起.. 我什至不知道我会在哪里设置mode: 'development'。我也不知道customWebpackConfig 中应该包含哪些值
  • 我认为将console.log() 放在需要输出的地方会更容易。只需编辑 node_modules 中的 JS 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-28
  • 2012-09-24
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 2012-05-16
相关资源
最近更新 更多