Skip to content

Commit

Permalink
feat: fix and deprecate addDts (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre authored Aug 22, 2024
1 parent 38be4e8 commit e2f85b5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
20 changes: 20 additions & 0 deletions .changeset/four-trains-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"astro-integration-kit": patch
---

Deprecates the `addDts` utility, it will be removed in a future minor release. You can anticipate this change by bumping your Astro peer dependency to ^4.14.0 and using [injectTypes](https://docs.astro.build/en/reference/integrations-reference/#injecttypes-options):

```diff
"astro:config:setup": (params) => {
- addDts(params, {
- name: "my-integration",
- content: `declare module "virtual:my-integration" {}`
- })
},
"astro:config:done": (params) => {
+ params.injectTypes({
+ filename: "types.d.ts",
+ content: `declare module "virtual:my-integration" {}`
+ })
}
```
5 changes: 5 additions & 0 deletions .changeset/pretty-lies-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-integration-kit": patch
---

Fixes an `addDts` incompatibility with Astro >=4.14.0
7 changes: 6 additions & 1 deletion docs/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import { pluginLineNumbers } from "@expressive-code/plugin-line-numbers";
import { defineConfig } from "astro/config";
import { version } from "../package/package.json";

const badge = (type: "new" | "updated" | "soon" | "removed") => ({
const badge = (
type: "new" | "updated" | "soon" | "removed" | "deprecated",
) => ({
variant: {
new: "success" as const,
updated: "caution" as const,
soon: "tip" as const,
removed: "danger" as const,
deprecated: "caution" as const,
}[type],
text: {
new: "New",
updated: "Updated",
soon: "Soon",
removed: "Removed",
deprecated: "Deprecated",
}[type],
});

Expand Down Expand Up @@ -139,6 +143,7 @@ export default defineConfig({
{
label: "addDts",
link: "/utilities/add-dts/",
badge: badge("deprecated"),
},
{
label: "addIntegration",
Expand Down
20 changes: 20 additions & 0 deletions docs/src/content/docs/utilities/add-dts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ title: addDts
description: Allows to inject .d.ts file in users project. It will create a file inside `.astro` and reference it from `src/env.d.ts`.
---

:::caution

This utility is deprecated and will be removed in a future minor release. Bump your Astro peer dependency to ^4.14.0 and use [injectTypes](https://docs.astro.build/en/reference/integrations-reference/#injecttypes-options):

```ts del={2-5} ins={8-11}
"astro:config:setup": (params) => {
addDts(params, {
name: "my-integration",
content: `declare module "virtual:my-integration" {}`
})
},
"astro:config:done": (params) => {
params.injectTypes({
filename: "types.d.ts",
content: `declare module "virtual:my-integration" {}`
})
}
```
:::

`addDts` allows you to inject a `.d.ts` file into the user's project. It will
create a file inside `.astro` and reference it from `src/env.d.ts`. For example:

Expand Down
55 changes: 42 additions & 13 deletions package/src/utilities/add-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,45 @@ const injectEnvDTS = ({

const envDTsContents = readFileSync(envDTsPath, "utf8");

if (envDTsContents.includes(`/// <reference types='${specifier}' />`)) {
return;
}
if (envDTsContents.includes(`/// <reference types="${specifier}" />`)) {
if (
envDTsContents.includes(`/// <reference types='${specifier}' />`) ||
envDTsContents.includes(`/// <reference types="${specifier}" />`)
) {
return;
}

const newEnvDTsContents = envDTsContents
.replace(
`/// <reference types='astro/client' />`,
`/// <reference types='astro/client' />\n/// <reference types='${specifier}' />`,
)
.replace(
`/// <reference types="astro/client" />`,
`/// <reference types="astro/client" />\n/// <reference types="${specifier}" />`,
);
const data: { singleQuotes: boolean; hasClient: boolean } =
envDTsContents.includes(`/// <reference types='astro/client' />`)
? {
singleQuotes: true,
hasClient: true,
}
: envDTsContents.includes(`/// <reference types="astro/client" />`)
? { singleQuotes: false, hasClient: true }
: envDTsContents.includes(
`/// <reference path="../.astro/types.d.ts" />`,
)
? {
singleQuotes: false,
hasClient: false,
}
: {
singleQuotes: true,
hasClient: false,
};

const referenceToReplace = `/// <reference ${
data.hasClient ? "types" : "path"
}=${data.singleQuotes ? `'` : `"`}${
data.hasClient ? "astro/client" : "../.astro/types.d.ts"
}${data.singleQuotes ? `'` : `"`} />`;

const newEnvDTsContents = envDTsContents.replace(
referenceToReplace,
`${referenceToReplace}\n/// <reference types=${
data.singleQuotes ? `'` : `"`
}${specifier}${data.singleQuotes ? `'` : `"`} />`,
);

// the odd case where the user changed the reference to astro/client
if (newEnvDTsContents === envDTsContents) {
Expand All @@ -52,6 +75,11 @@ const injectEnvDTS = ({
};

/**
* @deprecated
* This utility will be removed in a future minor release. Bump your Astro peer dependency to ^4.14.0
* and use [injectTypes](https://docs.astro.build/en/reference/integrations-reference/#injecttypes-options).
*
* @description
* Allows to inject .d.ts file in users project. It will create a file inside `.astro`
* and reference it from `src/env.d.ts`.
*
Expand Down Expand Up @@ -93,6 +121,7 @@ export const addDts = defineUtility("astro:config:setup")(
mkdirSync(dirname(filePath), { recursive: true });
writeFileSync(
filePath,
// TODO: extract to helper to use with core injectTypes
prettyPrint(
parse(content, {
parser: typescriptParser,
Expand Down
8 changes: 3 additions & 5 deletions playground/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
/// <reference types="../.astro/test-format.d.ts" />
/// <reference types="../.astro/test-integration.d.ts" />

/// <reference path='../.astro/types.d.ts' />
/// <reference types='../.astro/test-format.d.ts' />
/// <reference types='../.astro/test-integration.d.ts' />

0 comments on commit e2f85b5

Please sign in to comment.