-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.js
136 lines (121 loc) · 3.58 KB
/
index.js
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
/**
* @import {CompileOptions} from '@mdx-js/mdx'
* @import {Compatible, VFile} from 'vfile'
* @import {VFileMessage} from 'vfile-message'
* @import {Compiler as WebpackCompiler, LoaderContext} from 'webpack'
*/
/**
* @typedef {Omit<CompileOptions, 'SourceMapGenerator' | 'development'>} Options
* Configuration (TypeScript type).
*
* Options are the same as `compile` from `@mdx-js/mdx` with the exception
* that the `SourceMapGenerator` and `development` options are supported
* based on how you configure webpack.
* You cannot pass them manually.
*
* @callback Process
* Process.
* @param {Compatible} vfileCompatible
* Input.
* @returns {Promise<VFile>}
* File.
*/
import {Buffer} from 'node:buffer'
import {createHash} from 'node:crypto'
import path from 'node:path'
import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {SourceMapGenerator} from 'source-map'
// Note: the cache is heavily inspired by:
// <https://github.com/TypeStrong/ts-loader/blob/5c030bf/src/instance-cache.ts>
const marker = /** @type {WebpackCompiler} */ ({})
/** @type {WeakMap<WebpackCompiler, Map<string, Process>>} */
const cache = new WeakMap()
/**
* A Webpack (5+) loader for MDX.
* See `webpack.cjs`, which wraps this, because Webpack loaders must currently
* be CommonJS.
*
* @this {LoaderContext<unknown>}
* Context.
* @param {string} value
* Value.
* @param {LoaderContext<unknown>['callback']} callback
* Callback.
* @returns {undefined}
* Nothing.
*/
export function loader(value, callback) {
const options = /** @type {CompileOptions} */ (this.getOptions())
const hash = getOptionsHash(options)
const config = {
SourceMapGenerator: this.sourceMap ? SourceMapGenerator : undefined,
development: this.mode === 'development',
...options
}
/* c8 ignore next -- some loaders set `undefined` (see `TypeStrong/ts-loader`). */
const compiler = this._compiler || marker
// To do: next major: remove.
if ('renderer' in config) {
callback(
new Error(
'`options.renderer` is no longer supported. Please see <https://mdxjs.com/migrating/v2/> for more information'
)
)
return
}
let map = cache.get(compiler)
if (!map) {
map = new Map()
cache.set(compiler, map)
}
let process = map.get(hash)
if (!process) {
process = createFormatAwareProcessors(config).process
map.set(hash, process)
}
const context = this.context
const filePath = this.resourcePath
process({value, path: filePath}).then(
function (file) {
callback(
undefined,
Buffer.from(file.value),
// @ts-expect-error: `webpack` is not compiled with `exactOptionalPropertyTypes`,
// so it does not allow `sourceRoot` in `file.map` to be `undefined` here.
file.map || undefined
)
},
/**
* @param {VFileMessage} error
* Error.
* @returns {undefined}
* Nothing.
*/
function (error) {
const fpath = path.relative(context, filePath)
error.message = `${fpath}:${error.name}: ${error.message}`
callback(error)
}
)
}
/**
* @param {Readonly<Options>} options
* Configuration.
* @returns {string}
* Hash.
*/
function getOptionsHash(options) {
const hash = createHash('sha256')
/** @type {keyof Options} */
let key
for (key in options) {
if (Object.hasOwn(options, key)) {
const value = options[key]
if (value !== undefined) {
const valueString = JSON.stringify(value)
hash.update(key + valueString)
}
}
}
return hash.digest('hex').slice(0, 16)
}