|
1 | | -# Angular template source inspector (prototype) |
| 1 | +# ngx-source-inspector |
2 | 2 |
|
3 | | -Hold Alt (Option on macOS), hover any element to see which component and which template line it |
4 | | -comes from, and Alt+click to open that exact line in your editor. |
| 3 | +Hold Alt (Option on macOS) and hover any element of your Angular app to see which component and |
| 4 | +which template line it comes from. Alt+click opens that line in your editor. |
5 | 5 |
|
6 | | -This is a small experiment to see how far the pieces already in Angular get us. It goes with |
7 | | -[angular/angular#70927](https://github.com/angular/angular/issues/70927), it's not a library. |
| 6 | +It only runs in development builds. |
8 | 7 |
|
9 | | -## Try it |
| 8 | +> **Experimental.** This relies on `enableTemplateSourceLocations`, a compiler option that Angular |
| 9 | +> doesn't document and doesn't cover by semver. It could change or go away in any release. Tested |
| 10 | +> with Angular 22.2. This isn't an official Angular package. The proposal to support this in Angular |
| 11 | +> itself is [angular/angular#70927](https://github.com/angular/angular/issues/70927). |
10 | 12 |
|
11 | | -Requires Node.js 22.22.3+ or 24.15+. |
| 13 | +## Install |
12 | 14 |
|
13 | 15 | ```sh |
14 | | -npm install |
15 | | -npm start |
| 16 | +ng add ngx-source-inspector |
| 17 | +``` |
| 18 | + |
| 19 | +This: |
| 20 | + |
| 21 | +- creates a `tsconfig.dev.json` that turns on `enableTemplateSourceLocations`, and uses it for the |
| 22 | + `development` build configuration only (or turns the option on in the tsconfig that configuration |
| 23 | + already uses); |
| 24 | +- adds `provideSourceInspector()` to your application providers. |
| 25 | + |
| 26 | +Then run `ng serve`, hold Alt and move the mouse over the page. |
| 27 | + |
| 28 | +### Manual setup |
| 29 | + |
| 30 | +1. Create a tsconfig for development builds, next to your app's tsconfig: |
| 31 | + |
| 32 | + ```json |
| 33 | + { |
| 34 | + "extends": "./tsconfig.app.json", |
| 35 | + "angularCompilerOptions": { |
| 36 | + "enableTemplateSourceLocations": true |
| 37 | + } |
| 38 | + } |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Use it in the `development` configuration of your build target in `angular.json`: |
| 42 | + |
| 43 | + ```json |
| 44 | + "development": { |
| 45 | + "tsConfig": "tsconfig.dev.json" |
| 46 | + } |
| 47 | + ``` |
| 48 | + |
| 49 | +3. Add the provider: |
| 50 | + |
| 51 | + ```ts |
| 52 | + import { provideSourceInspector } from 'ngx-source-inspector'; |
| 53 | + |
| 54 | + export const appConfig: ApplicationConfig = { |
| 55 | + providers: [provideSourceInspector()], |
| 56 | + }; |
| 57 | + ``` |
| 58 | + |
| 59 | +## Options |
| 60 | + |
| 61 | +```ts |
| 62 | +provideSourceInspector({ key: 'Shift' }); |
16 | 63 | ``` |
17 | 64 |
|
18 | | -Open http://localhost:4200, hold Alt and move the mouse over the page, then Alt+click something. |
| 65 | +| Option | Default | Description | |
| 66 | +| ----------------- | --------------------- | --------------------------------------------------------------------------------------------------- | |
| 67 | +| `key` | `'Alt'` | Key to hold: `'Alt'`, `'Shift'`, `'Control'` or `'Meta'`. Some Linux window managers use Alt+click. | |
| 68 | +| `openInEditorUrl` | `'/__open-in-editor'` | Dev server endpoint that opens a file. It gets `?file=<path>:<line>:<column>`. | |
| 69 | +| `onOpen` | | `(location) => void`, called instead of opening the editor. | |
19 | 70 |
|
20 | | -The editor is opened by the dev server's `/__open-in-editor` endpoint (Vite's `launch-editor` |
21 | | -middleware). It detects the editor that's currently running. If yours isn't detected, point |
22 | | -`LAUNCH_EDITOR` to it. It gets called with `<file> <line> <column>`: |
| 71 | +## Opening the editor |
| 72 | + |
| 73 | +The Angular CLI dev server runs on Vite, which serves `/__open-in-editor` (the `launch-editor` |
| 74 | +middleware). It detects the editor that's running: VS Code, Cursor, WebStorm, Zed and others. If |
| 75 | +yours isn't detected, set `LAUNCH_EDITOR`, which gets called with `<file> <line> <column>`: |
23 | 76 |
|
24 | 77 | ```sh |
25 | | -LAUNCH_EDITOR=code npm start |
| 78 | +LAUNCH_EDITOR=code ng serve |
26 | 79 | ``` |
27 | 80 |
|
28 | | -## How it works |
| 81 | +That endpoint comes from Vite and isn't documented by the Angular CLI. If it's missing, the |
| 82 | +inspector copies `path:line:column` to the clipboard and tells you so. |
29 | 83 |
|
30 | | -- `tsconfig.dev.json` turns on the `enableTemplateSourceLocations` compiler option. It's only used |
31 | | - by the `development` configuration in `angular.json`. With it, the compiler adds a |
32 | | - `data-ng-source-location="src/app/app.html@o:9,l:1,c:2"` attribute to every template element. |
33 | | -- `src/dev/source-inspector.ts` reads that attribute on hover and click. It falls back to the |
34 | | - component's debug info (class file and line) when the attribute isn't there. That fallback reads |
35 | | - the private `ɵcmp`. |
36 | | -- `src/main.ts` only loads the inspector in development. It checks `ngDevMode` directly rather |
37 | | - than calling `isDevMode()`: a production build replaces `ngDevMode` with `false`, so the import |
38 | | - is dropped and no inspector chunk ends up in `dist/`. With `isDevMode()` the bundler can't do |
39 | | - that, and the chunk was emitted (though never loaded). |
40 | | -- Only project-relative paths found on elements rendered by an Angular component are sent to the |
41 | | - dev server, so injected markup can't be used to open a file outside the project. |
42 | | - |
43 | | -## Things worth knowing |
44 | | - |
45 | | -- If you turn the option on in `tsconfig.app.json` instead, the template paths end up in the |
46 | | - production bundle. That's why it lives in a dev-only tsconfig here. Compare |
47 | | - `ng build` (no paths) with a production build using `tsconfig.dev.json`. |
48 | | -- Clicking a component's host element opens the template where the component is *used*, not |
49 | | - its own template. |
50 | | -- The demo covers an external template (`product-card`), an inline template (`price-tag.ts`) and |
51 | | - content projection. |
| 84 | +## Production builds |
| 85 | + |
| 86 | +Nothing ends up in production bundles, as long as the compiler option stays out of the tsconfig used |
| 87 | +for production: |
| 88 | + |
| 89 | +- `provideSourceInspector()` checks `ngDevMode`, which production builds replace with `false`, so it |
| 90 | + becomes an empty provider list and the inspector code is dropped from the bundle. |
| 91 | +- The template paths come from the compiler option. If you turn it on in `tsconfig.app.json`, every |
| 92 | + template path ships in your production bundle. That's why it goes in a development-only tsconfig. |
| 93 | + |
| 94 | +This repository's CI builds the demo app for production and fails if the bundle contains any |
| 95 | +inspector code, `data-ng-source-location` attribute or template path. To check your own app, build |
| 96 | +it for production and search the output for `data-ng-source-location`. |
| 97 | + |
| 98 | +The inspector only sends project-relative paths (no absolute paths, no `..`) found on elements |
| 99 | +rendered by an Angular component to the dev server, so injected markup can't be used to open an |
| 100 | +arbitrary file. |
52 | 101 |
|
53 | 102 | ## Known limitations |
54 | 103 |
|
55 | | -- **Shadow DOM**: in a component using `ViewEncapsulation.ShadowDom`, the event target is the host, |
56 | | - so you get the line where the component is used rather than the element inside it. |
57 | | -- **Paths**: the compiler makes paths relative to `rootDir`/`rootDirs` and may lowercase them on |
58 | | - case-insensitive file systems, while `/__open-in-editor` resolves them from the directory where |
59 | | - `ng serve` runs. With a custom `rootDir`, or in a multi-project workspace, Alt+click can point to |
60 | | - the wrong place. |
61 | | -- **Alt key**: some Linux window managers use Alt+click themselves, and on Windows AltGr is |
62 | | - reported as Alt. The modifier would need to be configurable in a real tool. |
63 | | -- Only `click` is intercepted, so `mousedown`/`pointerdown` handlers on the element still run. |
| 104 | +- The compiler makes paths relative to `rootDir`/`rootDirs`, while `/__open-in-editor` resolves them |
| 105 | + from the directory where `ng serve` runs. With a custom `rootDir`, or in some multi-project |
| 106 | + workspaces, Alt+click can open the wrong path. |
| 107 | +- A component's host element points to the template where the component is used, not to its own |
| 108 | + template. |
| 109 | +- Elements inside closed shadow roots can't be inspected. Open shadow roots work. |
| 110 | +- Elements rendered by libraries compiled without the option (such as UI component libraries) have |
| 111 | + no location. The inspector uses the nearest element from your own templates instead. |
| 112 | + |
| 113 | +## How it works |
| 114 | + |
| 115 | +With `enableTemplateSourceLocations`, the Angular compiler adds a |
| 116 | +`data-ng-source-location="src/app/app.html@o:9,l:1,c:2"` attribute to every element of your |
| 117 | +templates. The inspector reads it on hover, uses Angular's `ng.getOwningComponent()` debugging |
| 118 | +utility to find the component, and calls the dev server to open the file. |
| 119 | + |
| 120 | +## Developing |
| 121 | + |
| 122 | +Requires Node.js 22.22.3+ or 24.15+. |
| 123 | + |
| 124 | +```sh |
| 125 | +npm install |
| 126 | +npm run build:lib # library and ng-add schematic, in dist/ngx-source-inspector |
| 127 | +npm start # demo app with the inspector |
| 128 | +npm test # unit tests and schematic tests |
| 129 | +npm run check:production # production build of the demo, checked for leftovers |
| 130 | +``` |
| 131 | + |
| 132 | +The first prototype, linked from the Angular issue, is tagged |
| 133 | +[`prototype`](https://github.com/aminesbdev/angular-source-inspector/tree/prototype). |
| 134 | + |
| 135 | +## License |
| 136 | + |
| 137 | +MIT |
0 commit comments