Skip to content

Commit

Permalink
node-loader: add support for options w/ initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 16, 2024
1 parent d1c8492 commit f1ccf4f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/node-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const defaultLoader = createLoader()

export {createLoader} from './lib/index.js'

/**
* Pass options to the loader.
*/
export const initialize = defaultLoader.initialize

/**
* Load `file:` URLs to MD(X) files.
*/
Expand Down
42 changes: 32 additions & 10 deletions packages/node-loader/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @import {LoadFnOutput, LoadHook, LoadHookContext} from 'node:module'
* @import {Process} from '@mdx-js/mdx/internal-create-format-aware-processors'
* @import {CompileOptions} from '@mdx-js/mdx'
*/

Expand All @@ -14,6 +15,8 @@
* exception that the `development` option is supported based on
* whether you run Node with `--conditions development`.
* You cannot pass it manually.
*
* @typedef {[regex: RegExp, process: Process]} Settings
*/

import fs from 'node:fs/promises'
Expand All @@ -27,21 +30,24 @@ import {development as defaultDevelopment} from '#condition'
/**
* Create Node.js hooks to handle markdown and MDX.
*
* @param {Readonly<Options> | null | undefined} [options]
* @param {Readonly<Options> | null | undefined} [loaderOptions]
* Configuration (optional).
* @returns
* Node.js hooks.
*/
export function createLoader(options) {
const options_ = options || {}
const {extnames, process} = createFormatAwareProcessors({
development: defaultDevelopment,
...options_,
SourceMapGenerator
})
const regex = extnamesToRegex(extnames)
export function createLoader(loaderOptions) {
/** @type {Settings} */
let settings = configure(loaderOptions || {})

return {load}
return {initialize, load}

/**
*
* @param {Readonly<Options> | null | undefined} options
*/
async function initialize(options) {
settings = configure({...loaderOptions, ...options})
}

/**
* Load `file:` URLs to MD(X) files.
Expand All @@ -58,6 +64,7 @@ export function createLoader(options) {
*/
async function load(href, context, nextLoad) {
const url = new URL(href)
const [regex, process] = settings

if (url.protocol === 'file:' && regex.test(url.pathname)) {
const value = await fs.readFile(url)
Expand All @@ -82,3 +89,18 @@ export function createLoader(options) {
return nextLoad(href, context)
}
}

/**
* @param {Readonly<Options>} options
* @returns {Settings}
*/
function configure(options) {
const {extnames, process} = createFormatAwareProcessors({
development: defaultDevelopment,
...options,
SourceMapGenerator
})
const regex = extnamesToRegex(extnames)

return [regex, process]
}
11 changes: 8 additions & 3 deletions website/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {createLoader as createMdxLoader} from '@mdx-js/node-loader'
import {createLoader as createJsxLoader} from '../script/jsx-loader.js'
import config from './mdx-config.js'
import {initialize, load} from './mdx-loader.js'

const loader = {loaders: [createJsxLoader(), createMdxLoader(config)]}
const loader = {loaders: [createJsxLoader(), {initialize, load}]}
export default loader

// To do: after dropping Node 18:
// import {register} from 'node:module'

// register('../script/jsx-loader.js', import.meta.url)
// register('./mdx-loader.js', import.meta.url)
5 changes: 4 additions & 1 deletion website/mdx-config.js → website/mdx-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import assert from 'node:assert/strict'
import {fileURLToPath, pathToFileURL} from 'node:url'
import {createLoader} from '@mdx-js/node-loader'
import {nodeTypes} from '@mdx-js/mdx'
import {common} from '@wooorm/starry-night'
import sourceMdx from '@wooorm/starry-night/source.mdx'
Expand Down Expand Up @@ -117,7 +118,9 @@ const options = {
]
}

export default options
const {initialize, load} = createLoader(options)

export {initialize, load}

function link() {
return s(
Expand Down

0 comments on commit f1ccf4f

Please sign in to comment.