Skip to content

Commit

Permalink
Move module declarations for Markdown and MDX so they're available ev…
Browse files Browse the repository at this point in the history
…erywhere (#4928)

* Move module declarations for Markdown and MDX to the proper file so they're globally available

* Remove tsconfig.json depending on client.d.ts unnecessarily
  • Loading branch information
Princesseuh committed Sep 30, 2022
1 parent 849fefd commit 7690849
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-crabs-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix module definition of Markdown and MDX files not being available outside Astro files
34 changes: 34 additions & 0 deletions packages/astro/client-base.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
/// <reference path="./import-meta.d.ts" />

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

export const frontmatter: MD['frontmatter'];
export const file: MD['file'];
export const url: MD['url'];
export const getHeadings: MD['getHeadings'];
/** @deprecated Renamed to `getHeadings()` */
export const getHeaders: () => void;
export const Content: MD['Content'];
export const rawContent: MD['rawContent'];
export const compiledContent: MD['compiledContent'];

const load: MD['default'];
export default load;
}

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

export const frontmatter: MDX['frontmatter'];
export const file: MDX['file'];
export const url: MDX['url'];
export const getHeadings: MDX['getHeadings'];
export const Content: MDX['Content'];
export const rawContent: MDX['rawContent'];
export const compiledContent: MDX['compiledContent'];

const load: MDX['default'];
export default load;
}

// Everything below are Vite's types (apart from image types, which are in `client.d.ts`)

// CSS modules
type CSSModuleClasses = { readonly [key: string]: string };

Expand Down
39 changes: 6 additions & 33 deletions packages/astro/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/// <reference path="./client.d.ts" />

// Caution! The types here are only available inside Astro files (injected automatically by our language server)
// As such, if the typings you're trying to add should be available inside ex: React components, they should instead
// be inside `client-base.d.ts`

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)
// We have to duplicate the description here because editors won't show the JSDoc comment from the imported type
// However, they will for its properties, ex: Astro.request will show the AstroGlobal.request description
/**
* Astro global available in all contexts in .astro files
*
Expand All @@ -12,38 +17,6 @@ declare const Astro: Readonly<Astro>;

declare const Fragment: any;

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

export const frontmatter: MD['frontmatter'];
export const file: MD['file'];
export const url: MD['url'];
export const getHeadings: MD['getHeadings'];
/** @deprecated Renamed to `getHeadings()` */
export const getHeaders: () => void;
export const Content: MD['Content'];
export const rawContent: MD['rawContent'];
export const compiledContent: MD['compiledContent'];

const load: MD['default'];
export default load;
}

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

export const frontmatter: MDX['frontmatter'];
export const file: MDX['file'];
export const url: MDX['url'];
export const getHeadings: MDX['getHeadings'];
export const Content: MDX['Content'];
export const rawContent: MDX['rawContent'];
export const compiledContent: MDX['compiledContent'];

const load: MDX['default'];
export default load;
}

declare module '*.html' {
const Component: { render(opts: { slots: Record<string, string> }): string };
export default Component;
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"declarationDir": "./dist",
"module": "ES2020",
"outDir": "./dist",
"target": "ES2020",
"types": ["./client"]
"target": "ES2020"
}
}

0 comments on commit 7690849

Please sign in to comment.