Skip to content

Commit

Permalink
fix: new build import bugs and types noop (#91)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Lefebvre <[email protected]>
  • Loading branch information
Fryuni and florian-lefebvre authored Mar 22, 2024
1 parent 694d254 commit 23a41b8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-bags-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-integration-kit": patch
---

Fixes runtime error when importing the new DB type extension
5 changes: 5 additions & 0 deletions .changeset/short-rice-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-integration-kit": patch
---

Fixes ESM import resolution after new build step
3 changes: 2 additions & 1 deletion package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"default": "./dist/plugins/index.js"
},
"./types/db": {
"types": "./types/db.d.ts"
"types": "./types/db.d.ts",
"default": "./types/noop.js"
}
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion package/src/utilities/add-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { dirname, relative } from "node:path";
import { fileURLToPath } from "node:url";
import type { AstroIntegrationLogger } from "astro";
import { parse, prettyPrint } from "recast";
import typescriptParser from "recast/parsers/typescript";
import typescriptParser from "recast/parsers/typescript.js";

const injectEnvDTS = ({
srcDir,
Expand Down
2 changes: 1 addition & 1 deletion package/src/utilities/inject-dev-route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { InjectedRoute } from "astro";
import type { HookParameters } from "../core/types";
import type { HookParameters } from "../core/types.js";

type HookParameterProperties = Pick<
HookParameters<"astro:config:setup">,
Expand Down
2 changes: 2 additions & 0 deletions package/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "astro/tsconfigs/strictest",
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16",
"jsx": "preserve"
},
"exclude": ["dist"]
Expand Down
18 changes: 18 additions & 0 deletions package/types/noop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Type-only import is not supported with the nice syntax for modules across packages.
* This requires libraries using the extended types provided by AIK with one of the syntaxes below:
* - `import type {} from 'astro-integration-kit/types/db';` with the empty `{}`
* - `/// <reference types="astro-integration-kit/types/db" />` with the triple-slash directive`
*
* None of those are as nice as simply `import 'astro-integration-kit/types/db';`.
*
* Using the plain import means it will attempt to import a JS file during module resolution,
* which would fail at runtime because the type extensions are only available as types.
*
* As a workaround for that, this file exists. The type extensions are declared in the package.json
* with this file as their runtime counterpart, so the import will succeed and not throw at runtime.
*
* This file is a no-op, it does nothing. It is just a placeholder to make the nice import work.
*
* @see https://discord.com/channels/830184174198718474/1197638002764152843/1220528418035208212
*/

0 comments on commit 23a41b8

Please sign in to comment.