11/**
22 * @import {LoadFnOutput, LoadHook, LoadHookContext} from 'node:module'
3+ * @import {Process} from '@mdx-js/mdx/internal-create-format-aware-processors'
34 * @import {CompileOptions} from '@mdx-js/mdx'
45 */
56
1415 * exception that the `development` option is supported based on
1516 * whether you run Node with `--conditions development`.
1617 * You cannot pass it manually.
18+ *
19+ * @typedef {[regex: RegExp, process: Process] } Settings
1720 */
1821
1922import fs from 'node:fs/promises'
@@ -27,21 +30,24 @@ import {development as defaultDevelopment} from '#condition'
2730/**
2831 * Create Node.js hooks to handle markdown and MDX.
2932 *
30- * @param {Readonly<Options> | null | undefined } [options ]
33+ * @param {Readonly<Options> | null | undefined } [loaderOptions ]
3134 * Configuration (optional).
3235 * @returns
3336 * Node.js hooks.
3437 */
35- export function createLoader ( options ) {
36- const options_ = options || { }
37- const { extnames, process} = createFormatAwareProcessors ( {
38- development : defaultDevelopment ,
39- ...options_ ,
40- SourceMapGenerator
41- } )
42- const regex = extnamesToRegex ( extnames )
38+ export function createLoader ( loaderOptions ) {
39+ /** @type {Settings } */
40+ let settings = configure ( loaderOptions || { } )
4341
44- return { load}
42+ return { initialize, load}
43+
44+ /**
45+ *
46+ * @param {Readonly<Options> | null | undefined } options
47+ */
48+ async function initialize ( options ) {
49+ settings = configure ( { ...loaderOptions , ...options } )
50+ }
4551
4652 /**
4753 * Load `file:` URLs to MD(X) files.
@@ -58,6 +64,7 @@ export function createLoader(options) {
5864 */
5965 async function load ( href , context , nextLoad ) {
6066 const url = new URL ( href )
67+ const [ regex , process ] = settings
6168
6269 if ( url . protocol === 'file:' && regex . test ( url . pathname ) ) {
6370 const value = await fs . readFile ( url )
@@ -82,3 +89,18 @@ export function createLoader(options) {
8289 return nextLoad ( href , context )
8390 }
8491}
92+
93+ /**
94+ * @param {Readonly<Options> } options
95+ * @returns {Settings }
96+ */
97+ function configure ( options ) {
98+ const { extnames, process} = createFormatAwareProcessors ( {
99+ development : defaultDevelopment ,
100+ ...options ,
101+ SourceMapGenerator
102+ } )
103+ const regex = extnamesToRegex ( extnames )
104+
105+ return [ regex , process ]
106+ }
0 commit comments