Skip to content

Commit

Permalink
Make astro package play nice with node16 module resolution (#4182)
Browse files Browse the repository at this point in the history
* Make astro package play nice with node16 module resolution

Projects using node16 module resolution in typescript uses the new
exports and imports configuration from typescript to find definition
files. This mirrors how nodejs resolves the files. If a package contains
an exports map in the package.json, typescript will ignore the "types"
field (not sure how it plays with typesVersions). This moves the typings
hirearchy of definition files into the same hierarchy that astro
produces output files in, so that typescript can discover them.

Fixes: #4172

* Add changeset

* Reorder export keys

* Update paths inside .d.ts files

Co-authored-by: Princesseuh <[email protected]>
  • Loading branch information
Alxandr and Princesseuh authored Aug 25, 2022
1 parent 243525b commit fcc36ac
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-melons-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Make type definitions available through package.json exports
10 changes: 5 additions & 5 deletions packages/astro/astro-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ declare namespace astroHTML.JSX {
children?: Children;
}

type AstroBuiltinProps = import('./dist/types/@types/astro').AstroBuiltinProps;
type AstroBuiltinAttributes = import('./dist/types/@types/astro').AstroBuiltinAttributes;
type AstroDefineVarsAttribute = import('./dist/types/@types/astro').AstroDefineVarsAttribute;
type AstroScriptAttributes = import('./dist/types/@types/astro').AstroScriptAttributes &
type AstroBuiltinProps = import('./dist/@types/astro').AstroBuiltinProps;
type AstroBuiltinAttributes = import('./dist/@types/astro').AstroBuiltinAttributes;
type AstroDefineVarsAttribute = import('./dist/@types/astro').AstroDefineVarsAttribute;
type AstroScriptAttributes = import('./dist/@types/astro').AstroScriptAttributes &
AstroDefineVarsAttribute;
type AstroStyleAttributes = import('./dist/types/@types/astro').AstroStyleAttributes &
type AstroStyleAttributes = import('./dist/@types/astro').AstroStyleAttributes &
AstroDefineVarsAttribute;

// This is an unfortunate use of `any`, but unfortunately we can't make a type that works for every framework
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path="./client.d.ts" />

type Astro = import('./dist/types/@types/astro').AstroGlobal;
type Astro = import('./dist/@types/astro').AstroGlobal;

// We duplicate the description here because editors won't show the JSDoc comment from the imported type (but will for its properties, ex: Astro.request will show the AstroGlobal.request description)
/**
Expand All @@ -13,7 +13,7 @@ declare const Astro: Readonly<Astro>;
declare const Fragment: any;

declare module '*.md' {
type MD = import('./dist/types/@types/astro').MarkdownInstance<Record<string, any>>;
type MD = import('./dist/@types/astro').MarkdownInstance<Record<string, any>>;

export const frontmatter: MD['frontmatter'];
export const file: MD['file'];
Expand All @@ -30,7 +30,7 @@ declare module '*.md' {
}

declare module '*.mdx' {
type MDX = import('astro').MDXInstance<Record<string, any>>;
type MDX = import('./dist/@types/astro').MDXInstance<Record<string, any>>;

export const frontmatter: MDX['frontmatter'];
export const file: MDX['file'];
Expand Down
11 changes: 7 additions & 4 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
},
"bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://astro.build",
"types": "./dist/types/@types/astro.d.ts",
"types": "./dist/@types/astro.d.ts",
"typesVersions": {
"*": {
"app": [
"./dist/types/core/app/index"
"./dist/core/app/index"
],
"app/*": [
"./dist/types/core/app/*"
"./dist/core/app/*"
]
}
},
"exports": {
".": "./astro.js",
".": {
"types": "./dist/@types/astro.d.ts",
"default": "./astro.js"
},
"./env": "./env.d.ts",
"./client": "./client.d.ts",
"./client-base": "./client-base.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"include": ["src", "index.d.ts"],
"compilerOptions": {
"allowJs": true,
"declarationDir": "./dist/types",
"declarationDir": "./dist",
"module": "ES2020",
"outDir": "./dist",
"target": "ES2020",
Expand Down

0 comments on commit fcc36ac

Please sign in to comment.