Skip to content

Commit 4fafdd8

Browse files
committed
Turn the prototype into the ngx-source-inspector package
The inspector now lives in an Angular library under projects/ngx-source-inspector, published as `ngx-source-inspector`: - `provideSourceInspector()` installs it in development builds only. It checks `ngDevMode`, so production builds get an empty provider list and none of its code. It also does nothing during server-side rendering. - `ng add ngx-source-inspector` turns on `enableTemplateSourceLocations` for the development configuration only (a new tsconfig.dev.json, or the tsconfig that configuration already uses) and adds the provider. - No private API anymore: the component name comes from `ng.getOwningComponent` and `ng.getHostElement`. - Works in open shadow roots, swallows the pointer events of the click, hides the overlay on blur and keeps it in place on scroll, updates at most once per frame, and can be installed only once. - The key is configurable, and when `/__open-in-editor` is missing the location is copied to the clipboard instead. The demo app uses the package. CI builds it, runs the unit and schematic tests, and builds the demo for production to check that no inspector code or template path ends up in the bundle.
1 parent a977f07 commit 4fafdd8

31 files changed

Lines changed: 2218 additions & 321 deletions

‎.github/workflows/ci.yml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build-and-test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 24
16+
cache: npm
17+
- run: npm ci
18+
- run: npm run build:lib
19+
- run: npm test
20+
- name: Check that nothing ends up in production bundles
21+
run: npm run check:production

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Amine
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 120 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,137 @@
1-
# Angular template source inspector (prototype)
1+
# ngx-source-inspector
22

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.
55

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.
87

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).
1012
11-
Requires Node.js 22.22.3+ or 24.15+.
13+
## Install
1214

1315
```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' });
1663
```
1764

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. |
1970

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>`:
2376

2477
```sh
25-
LAUNCH_EDITOR=code npm start
78+
LAUNCH_EDITOR=code ng serve
2679
```
2780

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.
2983

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.
52101

53102
## Known limitations
54103

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

‎angular.json‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
"input": "public"
2525
}
2626
],
27-
"styles": [
28-
"src/styles.css"
29-
]
27+
"styles": ["src/styles.css"]
3028
},
3129
"configurations": {
3230
"production": {
@@ -69,6 +67,32 @@
6967
"builder": "@angular/build:unit-test"
7068
}
7169
}
70+
},
71+
"ngx-source-inspector": {
72+
"projectType": "library",
73+
"root": "projects/ngx-source-inspector",
74+
"sourceRoot": "projects/ngx-source-inspector/src",
75+
"prefix": "nsi",
76+
"architect": {
77+
"build": {
78+
"builder": "@angular/build:ng-packagr",
79+
"configurations": {
80+
"production": {
81+
"tsConfig": "projects/ngx-source-inspector/tsconfig.lib.prod.json"
82+
},
83+
"development": {
84+
"tsConfig": "projects/ngx-source-inspector/tsconfig.lib.json"
85+
}
86+
},
87+
"defaultConfiguration": "production"
88+
},
89+
"test": {
90+
"builder": "@angular/build:unit-test",
91+
"options": {
92+
"tsConfig": "projects/ngx-source-inspector/tsconfig.spec.json"
93+
}
94+
}
95+
}
7296
}
7397
}
74-
}
98+
}

0 commit comments

Comments
 (0)