-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.cjs
136 lines (122 loc) · 3.69 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @typedef {import('node:module').Module} Module
* @typedef {import('mdx/types.js').MDXModule} MDXModule
*/
/**
* @callback LoadCallback
* Callback.
* @param {Error | undefined} error
* Error (optional).
* @param {unknown} [result]
* Module (optional).
* @returns {void}
* Nothing.
*
* Note: `void` needed, `deasync` types don’t accept `undefined`.
*/
// @ts-expect-error: type imports from CJS do work.
/** @typedef {import('@mdx-js/mdx').EvaluateOptions} EvaluateOptions */
// @ts-expect-error: type imports from CJS do work.
/** @typedef {import('@mdx-js/mdx')} MdxMod */
// @ts-expect-error: type imports from CJS do work.
/** @typedef {import('@mdx-js/mdx/internal-create-format-aware-processors')} CreateProcessorMod */
// @ts-expect-error: type imports from CJS do work.
/** @typedef {import('@mdx-js/mdx/internal-resolve-evaluate-options')} ResolveEvaluateMod */
'use strict'
const fs = require('node:fs')
const deasync = require('deasync')
const {createFormatAwareProcessors} = /** @type {CreateProcessorMod} */ (
deasync(load)('@mdx-js/mdx/internal-create-format-aware-processors')
)
const {resolveEvaluateOptions} = /** @type {ResolveEvaluateMod} */ (
deasync(load)('@mdx-js/mdx/internal-resolve-evaluate-options')
)
const {runSync} = /** @type {MdxMod} */ (deasync(load)('@mdx-js/mdx'))
module.exports = register
/**
* @param {Readonly<EvaluateOptions>} options
* Configuration (optional)
* @returns {undefined}
* Nothing.
*/
function register(options) {
const {compiletime, runtime} = resolveEvaluateOptions(options)
const {extnames, processSync} = createFormatAwareProcessors(compiletime)
let index = -1
while (++index < extnames.length) {
// eslint-disable-next-line n/no-deprecated-api
require.extensions[extnames[index]] = mdx
}
/**
* @param {Module} module
* Module.
* @param {string} path
* Path.
* @returns {undefined}
* Nothing.
*/
function mdx(module, path) {
const file = processSync(fs.readFileSync(path))
/** @type {MDXModule} */
const result = runSync(file, runtime)
// Something going weird here w/ `type-coverage`.
// type-coverage:ignore-next-line
module.exports = result.default
module.loaded = true
}
}
/**
*
* @param {string} filePath
* File path.
* @param {LoadCallback} callback
* Callback.
* @returns {undefined}
* Nothing.
*/
function load(filePath, callback) {
/** @type {boolean} */
let called
// Sometimes, the import hangs (see error message for reasons).
// To fix that, a timeout can be used.
const id = setTimeout(timeout, 1024)
/* c8 ignore start -- something is going wrong in Node/V8 if this happens. */
function timeout() {
done(
new Error(
'Could not import:\n' +
"this error can occur when doing `require('@mdx-js/register')` in an async function: please move the require to the top or remove it and use `node -r @mdx-js/register …` instead\n" +
'this error can also occur if both `@mdx-js/register` and `@mdx-js/esm-loader` are used: please use one or the other'
)
)
}
/* c8 ignore end */
import(filePath).then(
/**
* @param {unknown} module
* Result from calling `import`.
* @returns {undefined}
* Nothing.
*/
function (module) {
done(undefined, module)
},
done
)
/**
* @param {Error | undefined} error
* Error (optional).
* @param {unknown} [result]
* Module (optional).
* @returns {undefined}
* Nothing.
*/
function done(error, result) {
/* Something is going wrong in Node/V8 if this happens. */
/* c8 ignore next */
if (called) return
called = true
clearTimeout(id)
callback(error, result)
}
}