Skip to content

Commit 6f17820

Browse files
committed
refactor: convert pluginOptions into MdxPluginOptions instead of extending PluginOptions
1 parent 2f3efd9 commit 6f17820

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

packages/gatsby-plugin-mdx/src/compile-mdx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import deepmerge from "deepmerge"
2-
import type { NodePluginArgs } from "gatsby"
2+
import type { NodePluginArgs, PluginOptions } from "gatsby"
33
import type { ProcessorOptions } from "@mdx-js/mdx"
44
import type { IFileNode, IMdxMetadata, IMdxNode } from "./types"
55

@@ -77,7 +77,7 @@ export const compileMDXWithCustomOptions = async ({
7777
cache,
7878
mdxNode,
7979
}: {
80-
pluginOptions: IMdxPluginOptions
80+
pluginOptions: PluginOptions
8181
customOptions: Partial<IMdxPluginOptions>
8282
getNode: NodePluginArgs["getNode"]
8383
getNodesByType: NodePluginArgs["getNodesByType"]

packages/gatsby-plugin-mdx/src/gatsby-node.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import { sentenceCase } from "change-case"
88
import fs from "fs-extra"
99
import grayMatter from "gray-matter"
1010

11-
import {
12-
defaultOptions,
13-
enhanceMdxOptions,
14-
IMdxPluginOptions,
15-
} from "./plugin-options"
11+
import { defaultOptions, enhanceMdxOptions } from "./plugin-options"
1612
import { IGatsbyLayoutLoaderOptions } from "./gatsby-layout-loader"
1713
import { compileMDX, compileMDXWithCustomOptions } from "./compile-mdx"
1814
import { IGatsbyMDXLoaderOptions } from "./gatsby-mdx-loader"
@@ -25,7 +21,7 @@ import { ERROR_MAP } from "./error-utils"
2521
export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] =
2622
async (
2723
{ actions, loaders, getNode, getNodesByType, pathPrefix, reporter, cache },
28-
pluginOptions: IMdxPluginOptions
24+
pluginOptions
2925
) => {
3026
const mdxNodes = getNodesByType(`Mdx`)
3127
const nodeMap: NodeMap = new Map()
@@ -105,17 +101,18 @@ export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] =
105101
*/
106102
export const resolvableExtensions: GatsbyNode["resolvableExtensions"] = (
107103
_data,
108-
pluginOptions: IMdxPluginOptions
104+
pluginOptions
109105
) => defaultOptions(pluginOptions).extensions
110106

111107
/**
112108
* Convert MDX to JSX so that Gatsby can extract the GraphQL queries and render the pages.
113109
*/
114110
export const preprocessSource: GatsbyNode["preprocessSource"] = async (
115111
{ filename, getNode, getNodesByType, pathPrefix, reporter, cache },
116-
pluginOptions: IMdxPluginOptions
112+
pluginOptions
117113
) => {
118-
const { extensions } = defaultOptions(pluginOptions)
114+
const options = defaultOptions(pluginOptions)
115+
const { extensions } = options
119116
const splitPath = filename.split(`__mdxPath=`)
120117
const mdxPath = splitPath.length === 2 ? splitPath[1] : splitPath[0]
121118

@@ -169,7 +166,7 @@ export const preprocessSource: GatsbyNode["preprocessSource"] = async (
169166
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
170167
async (
171168
{ getNode, getNodesByType, pathPrefix, reporter, cache, actions, schema },
172-
pluginOptions: IMdxPluginOptions
169+
pluginOptions
173170
) => {
174171
const { createTypes } = actions
175172
const typeDefs = [
@@ -271,7 +268,7 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
271268

272269
// eslint-disable-next-line @typescript-eslint/naming-convention
273270
export const unstable_shouldOnCreateNode: GatsbyNode["unstable_shouldOnCreateNode"] =
274-
({ node }: { node: FileSystemNode }, pluginOptions: IMdxPluginOptions) => {
271+
({ node }: { node: FileSystemNode }, pluginOptions) => {
275272
const { extensions } = defaultOptions(pluginOptions)
276273
return node.internal.type === `File` && extensions.includes(node.ext)
277274
}
@@ -324,7 +321,7 @@ export const onCreateNode: GatsbyNode<FileSystemNode>["onCreateNode"] = async ({
324321
*/
325322
export const onCreatePage: GatsbyNode["onCreatePage"] = async (
326323
{ page, actions },
327-
pluginOptions: IMdxPluginOptions
324+
pluginOptions
328325
) => {
329326
const { createPage, deletePage } = actions
330327
const { extensions } = defaultOptions(pluginOptions)

packages/gatsby-plugin-mdx/src/plugin-options.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { ProcessorOptions } from "@mdx-js/mdx"
2-
import type { PluginOptions, GatsbyCache, NodePluginArgs } from "gatsby"
2+
import type { GatsbyCache, NodePluginArgs, PluginOptions } from "gatsby"
33
import deepmerge from "deepmerge"
44
import { IPluginRefObject } from "gatsby-plugin-utils/types"
55
import { getSourcePluginsAsRemarkPlugins } from "./get-source-plugins-as-remark-plugins"
66
import rehypeMdxMetadataExtractor from "./rehype-metadata-extractor"
77
import { remarkMdxHtmlPlugin } from "./remark-mdx-html-plugin"
88
import { remarkPathPlugin } from "./remark-path-prefix-plugin"
99

10-
export interface IMdxPluginOptions extends Partial<PluginOptions> {
10+
export interface IMdxPluginOptions {
1111
extensions: [string]
1212
mdxOptions: ProcessorOptions
1313
gatsbyRemarkPlugins?: [IPluginRefObject]
@@ -19,7 +19,7 @@ interface IHelpers {
1919
reporter: NodePluginArgs["reporter"]
2020
cache: GatsbyCache
2121
}
22-
type MdxDefaultOptions = (pluginOptions: IMdxPluginOptions) => IMdxPluginOptions
22+
type MdxDefaultOptions = (pluginOptions: PluginOptions) => IMdxPluginOptions
2323

2424
export const defaultOptions: MdxDefaultOptions = pluginOptions => {
2525
const mdxOptions: ProcessorOptions = {
@@ -28,19 +28,20 @@ export const defaultOptions: MdxDefaultOptions = pluginOptions => {
2828
providerImportSource: `@mdx-js/react`,
2929
jsxRuntime: `classic`,
3030
}
31-
const options: IMdxPluginOptions = deepmerge(
32-
{
33-
extensions: [`.mdx`],
34-
mdxOptions,
35-
},
31+
const defaults = {
32+
extensions: [`.mdx`],
33+
mdxOptions,
34+
}
35+
const options = deepmerge(
36+
defaults,
3637
pluginOptions
37-
)
38+
) as unknown as IMdxPluginOptions
3839

3940
return options
4041
}
4142

4243
type EnhanceMdxOptions = (
43-
pluginOptions: IMdxPluginOptions,
44+
pluginOptions: PluginOptions,
4445
helpers: IHelpers
4546
) => Promise<ProcessorOptions>
4647

packages/gatsby/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ export interface GatsbySSR<
795795
}
796796

797797
export interface PluginOptions {
798-
// plugins: unknown[]
798+
plugins: unknown[]
799799
[key: string]: unknown
800800
}
801801

0 commit comments

Comments
 (0)