diff --git a/packages/node-loader/index.js b/packages/node-loader/index.js index a13d7072a..6898f36bd 100644 --- a/packages/node-loader/index.js +++ b/packages/node-loader/index.js @@ -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. */ diff --git a/packages/node-loader/lib/index.js b/packages/node-loader/lib/index.js index e1e70cf91..71b8c7cc1 100644 --- a/packages/node-loader/lib/index.js +++ b/packages/node-loader/lib/index.js @@ -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' */ @@ -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' @@ -27,21 +30,24 @@ import {development as defaultDevelopment} from '#condition' /** * Create Node.js hooks to handle markdown and MDX. * - * @param {Readonly | null | undefined} [options] + * @param {Readonly | 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 | null | undefined} options + */ + async function initialize(options) { + settings = configure({...loaderOptions, ...options}) + } /** * Load `file:` URLs to MD(X) files. @@ -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) @@ -82,3 +89,18 @@ export function createLoader(options) { return nextLoad(href, context) } } + +/** + * @param {Readonly} options + * @returns {Settings} + */ +function configure(options) { + const {extnames, process} = createFormatAwareProcessors({ + development: defaultDevelopment, + ...options, + SourceMapGenerator + }) + const regex = extnamesToRegex(extnames) + + return [regex, process] +} diff --git a/packages/node-loader/test/index.js b/packages/node-loader/test/index.js index 3e2c559ae..de2403be6 100644 --- a/packages/node-loader/test/index.js +++ b/packages/node-loader/test/index.js @@ -12,6 +12,7 @@ test('@mdx-js/node-loader', async function (t) { await t.test('should expose the public api', async function () { assert.deepEqual(Object.keys(await import('@mdx-js/node-loader')).sort(), [ 'createLoader', + 'initialize', 'load' ]) }) diff --git a/website/loader.js b/website/loader.js index 85c49fa11..d26742aad 100644 --- a/website/loader.js +++ b/website/loader.js @@ -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) diff --git a/website/mdx-config.js b/website/mdx-loader.js similarity index 99% rename from website/mdx-config.js rename to website/mdx-loader.js index b045b5c80..fd96f813d 100644 --- a/website/mdx-config.js +++ b/website/mdx-loader.js @@ -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' @@ -117,7 +118,9 @@ const options = { ] } -export default options +const {initialize, load} = createLoader(options) + +export {initialize, load} function link() { return s(