Make astro package play nice with node16 module resolution (#4182)

* 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 <princssdev@gmail.com>
This commit is contained in:
Aleksander Heintz 2022-08-25 20:57:11 +02:00 committed by GitHub
parent 243525b156
commit fcc36ac908
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 13 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Make type definitions available through package.json exports

View file

@ -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

View file

@ -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)
/**
@ -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'];
@ -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'];

View file

@ -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",

View file

@ -3,7 +3,7 @@
"include": ["src", "index.d.ts"],
"compilerOptions": {
"allowJs": true,
"declarationDir": "./dist/types",
"declarationDir": "./dist",
"module": "ES2020",
"outDir": "./dist",
"target": "ES2020",