-
-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add Angular devtools adapter #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlemTuzlak
wants to merge
8
commits into
main
Choose a base branch
from
worktree-kind-orbiting-dolphin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
416b8e7
feat(angular-devtools): create angular adapter package
AlemTuzlak f376a86
fix(angular-devtools): add reactivity for input changes, remove unuse…
AlemTuzlak 658c8ad
feat(devtools-utils): add angular support
AlemTuzlak 5db9c6f
fix: resolve angular build issues
AlemTuzlak 94713c3
ci: apply automated fixes
autofix-ci[bot] cd9e8ef
fix: resolve lint, knip, and sherif issues
AlemTuzlak b56b0af
Merge branch 'main' into worktree-kind-orbiting-dolphin
AlemTuzlak e57c0fe
feat: add Angular support with setup, adapter, and custom plugin docu…
AlemTuzlak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // @ts-check | ||
|
|
||
| import rootConfig from '../../eslint.config.js' | ||
|
|
||
| export default [...rootConfig] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| { | ||
| "name": "@tanstack/angular-devtools", | ||
| "version": "0.0.1", | ||
| "description": "TanStack Devtools is a set of tools for building advanced devtools for your Angular application.", | ||
| "author": "Tanner Linsley", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/TanStack/devtools.git", | ||
| "directory": "packages/angular-devtools" | ||
| }, | ||
| "homepage": "https://tanstack.com/devtools", | ||
| "funding": { | ||
| "type": "github", | ||
| "url": "https://github.com/sponsors/tannerlinsley" | ||
| }, | ||
| "keywords": [ | ||
| "angular", | ||
| "devtools" | ||
| ], | ||
| "type": "module", | ||
| "types": "dist/esm/index.d.ts", | ||
| "module": "dist/esm/index.js", | ||
| "exports": { | ||
| ".": { | ||
| "import": { | ||
| "types": "./dist/esm/index.d.ts", | ||
| "default": "./dist/esm/index.js" | ||
| } | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "sideEffects": false, | ||
| "engines": { | ||
| "node": ">=18" | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "src" | ||
| ], | ||
| "scripts": { | ||
| "clean": "premove ./build ./dist", | ||
| "test:eslint": "eslint ./src", | ||
| "test:lib": "vitest --passWithNoTests", | ||
| "test:lib:dev": "pnpm test:lib --watch", | ||
| "test:types": "tsc", | ||
| "test:build": "publint --strict", | ||
| "build": "vite build" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/devtools": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@angular/compiler": "^20.0.0", | ||
| "@angular/core": "^20.0.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "@angular/core": ">=19.0.0", | ||
| "@angular/platform-browser": ">=19.0.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { | ||
| ApplicationRef, | ||
| Component, | ||
| DestroyRef, | ||
| ElementRef, | ||
| EnvironmentInjector, | ||
| afterNextRender, | ||
| createComponent, | ||
| effect, | ||
| inject, | ||
| input, | ||
| } from '@angular/core' | ||
| import { PLUGIN_CONTAINER_ID, TanStackDevtoolsCore } from '@tanstack/devtools' | ||
| import type { ComponentRef, Type } from '@angular/core' | ||
| import type { TanStackDevtoolsPlugin } from '@tanstack/devtools' | ||
| import type { | ||
| TanStackDevtoolsAngularInit, | ||
| TanStackDevtoolsAngularPlugin, | ||
| } from './types' | ||
|
|
||
| @Component({ | ||
| selector: 'tanstack-devtools', | ||
| standalone: true, | ||
| template: '<div #devtoolsHost></div>', | ||
| }) | ||
| export class TanStackDevtoolsComponent { | ||
| plugins = input<TanStackDevtoolsAngularInit['plugins']>() | ||
| config = input<TanStackDevtoolsAngularInit['config']>() | ||
| eventBusConfig = input<TanStackDevtoolsAngularInit['eventBusConfig']>() | ||
|
|
||
| private hostRef = inject(ElementRef) | ||
| private appRef = inject(ApplicationRef) | ||
| private injector = inject(EnvironmentInjector) | ||
| private destroyRef = inject(DestroyRef) | ||
|
|
||
| private componentRefs: Array<ComponentRef<any>> = [] | ||
| private devtools: TanStackDevtoolsCore | null = null | ||
|
|
||
| constructor() { | ||
| afterNextRender(() => { | ||
| const hostEl = this.hostRef.nativeElement.querySelector('div') | ||
| if (!hostEl) return | ||
|
|
||
| const pluginsMap = this.getPluginsMap() | ||
|
|
||
| this.devtools = new TanStackDevtoolsCore({ | ||
| config: this.config(), | ||
| eventBusConfig: this.eventBusConfig(), | ||
| plugins: pluginsMap, | ||
| }) | ||
|
|
||
| this.devtools.mount(hostEl) | ||
| }) | ||
|
|
||
| effect(() => { | ||
| const plugins = this.plugins() | ||
| const config = this.config() | ||
| const eventBusConfig = this.eventBusConfig() | ||
|
|
||
| if (this.devtools) { | ||
| this.devtools.setConfig({ | ||
| config, | ||
| eventBusConfig, | ||
| plugins: plugins?.map((p) => this.convertPlugin(p)) ?? [], | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| this.destroyRef.onDestroy(() => { | ||
| this.destroyAllComponents() | ||
| if (this.devtools) { | ||
| this.devtools.unmount() | ||
| this.devtools = null | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private getPluginsMap(): Array<TanStackDevtoolsPlugin> { | ||
| const plugins = this.plugins() | ||
| if (!plugins) return [] | ||
|
|
||
| return plugins.map((plugin) => this.convertPlugin(plugin)) | ||
| } | ||
|
|
||
| private convertPlugin( | ||
| plugin: TanStackDevtoolsAngularPlugin, | ||
| ): TanStackDevtoolsPlugin { | ||
| return { | ||
| id: plugin.id, | ||
| defaultOpen: plugin.defaultOpen, | ||
| name: | ||
| typeof plugin.name === 'string' | ||
| ? plugin.name | ||
| : (e, theme) => { | ||
| this.renderComponent(plugin.name as Type<any>, e, { | ||
| theme, | ||
| ...(plugin.inputs ?? {}), | ||
| }) | ||
| }, | ||
| render: (e, theme) => { | ||
| this.renderComponent(plugin.component, e, { | ||
| theme, | ||
| ...(plugin.inputs ?? {}), | ||
| }) | ||
| }, | ||
| destroy: (pluginId) => { | ||
| this.destroyComponentsInContainer(`${PLUGIN_CONTAINER_ID}-${pluginId}`) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| private renderComponent( | ||
| component: Type<any>, | ||
| container: HTMLElement, | ||
| inputs: Record<string, unknown>, | ||
| ) { | ||
| const componentRef = createComponent(component, { | ||
| environmentInjector: this.injector, | ||
| hostElement: container, | ||
| }) | ||
|
|
||
| for (const [key, value] of Object.entries(inputs)) { | ||
| componentRef.setInput(key, value) | ||
| } | ||
|
|
||
| this.appRef.attachView(componentRef.hostView) | ||
| componentRef.changeDetectorRef.detectChanges() | ||
| this.componentRefs.push(componentRef) | ||
| } | ||
|
|
||
| private destroyComponentsInContainer(containerId: string) { | ||
| this.componentRefs = this.componentRefs.filter((ref) => { | ||
| const el = ref.location.nativeElement as HTMLElement | ||
| if (el.parentElement?.id === containerId) { | ||
| this.appRef.detachView(ref.hostView) | ||
| ref.destroy() | ||
| return false | ||
| } | ||
| return true | ||
| }) | ||
| } | ||
|
|
||
| private destroyAllComponents() { | ||
| for (const ref of this.componentRefs) { | ||
| this.appRef.detachView(ref.hostView) | ||
| ref.destroy() | ||
| } | ||
| this.componentRefs = [] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export { TanStackDevtoolsComponent } from './devtools' | ||
|
|
||
| export type { | ||
| TanStackDevtoolsAngularPlugin, | ||
| TanStackDevtoolsAngularInit, | ||
| } from './types' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { Type } from '@angular/core' | ||
| import type { | ||
| ClientEventBusConfig, | ||
| TanStackDevtoolsConfig, | ||
| } from '@tanstack/devtools' | ||
|
|
||
| export type TanStackDevtoolsAngularPlugin = { | ||
| id?: string | ||
| component: Type<any> | ||
| name: string | Type<any> | ||
| inputs?: Record<string, any> | ||
| defaultOpen?: boolean | ||
| } | ||
|
|
||
| export interface TanStackDevtoolsAngularInit { | ||
| /** | ||
| * Array of plugins to be used in the devtools. | ||
| * Each plugin should have a `component` that is an Angular component class. | ||
| * | ||
| * Example: | ||
| * ```typescript | ||
| * @Component({ | ||
| * template: `<tanstack-devtools [plugins]="plugins" />`, | ||
| * imports: [TanStackDevtoolsComponent], | ||
| * }) | ||
| * class AppComponent { | ||
| * plugins: TanStackDevtoolsAngularPlugin[] = [ | ||
| * { name: 'My Plugin', component: MyPluginComponent } | ||
| * ]; | ||
| * } | ||
| * ``` | ||
| */ | ||
| plugins?: Array<TanStackDevtoolsAngularPlugin> | ||
| /** | ||
| * Configuration for the devtools shell. These configuration options are used to set the | ||
| * initial state of the devtools when it is started for the first time. Afterwards, | ||
| * the settings are persisted in local storage and changed through the settings panel. | ||
| */ | ||
| config?: Partial<TanStackDevtoolsConfig> | ||
| /** | ||
| * Configuration for the TanStack Devtools client event bus. | ||
| */ | ||
| eventBusConfig?: ClientEventBusConfig | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "experimentalDecorators": true | ||
| }, | ||
| "include": ["src", "eslint.config.js", "vite.config.ts", "tests"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { defineConfig, mergeConfig } from 'vitest/config' | ||
| import { tanstackViteConfig } from '@tanstack/vite-config' | ||
| import packageJson from './package.json' | ||
|
|
||
| const config = defineConfig({ | ||
| plugins: [], | ||
| test: { | ||
| name: packageJson.name, | ||
| dir: './tests', | ||
| watch: false, | ||
| environment: 'jsdom', | ||
| globals: true, | ||
| }, | ||
| }) | ||
|
|
||
| export default mergeConfig( | ||
| config, | ||
| tanstackViteConfig({ | ||
| entry: ['./src/index.ts'], | ||
| srcDir: './src', | ||
| externalDeps: [ | ||
| '@angular/core', | ||
| '@angular/platform-browser', | ||
| 'rxjs', | ||
| 'zone.js', | ||
| ], | ||
| cjs: false, | ||
| }), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './panel' | ||
| export * from './plugin' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, most other TanStack Angular adapters are using Angular CLI (and/or
ng-packagr) itself:https://github.com/TanStack/form/blob/main/packages/angular-form/package.json#L25
Because of incompatibilities of using Vite for builds.