Skip to content

Commit

Permalink
refactor: expose HookParameters type
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Jun 29, 2022
1 parent 489d1b2 commit efdd022
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ export interface SSRLoadedRenderer extends AstroRenderer {
};
}

export type HookParameters<Hook extends keyof AstroIntegration['hooks'], Fn = AstroIntegration['hooks'][Hook]> = Fn extends (...args: any) => any
? Parameters<Fn>[0]
: never;

export interface AstroIntegration {
/** The name of the integration. */
name: string;
Expand Down
9 changes: 2 additions & 7 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AddressInfo } from 'net';
import type { ViteDevServer } from 'vite';
import {
AstroConfig,
AstroIntegration,
HookParameters,
AstroRenderer,
BuildConfig,
RouteData,
Expand All @@ -14,11 +14,6 @@ import { mergeConfig } from '../core/config.js';
import type { ViteConfigWithSSR } from '../core/create-vite.js';
import { isBuildingToSSR } from '../core/util.js';

type Hooks<
Hook extends keyof AstroIntegration['hooks'],
Fn = AstroIntegration['hooks'][Hook]
> = Fn extends (...args: any) => any ? Parameters<Fn>[0] : never;

export async function runHookConfigSetup({
config: _config,
command,
Expand All @@ -45,7 +40,7 @@ export async function runHookConfigSetup({
* ```
*/
if (integration?.hooks?.['astro:config:setup']) {
const hooks: Hooks<'astro:config:setup'> = {
const hooks: HookParameters<'astro:config:setup'> = {
config: updatedConfig,
command,
addRenderer(renderer: AstroRenderer) {
Expand Down
16 changes: 7 additions & 9 deletions packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import type { AstroIntegration } from 'astro';
import mdx from '@mdx-js/rollup';
import mdxPlugin from '@mdx-js/rollup';

export default function (): AstroIntegration {
export default function mdx(): AstroIntegration {
return {
name: '@astrojs/mdx',
hooks: {
'astro:config:setup': ({ updateConfig, addPageExtension }) => {
const mdxPlugin = mdx({
jsx: true,
jsxImportSource: 'astro'
})

'astro:config:setup': ({ updateConfig, addPageExtension }: any) => {
addPageExtension('.mdx');
updateConfig({
vite: {
plugins: [
{
enforce: 'pre',
...mdxPlugin
...mdxPlugin({
jsx: true,
jsxImportSource: 'astro'
})
},
{
name: '@astrojs/mdx',
Expand Down

0 comments on commit efdd022

Please sign in to comment.