Skip to content

Commit

Permalink
feat(unplugin): add client types
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 27, 2024
1 parent 0aad896 commit b8aecbc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ import Analytics from '~analytics/component.astro'
</html>
```

To make the TypeScript work, you can add `unplugin-analytics/types` to your corresponding `tsconfig.json`.

```json5
{
"compilerOptions": {
// ...
"types": [
"unplugin-analytics/client"
],
},
// ...
}
```

Or you can add TypeScript [triple-slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) to your `.d.ts` (i.e. for projects initialized by Vite, it may be `src/env.d.ts`).

```ts
// Your .d.ts file

/// <reference types="unplugin-analytics/client" />
```

Full example is located at [examples/astro](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/astro).

<br></details>
Expand Down
3 changes: 2 additions & 1 deletion examples/astro/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference types="astro/client" />
/// <reference types="astro/client" />
/// <reference types="unplugin-analytics/client" />
22 changes: 22 additions & 0 deletions packages/unplugin-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ import Analytics from '~analytics/component.astro'
</html>
```

To make the TypeScript work, you can add `unplugin-analytics/types` to your corresponding `tsconfig.json`.

```json5
{
"compilerOptions": {
// ...
"types": [
"unplugin-analytics/client"
],
},
// ...
}
```

Or you can add TypeScript [triple-slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) to your `.d.ts` (i.e. for projects initialized by Vite, it may be `src/env.d.ts`).

```ts
// Your .d.ts file

/// <reference types="unplugin-analytics/client" />
```

Full example is located at [examples/astro](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/astro).

<br></details>
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion packages/unplugin-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./*": "./*",
"./vite": {
"types": "./dist/vite.d.ts",
"import": "./dist/vite.mjs",
Expand All @@ -43,6 +44,9 @@
"types": "./dist/nuxt.d.ts",
"import": "./dist/nuxt.mjs",
"require": "./dist/nuxt.cjs"
},
"./client": {
"types": "./client.d.ts"
}
},
"main": "dist/index.cjs",
Expand Down Expand Up @@ -104,4 +108,4 @@
"engines": {
"node": ">=v20.10.0"
}
}
}
2 changes: 0 additions & 2 deletions packages/unplugin-analytics/src/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { generate } from '@unplugin-analytics/core';

import type { Options } from './plugin';

import './env.d';

export default (options: Options = {}) => ({
name: 'unplugin-analytics',
hooks: {
Expand Down

0 comments on commit b8aecbc

Please sign in to comment.