Skip to content

Commit 7effdc8

Browse files
Merge pull request #275 from ember-cli/typescript-take-two
TypeScript conversion so we have accurately generated types for consumers
2 parents 16b33ef + b74317b commit 7effdc8

24 files changed

Lines changed: 916 additions & 471 deletions

.github/workflows/ci.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,28 @@ jobs:
8282
- uses: wyvox/action-setup-pnpm@v3
8383
- run: pnpm build
8484
- run: pnpm i -f # just in case
85-
- name: 'Change TS to ${{ matrix.typescript-scenario }}'
85+
- name: 'test-types : ${{ matrix.typescript-scenario }}'
8686
working-directory: ./test-types
8787
run: 'pnpm add --save-dev ${{ matrix.typescript-scenario}}'
8888

89-
- name: 'Type checking'
89+
- name: 'docs : ${{ matrix.typescript-scenario }}'
90+
working-directory: ./docs
91+
run: 'pnpm add --save-dev ${{ matrix.typescript-scenario}}'
92+
93+
- name: 'test-types'
9094
working-directory: ./test-types
9195
run: |
9296
pnpm tsc -v
9397
pnpm tsc --noEmit
9498
99+
- name: 'Glint in the docs app'
100+
working-directory: ./docs
101+
run: |
102+
pnpm tsc -v
103+
pnpm glint --version
104+
pnpm glint
105+
106+
95107
try-scenarios:
96108
name: Tests - ${{ matrix.ember-try-scenario }}
97109
runs-on: ubuntu-latest

.npmrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resolution-mode=highest
2+
3+
# super strict mode
4+
auto-install-peers=false
5+
resolve-peers-from-workspace-root=false

addon/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ declare module '@glint/environment-ember-loose/registry' {
123123
}
124124
```
125125

126+
Similarly, if you rely on a service registry, you'll want to import ember-page-title's service registry and extend from it.
127+
128+
```ts
129+
import type PageTitle from 'ember-page-title/service-registry';
130+
131+
declare module '@ember/service' {
132+
interface Registry extends PageTitle {
133+
/* your local service entries here */
134+
}
135+
}
136+
```
137+
138+
or, if you wish to manage how the service becomes registered yourself, you may import the service:
139+
```ts
140+
import type PageTitle from 'ember-page-title/services/page-title';
141+
```
142+
126143
### Upgrading notes for 5.x to 6.x
127144

128145
- `ember-page-title` no longer requires the usage of `ember-cli-head`.

addon/package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@
1616
"types": "./declarations/index.d.ts",
1717
"default": "./dist/index.js"
1818
},
19-
"./*": {
20-
"types": "./declarations/*",
21-
"default": "./dist/*"
19+
"./_app_/*": {
20+
"default": "./dist/_app_/*"
21+
},
22+
"./services/page-title": {
23+
"types": "./declarations/services/page-title.d.ts",
24+
"default": "./dist/services/page-title.js"
25+
},
26+
"./helpers/page-title": {
27+
"types": "./declarations/helpers/page-title.d.ts",
28+
"default": "./dist/helpers/page-title.js"
2229
},
2330
"./test-support": {
2431
"types": "./declarations/test-support/index.d.ts",
2532
"default": "./dist/test-support/index.js"
2633
},
34+
"./template-registry": {
35+
"types": "./declarations/template-registry.d.ts"
36+
},
37+
"./service-registry": {
38+
"types": "./declarations/service-registry.d.ts"
39+
},
2740
"./addon-main.js": "./addon-main.js"
2841
},
2942
"files": [
@@ -40,14 +53,15 @@
4053
"lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'",
4154
"lint:js": "eslint . --cache",
4255
"lint:js:fix": "eslint . --fix",
43-
"lint:types": "glint",
56+
"lint:types": "glint --declaration",
4457
"prepack": "rollup --config",
4558
"start": "concurrently 'npm:start:*'",
4659
"start:js": "rollup --config --watch --no-watch.clearScreen",
4760
"start:types": "glint --declaration --watch"
4861
},
4962
"dependencies": {
50-
"@embroider/addon-shim": "^1.8.7"
63+
"@embroider/addon-shim": "^1.8.7",
64+
"@simple-dom/document": "^1.4.0"
5165
},
5266
"peerDependencies": {
5367
"ember-source": ">= 3.28.0"

addon/src/helpers/page-title.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// @ts-nocheck
21
import { inject as service } from '@ember/service';
32
import Helper from '@ember/component/helper';
43
import { guidFor } from '@ember/object/internals';
54

5+
import type Owner from '@ember/owner';
66
import type PageTitleService from '../services/page-title.ts';
7-
import type { PageTitleToken } from '../services/page-title.ts';
7+
import type { PageTitleToken } from '../private-types.ts';
88

99
export type PageTitleHelperOptions = Pick<
1010
PageTitleToken,
@@ -34,24 +34,28 @@ interface Signature {
3434
export default class PageTitle extends Helper<Signature> {
3535
@service('page-title') declare tokens: PageTitleService;
3636

37-
get tokenId(): string {
38-
return guidFor(this);
39-
}
37+
tokenId = guidFor(this);
4038

41-
constructor() {
42-
super(...arguments);
39+
constructor(owner: Owner) {
40+
super(owner);
4341
this.tokens.push({ id: this.tokenId });
4442
}
4543

46-
compute(params, _hash) {
47-
const hash = {
48-
..._hash,
44+
compute(params: string[], userOptions: PageTitleHelperOptions) {
45+
const options = {
46+
...userOptions,
4947
id: this.tokenId,
5048
title: params.join(''),
5149
};
5250

53-
this.tokens.push(hash);
51+
this.tokens.push(options);
5452
this.tokens.scheduleTitleUpdate();
53+
// We must return an empty value here because otherwise
54+
// invoking the pageTitle helper will render something
55+
// in the component it's used in, and we don't want that.
56+
//
57+
// pageTitle is a side-effecting helper.
58+
// We *synchronize* the document.title with our internal state.
5559
return '';
5660
}
5761

addon/src/private-types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type SimpleDomDocument from '@simple-dom/document';
2+
3+
export type FastBootDocument = ReturnType<typeof SimpleDomDocument> & {
4+
title?: string;
5+
};
6+
7+
export interface PageTitleConfig {
8+
/** The default separator to use between tokens. */
9+
separator?: string;
10+
11+
/** The default prepend value to use. */
12+
prepend?: boolean;
13+
14+
/** The default replace value to use. */
15+
replace?: boolean | null;
16+
}
17+
18+
export interface PageTitleToken extends PageTitleConfig {
19+
id: string;
20+
title?: string;
21+
separator?: string;
22+
prepend?: boolean;
23+
replace?: boolean;
24+
front?: unknown;
25+
previous?: PageTitleToken | null;
26+
next?: PageTitleToken | null;
27+
}

addon/src/service-registry.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type PageTitleService from './services/page-title.ts';
2+
import type { FastBootDocument } from './private-types.ts';
3+
4+
export default interface ServiceRegistry {
5+
/**
6+
* The service for managing the title of the page.
7+
*/
8+
'page-title': PageTitleService;
9+
/**
10+
* ⚠️ This service is not provided by ember-page-title,
11+
* but is needed by ember-page-title
12+
*/
13+
'-document': FastBootDocument & { title?: string };
14+
}

0 commit comments

Comments
 (0)