From fcc36ac908429733b1d9e51caddbc7590f9eeea5 Mon Sep 17 00:00:00 2001 From: Aleksander Heintz Date: Thu, 25 Aug 2022 20:57:11 +0200 Subject: [PATCH] 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 --- .changeset/perfect-melons-wonder.md | 5 +++++ packages/astro/astro-jsx.d.ts | 10 +++++----- packages/astro/env.d.ts | 6 +++--- packages/astro/package.json | 11 +++++++---- packages/astro/tsconfig.json | 2 +- 5 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 .changeset/perfect-melons-wonder.md diff --git a/.changeset/perfect-melons-wonder.md b/.changeset/perfect-melons-wonder.md new file mode 100644 index 000000000..0c45ce87d --- /dev/null +++ b/.changeset/perfect-melons-wonder.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Make type definitions available through package.json exports diff --git a/packages/astro/astro-jsx.d.ts b/packages/astro/astro-jsx.d.ts index bdf6b6aa2..27a37bd42 100644 --- a/packages/astro/astro-jsx.d.ts +++ b/packages/astro/astro-jsx.d.ts @@ -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 diff --git a/packages/astro/env.d.ts b/packages/astro/env.d.ts index 4b6ce7750..a579d78ce 100644 --- a/packages/astro/env.d.ts +++ b/packages/astro/env.d.ts @@ -1,6 +1,6 @@ /// -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; declare const Fragment: any; declare module '*.md' { - type MD = import('./dist/types/@types/astro').MarkdownInstance>; + type MD = import('./dist/@types/astro').MarkdownInstance>; 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>; + type MDX = import('./dist/@types/astro').MDXInstance>; export const frontmatter: MDX['frontmatter']; export const file: MDX['file']; diff --git a/packages/astro/package.json b/packages/astro/package.json index 711b8edf0..d674ef69e 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -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", diff --git a/packages/astro/tsconfig.json b/packages/astro/tsconfig.json index 58e865c0c..a51690d2a 100644 --- a/packages/astro/tsconfig.json +++ b/packages/astro/tsconfig.json @@ -3,7 +3,7 @@ "include": ["src", "index.d.ts"], "compilerOptions": { "allowJs": true, - "declarationDir": "./dist/types", + "declarationDir": "./dist", "module": "ES2020", "outDir": "./dist", "target": "ES2020",