Skip to content

Commit

Permalink
🔨 improve(none): @roots/bud-mdx (#2630)
Browse files Browse the repository at this point in the history
- expose `bud.mdx`
- formatting

## Type of change

**PATCH: backwards compatible change**
  • Loading branch information
kellymears authored Jul 28, 2024
1 parent ca765f1 commit d4a5eb2
Showing 2 changed files with 36 additions and 42 deletions.
65 changes: 31 additions & 34 deletions sources/@roots/bud-mdx/src/extension.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import type {Bud} from '@roots/bud-framework'
import {Extension} from '@roots/bud-framework/extension'
import {
bind,
expose,
label,
options,
} from '@roots/bud-framework/extension/decorators'
@@ -20,51 +21,38 @@ interface Options {
rehypePlugins: {},
remarkPlugins: {},
})
@expose(`mdx`)
class BudMDX extends Extension<Options> {
/**
* Get rehype plugins
*/
public declare getRehypePlugins: () => Options[`rehypePlugins`]

/**
* Get remark plugins
*/
public declare getRemarkPlugins: () => Options[`remarkPlugins`]

/**
* Rehype plugins
*/
public declare rehypePlugins: Options[`rehypePlugins`]
/**
* Remark plugins
* Get rehype plugins
*/
public declare remarkPlugins: Options[`remarkPlugins`]
public declare getRehypePlugins: () => Options[`rehypePlugins`]
/**
* Set rehype plugins
*/
public declare setRehypePlugins: (
plugins: Options[`rehypePlugins`],
) => this

/**
* Remark plugins
*/
public declare remarkPlugins: Options[`remarkPlugins`]
/**
* Get remark plugins
*/
public declare getRemarkPlugins: () => Options[`remarkPlugins`]
/**
* Set remark plugins
*/
public declare setRemarkPlugins: (
plugins: Options[`remarkPlugins`],
) => this

/**
* {@link Extension.boot}
*/
@bind
public override async boot({build}: Bud) {
build.setRule(`mdx`, {
include: [({path}) => path(`@src`)],
test: /\.mdx?$/,
use: [...(build.rules.js.use ?? []), `mdx`],
})
}

/**
* {@link Extension.register}
*/
@@ -73,24 +61,33 @@ class BudMDX extends Extension<Options> {
const loader = await this.resolve(`@mdx-js/loader`, import.meta.url)
if (!loader) return this.logger.error(`MDX loader not found`)

build.setLoader(`mdx`, `@mdx-js/loader`).setItem(`mdx`, {
loader: `mdx`,
options: () => ({
rehypePlugins: Object.values(this.get(`rehypePlugins`)),
remarkPlugins: Object.values(this.get(`remarkPlugins`)),
}),
})

hooks.on(`pattern.mdx`, /mdx?$/)
hooks.on(`build.resolve.extensions`, (ext = new Set()) =>
ext.add(`.md`).add(`.mdx`),
)
hooks.on(`build.resolveLoader.alias`, (aliases = {}) => ({
...aliases,
[`@mdx-js/loader`]: loader,
}))
}

build.setLoader(`mdx`, `@mdx-js/loader`).setItem(`mdx`, {
loader: `mdx`,
options: () => ({
rehypePlugins: this.get(`rehypePlugins`)
? Object.values(this.get(`rehypePlugins`))
: [],
remarkPlugins: this.get(`remarkPlugins`)
? Object.values(this.get(`remarkPlugins`))
: [],
}),
/**
* {@link Extension.boot}
*/
@bind
public override async boot({build}: Bud) {
build.setRule(`mdx`, {
include: [({path}) => path(`@src`)],
test: /\.mdx?$/,
use: [...(build.rules.js.use ?? []), `mdx`],
})
}
}
13 changes: 5 additions & 8 deletions sources/@roots/bud-mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -15,12 +15,7 @@ import {default as BudMDX} from '@roots/bud-mdx/extension'

declare module '@roots/bud-framework' {
interface Bud {
mdx: {
get: BudMDX[`get`]
getOptions: BudMDX[`getOptions`]
set: BudMDX[`set`]
setOptions: BudMDX[`setOptions`]
}
mdx: BudMDX
}

interface Loaders {
@@ -35,8 +30,10 @@ declare module '@roots/bud-framework' {
mdx: Rule
}

interface Patterns {
mdx: RegExp
namespace Registry {
interface SyncRegistry {
'pattern.mdx': RegExp
}
}

interface Modules {

0 comments on commit d4a5eb2

Please sign in to comment.