Skip to content
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

TypeScript conversion so we have accurately generated types for consumers #275

Merged
merged 14 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,28 @@ jobs:
- uses: wyvox/action-setup-pnpm@v3
- run: pnpm build
- run: pnpm i -f # just in case
- name: 'Change TS to ${{ matrix.typescript-scenario }}'
- name: 'test-types : ${{ matrix.typescript-scenario }}'
working-directory: ./test-types
run: 'pnpm add --save-dev ${{ matrix.typescript-scenario}}'

- name: 'Type checking'
- name: 'docs : ${{ matrix.typescript-scenario }}'
working-directory: ./docs
run: 'pnpm add --save-dev ${{ matrix.typescript-scenario}}'

- name: 'test-types'
working-directory: ./test-types
run: |
pnpm tsc -v
pnpm tsc --noEmit

- name: 'Glint in the docs app'
working-directory: ./docs
run: |
pnpm tsc -v
pnpm glint --version
pnpm glint


try-scenarios:
name: Tests - ${{ matrix.ember-try-scenario }}
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resolution-mode=highest

# super strict mode
auto-install-peers=false
resolve-peers-from-workspace-root=false
17 changes: 17 additions & 0 deletions addon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ declare module '@glint/environment-ember-loose/registry' {
}
```

Similarly, if you rely on a service registry, you'll want to import ember-page-title's service registry and extend from it.

```ts
import type PageTitle from 'ember-page-title/service-registry';

declare module '@ember/service' {
interface Registry extends PageTitle {
/* your local service entries here */
}
}
```

or, if you wish to manage how the service becomes registered yourself, you may import the service:
```ts
import type PageTitle from 'ember-page-title/services/page-title';
```

### Upgrading notes for 5.x to 6.x

- `ember-page-title` no longer requires the usage of `ember-cli-head`.
Expand Down
24 changes: 19 additions & 5 deletions addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@
"types": "./declarations/index.d.ts",
"default": "./dist/index.js"
},
"./*": {
"types": "./declarations/*",
"default": "./dist/*"
"./_app_/*": {
"default": "./dist/_app_/*"
},
"./services/page-title": {
"types": "./declarations/services/page-title.d.ts",
"default": "./dist/services/page-title.js"
},
"./helpers/page-title": {
"types": "./declarations/helpers/page-title.d.ts",
"default": "./dist/helpers/page-title.js"
},
"./test-support": {
"types": "./declarations/test-support/index.d.ts",
"default": "./dist/test-support/index.js"
},
"./template-registry": {
"types": "./declarations/template-registry.d.ts"
},
"./service-registry": {
"types": "./declarations/service-registry.d.ts"
},
"./addon-main.js": "./addon-main.js"
},
"files": [
Expand All @@ -40,14 +53,15 @@
"lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'",
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"lint:types": "glint",
"lint:types": "glint --declaration",
"prepack": "rollup --config",
"start": "concurrently 'npm:start:*'",
"start:js": "rollup --config --watch --no-watch.clearScreen",
"start:types": "glint --declaration --watch"
},
"dependencies": {
"@embroider/addon-shim": "^1.8.7"
"@embroider/addon-shim": "^1.8.7",
"@simple-dom/document": "^1.4.0"
},
"peerDependencies": {
"ember-source": ">= 3.28.0"
Expand Down
26 changes: 15 additions & 11 deletions addon/src/helpers/page-title.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-nocheck
import { inject as service } from '@ember/service';
import Helper from '@ember/component/helper';
import { guidFor } from '@ember/object/internals';

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

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

get tokenId(): string {
return guidFor(this);
}
tokenId = guidFor(this);

constructor() {
super(...arguments);
constructor(owner: Owner) {
super(owner);
this.tokens.push({ id: this.tokenId });
}

compute(params, _hash) {
const hash = {
..._hash,
compute(params: string[], userOptions: PageTitleHelperOptions) {
const options = {
...userOptions,
id: this.tokenId,
title: params.join(''),
};

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

Expand Down
27 changes: 27 additions & 0 deletions addon/src/private-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type SimpleDomDocument from '@simple-dom/document';

export type FastBootDocument = ReturnType<typeof SimpleDomDocument> & {
title?: string;
};

export interface PageTitleConfig {
/** The default separator to use between tokens. */
separator?: string;

/** The default prepend value to use. */
prepend?: boolean;

/** The default replace value to use. */
replace?: boolean | null;
}

export interface PageTitleToken extends PageTitleConfig {
id: string;
title?: string;
separator?: string;
prepend?: boolean;
replace?: boolean;
front?: unknown;
previous?: PageTitleToken | null;
next?: PageTitleToken | null;
}
14 changes: 14 additions & 0 deletions addon/src/service-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type PageTitleService from './services/page-title.ts';
import type { FastBootDocument } from './private-types.ts';

export default interface ServiceRegistry {
/**
* The service for managing the title of the page.
*/
'page-title': PageTitleService;
/**
* ⚠️ This service is not provided by ember-page-title,
* but is needed by ember-page-title
*/
'-document': FastBootDocument & { title?: string };
}
Loading
Loading