Skip to content

Commit d586720

Browse files
committedSep 16, 2024··
node-loader: add support for options w/ initialize
1 parent d1c8492 commit d586720

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed
 

‎packages/node-loader/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const defaultLoader = createLoader()
88

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

11+
/**
12+
* Pass options to the loader.
13+
*/
14+
export const initialize = defaultLoader.initialize
15+
1116
/**
1217
* Load `file:` URLs to MD(X) files.
1318
*/

‎packages/node-loader/lib/index.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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

@@ -14,6 +15,8 @@
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

1922
import 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+
}

‎packages/node-loader/test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test('@mdx-js/node-loader', async function (t) {
1212
await t.test('should expose the public api', async function () {
1313
assert.deepEqual(Object.keys(await import('@mdx-js/node-loader')).sort(), [
1414
'createLoader',
15+
'initialize',
1516
'load'
1617
])
1718
})

‎website/loader.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import {createLoader as createMdxLoader} from '@mdx-js/node-loader'
21
import {createLoader as createJsxLoader} from '../script/jsx-loader.js'
3-
import config from './mdx-config.js'
2+
import {initialize, load} from './mdx-loader.js'
43

5-
const loader = {loaders: [createJsxLoader(), createMdxLoader(config)]}
4+
const loader = {loaders: [createJsxLoader(), {initialize, load}]}
65
export default loader
6+
7+
// To do: after dropping Node 18:
8+
// import {register} from 'node:module'
9+
10+
// register('../script/jsx-loader.js', import.meta.url)
11+
// register('./mdx-loader.js', import.meta.url)

‎website/mdx-config.js ‎website/mdx-loader.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import assert from 'node:assert/strict'
1818
import {fileURLToPath, pathToFileURL} from 'node:url'
19+
import {createLoader} from '@mdx-js/node-loader'
1920
import {nodeTypes} from '@mdx-js/mdx'
2021
import {common} from '@wooorm/starry-night'
2122
import sourceMdx from '@wooorm/starry-night/source.mdx'
@@ -117,7 +118,9 @@ const options = {
117118
]
118119
}
119120

120-
export default options
121+
const {initialize, load} = createLoader(options)
122+
123+
export {initialize, load}
121124

122125
function link() {
123126
return s(

0 commit comments

Comments
 (0)
Please sign in to comment.