diff --git a/README.md b/README.md index 604f3e2..3841a3d 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,28 @@ import Analytics from '~analytics/component.astro' ``` +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 + +/// +``` + Full example is located at [examples/astro](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/astro).
diff --git a/examples/astro/src/env.d.ts b/examples/astro/src/env.d.ts index 8c34fb4..bb9281b 100644 --- a/examples/astro/src/env.d.ts +++ b/examples/astro/src/env.d.ts @@ -1 +1,2 @@ -/// \ No newline at end of file +/// +/// diff --git a/packages/unplugin-analytics/README.md b/packages/unplugin-analytics/README.md index 9deb357..8ecdff6 100644 --- a/packages/unplugin-analytics/README.md +++ b/packages/unplugin-analytics/README.md @@ -101,6 +101,28 @@ import Analytics from '~analytics/component.astro' ``` +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 + +/// +``` + Full example is located at [examples/astro](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/astro).
diff --git a/packages/unplugin-analytics/src/env.d.ts b/packages/unplugin-analytics/client.d.ts similarity index 100% rename from packages/unplugin-analytics/src/env.d.ts rename to packages/unplugin-analytics/client.d.ts diff --git a/packages/unplugin-analytics/package.json b/packages/unplugin-analytics/package.json index d643913..46d90ed 100644 --- a/packages/unplugin-analytics/package.json +++ b/packages/unplugin-analytics/package.json @@ -29,6 +29,7 @@ "import": "./dist/index.mjs", "types": "./dist/index.d.ts" }, + "./*": "./*", "./vite": { "types": "./dist/vite.d.ts", "import": "./dist/vite.mjs", @@ -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", @@ -104,4 +108,4 @@ "engines": { "node": ">=v20.10.0" } -} +} \ No newline at end of file diff --git a/packages/unplugin-analytics/src/astro.ts b/packages/unplugin-analytics/src/astro.ts index 5a1d95c..5eba4ac 100644 --- a/packages/unplugin-analytics/src/astro.ts +++ b/packages/unplugin-analytics/src/astro.ts @@ -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: {