简体   繁体   English

使用chrome web开发人员工具在Angular 2 CLI项目中未显示的Typescript文件

[英]Typescript files not showing in Angular 2 CLI project using chrome web developer tools

I have generated a typescript project using angular2 CLI (ember cli). 我使用angular2 CLI(ember cli)生成了一个打字稿项目。 I am using Chromium Web Developer tools to debug. 我正在使用Chromium Web Developer工具进行调试。 I have "enable javascript source maps" selected in the Developers Tools settings. 我在开发者工具设置中选择了“启用javascript源地图”。 I see .js files and .ts listed in the sources view. 我看到源视图中列出的.js文件和.ts。 However, the .ts (typescript) files show as empty when I select them ie like it can't find them. 但是,当我选择它们时,.ts(typescript)文件显示为空,即它无法找到它们。

Screen Print showing the situation: 屏幕打印显示情况:

在此输入图像描述

I can see the text of the .js files if I select them. 如果我选择它们,我可以看到.js文件的文本。

The .ts files are not copied into the dist directory, and this seems like a problem to me, but I've seen examples elsewhere showing typical angular2 cli dist structures and they do not have .ts files either. .ts文件没有被复制到dist目录中,这对我来说似乎是一个问题,但我看到其他地方的例子显示了典型的angular2 cli dist结构,它们也没有.ts文件。

Relevant portion of my "tsconfig.json" showing I have source mapping enabled: 我的“tsconfig.json”的相关部分显示我启用了源映射:

 1   "compileOnSave": false,                                                                            
  2   "compilerOptions": {                                                                               
  3     "declaration": false,                                                                            
  4     "emitDecoratorMetadata": true,                                                                   
  5     "experimentalDecorators": true,                                                                  
  6     "mapRoot": "",                                                                                   
  7     "module": "system",                                                                              
  8     "moduleResolution": "node",                                                                      
  9     "noEmitOnError": true,                                                                           
 10     "noImplicitAny": false,                                                                          
 11     "outDir": "../dist/",                                                                            
 12     "rootDir": ".",                                                                                  
 13     "sourceMap": true,                                                                               
 14     "sourceRoot": "/",                                                                               
 15     "target": "es5"                                                                                  
 16   },                          

js.map files are produced and reside in the dist directory next to the .js files. js.map文件生产和居住在dist目录旁边的.js文件。

Example mapping file (portion): 映射文件示例(部分):

{"version":3,"file":"app.js","sourceRoot":"/","sources":["app.ts"],"names":[],"mappings":";;;;;;;;;;;;;YAGA,mB

I've tried: 我试过了:
1) copying the .ts files from src into dist. 1)将.ts文件从src复制到dist。
2) various attempts at editing the app.js.map file manually eg sourceRoot and sources . 2)手动编辑app.js.map文件的各种尝试,例如sourceRootsources

It's not quite clear what "/" is referring to in the js.map file: the project's src dir or the dist dir? 目前还不十分清楚js.map文件中的“/”是什么:项目的src目录还是dist目录? If I edit a .js.map file do I have to refresh my browser to pick up on it? 如果我编辑.js.map文件,我是否必须刷新浏览器以获取它?

I think the basic problem is it's looking for the ts files in the src dir, and this is outside the reach of the browser (since it's located at "../src"). 我认为基本问题是它在src目录中寻找ts文件,这超出了浏览器的范围(因为它位于“../src”)。

Chromium shows the path for the ts file as " http://localhost:4202/app/cpp-scenes.ts ". Chromium将ts文件的路径显示为“ http:// localhost:4202 / app / cpp-scenes.ts ”。 But when I put the .ts file there, no dice. 但是当我把.ts文件放在那里时,没有骰子。

I don't know if this is a js mapping problem, an Angular 2 problem, an Angular2 CLI problem, or a typescript problem. 我不知道这是一个js映射问题,一个Angular 2问题,一个Angular2 CLI问题,还是一个打字稿问题。

It's just a real pain when the console says "error in line 52 in blah.ts", but I can't jump there and put in a breakpoint etc. 当控制台说出“第52行中的错误”时,这真的很痛苦,但是我不能跳到那里并插入一个断点等等。

Has anyone else had any luck getting source mapping working in an Angular2 CLI project? 有没有其他人有幸在Angular2 CLI项目中获得源映射?

I was able to get this to work. 我能够让这个工作。 You have to to add the angular CLI src folder to your workspace, and then you have "map to network resource" on one of your .ts files. 您必须将角度CLI src文件夹添加到工作区,然后在其中一个.ts文件上“映射到网络资源”。

Note: this example assumes the case where the client and server are running on the same machine. 注意:此示例假定客户端和服务器在同一台计算机上运行的情况。 If you have the situation where the client is on a windows machine, but the server is running on linux for example, then on the client machine you have to mount the linux server source directory as a windows share and then specify something like 如果您遇到客户端在Windows机器上,但服务器在Linux上运行的情况,那么在客户端机器上,您必须将Linux服务器源目录挂载为Windows共享 ,然后指定类似的东西

 \\\\192.168.1.134\\myShare\\myProject\\src\\client 

as your workspace folder. 作为工作区文件夹。

I recently had to do this, and while it's a little more complicated, it fundamentally works the same as the local case 我最近不得不这样做,虽然它有点复杂,但它从根本上与本地案例相同

I took screen shots of the entire process, so it's probably easiest just to show these. 我拍摄了整个过程的屏幕截图,所以最简单的方法就是展示这些。

  1. Select one of the ts files that resides under your dist directory (that corresponds to the directory that ng serve is serving from), and select "Add folder to workspace": 选择位于dist目录下的ts文件之一(对应于ng服务所从的目录),然后选择“将文件夹添加到工作区”:

屏幕1

  1. Select your src/client folder: 选择你的src / client文件夹: 在此输入图像描述

  2. Say "Allow" to the subsequent prompt allowing dev tools to access the src dir: 对允许开发工具访问src目录的后续提示说“允许”: 在此输入图像描述

  3. Observe that the src folder is now listed under Dev Tools sources. 观察到src文件夹现在列在Dev Tools源下。 You can now browse all the ts files (not just the one you added), but any breakpoints you set won't take effect until you "establish a mapping": 您现在可以浏览所有ts文件(不仅仅是您添加的文件),但是在您“建立映射”之前,您设置的任何断点都不会生效: 在此输入图像描述

  4. You can enable the mapping as shown in the next two screen prints: 您可以启用映射,如下两个屏幕打印所示: 在此输入图像描述

Select the appropriate mapping and click on it to enable 选择适当的映射并单击它以启用 在此输入图像描述

Now I can set a breakpoint in the .ts file and drill down on error messages to the .ts source. 现在我可以在.ts文件中设置断点,并深入查看.ts源的错误消息。

The basic trick is the you have to add the source ts file directory to Chrome manually, since the files are not under the serving directory. 基本技巧是您必须手动将源ts文件目录添加到Chrome,因为文件不在服务目录下。 The mapping stays in effect until you either manually remove it (by right-clicking on a .ts file and selecting "remove network mapping") or until you close the browser (it will retain the mapping on refreshes however) 映射保持有效,直到您手动删除它(通过右键单击.ts文件并选择“删除网络映射”)或直到您关闭浏览器(然而它将保留映射刷新)

Introduction: 介绍:

I'm the original poster and supplier of the answer posted on 2016-06-04 . 我是2016-06-04发布的答案的原始海报和供应商。

I have recently discovered a simpler way to get this working. 我最近发现了一种更简单的方法来实现这一点。 I am posting this as a second answer because my original answer still works, and some people might prefer it. 我发布这个作为第二个答案,因为我的原始答案仍然有效,有些人可能更喜欢它。 However, I think the new way I'm about to describe is a little easier. 但是,我认为我将要描述的新方法更容易一些。

Solution Overview: 解决方案概述

Basically, you can set up everything under the DeveloperTools->Settings->Workspace menu, instead of right-clicking on everything. 基本上,您可以在DeveloperTools-> Settings-> Workspace菜单下设置所有内容,而不是右键单击所有内容。 The right-clicking of my original answer is essentially just another way to populate this workspace form. 右键单击我原来的答案基本上只是填充此工作区表单的另一种方法。

  1. You want to go into DeveloperTools->Settings dialog like so: 您想进入DeveloperTools-> Settings对话框,如下所示:

在此输入图像描述

  1. Then go to the Workspace section and proceed with either the Local Server Solution or Remote Server Solution described next. 然后转到Workspace部分,继续执行下面描述的Local Server SolutionRemote Server Solution

Local Server Solution: 本地服务器方案:

Here are some screen prints for when your server and browser are running on the same machine: 以下是您的服务器和浏览器在同一台计算机上运行时的一些屏幕打印:

  1. From the workspace dialog, add the 'src/client' directory of your project, and a mapping to '/'. 在工作区对话框中,添加项目的'src / client'目录,并添加到'/'的映射。 Here's an example of what it should look like when the server ('ng serve') and the browser are on the same machine: 以下是服务器('ng serve')和浏览器在同一台机器上时应该是什么样子的示例:

在此输入图像描述

Remote Server Solution: 远程服务器方案:

Here are some screen prints for when your server and browser are running different machines: 以下是您的服务器和浏览器运行不同计算机时的一些屏幕打印:

  1. If your server is running on Linux, but your browser is running on a different machine, say windows, you can create a samba share on your linux and mount it as a drive on your windows. 如果您的服务器在Linux上运行,但您的浏览器在另一台计算机上运行,​​比如说Windows,您可以在Linux上创建一个samba共享并将其作为驱动器安装在Windows上。

From a windows command prompt (actually git bash shell ): 从Windows命令提示符(实际上是git bash shell ):

>$ net view  
Server Name            Remark
-------------------------------------------------------------------------------  
\\VT-VIRTUAL      vt-virtual-machine server (Samba, Linux Min  
The command completed successfully.

>$ net use l: '\\vt-virtual\vtstuff'
  1. Then set your workspace to look something like: 然后将工作区设置为: 在此输入图像描述

Conclusion: 结论:

Doing it this way retains the information across browser cyclings, so you should only need to do it once. 这样做可以在浏览器循环中保留信息,因此您只需要执行一次。 Perhaps the person who posted on 2016-07-30, where he said it worked for him "out of the box", somehow had this workspace form already set up. 也许是发布于2016-07-30的人,他说这对他起了“开箱即用”的作用,不知何故已经设置了这个工作空间形式。

I now understand this to be a Chrome Developer Tools setup issue and has nothing to do with Angular2 or Angular2 CLI . 我现在明白这是Chrome开发者工具设置问题,与Angular2Angular2 CLI无关。 It's just that people who use angular2 CLI need to do this workspace mapping because the source (.ts) files are not directly available in the runtime (dist) directory. 只是使用angular2 CLI的人需要执行此工作空间映射,因为源(.ts)文件不能直接在runtime(dist)目录中使用。

It seems like basic configuration of Angular CLI uses Webpack and all TypeScript files can be found in its src directory, thus allowing one to put a breakpoint without making use of debugger statement. 看起来Angular CLI的基本配置使用Webpack,并且可以在其src目录中找到所有TypeScript文件,从而允许在不使用调试器语句的情况下放置断点。

在此输入图像描述

Not sure what the issue was but I did not encounter that. 不知道问题是什么,但我没有遇到。 It worked right out of the box, no convoluted five-step process required. 它开箱即用,不需要复杂的五步过程。

The .ts files are not and should not be copied to the /dist folder. .ts文件不会也不应该复制到/ dist文件夹。 What is being copied are the .map files containing a reference back to your source folder. 正在复制的是包含对源文件夹的引用的.map文件。 Chrome developer tools shows all the .ts files under its Sources tab -- orange folders. Chrome开发者工具会在其“来源”标签下显示所有.ts文件 - 橙色文件夹。

I took no action other than to run "ng serve", browse to http://localhost:4200 and open developer tools. 除了运行“ng serve”之外,我没有采取任何行动,浏览到http:// localhost:4200并打开开发人员工具。 I can open one of the listed .ts files (they are listed in the orange folders), set breakpoints, etc. 我可以打开一个列出的.ts文件(它们列在橙色文件夹中),设置断点等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript文件是否在Chrome调试工具中被jambed? - TypeScript files get jambed in Chrome Debugger Tools? 自动在Typescript Angular cli项目中将node_modules之外的库的声明文件包括在内 - Automatically include declaration files of libraries outside of node_modules in Typescript Angular cli project 使用angular-cli创建TypeScript定义文件 - Create TypeScript definition files with angular-cli 如何将现有打字稿和其他必要文件从现有项目添加(复制和粘贴)到使用angular-cli创建的新项目中? - How to add (copy and paste) existing typescript and other necessary files from an existing project to a new project made with angular-cli? 在TypeScript中使用jQuery(在angular 2项目中) - Using jQuery in TypeScript (in an angular 2 project) 如何使用VS2015 Web项目使用TypeScript 1.8构建我的Typescript文件? - How do I get the VS2015 web project to build my Typescript files using TypeScript 1.8? 如何在同一个项目中使用带有Angular CLI和Express / Node的Typescript? - How to use Typescript with Angular CLI and Express/Node in the same project? 如何在Angular CLI项目中使用Typescript声明合并 - How to use Typescript Declaration Merging in an Angular CLI project 在节点服务器中为Angular项目导入打字稿文件 - Importing typescript files for Angular project in node server Angular CLI和TypeScript outDir - Angular CLI and TypeScript outDir
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM