-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
66 lines (61 loc) · 1.94 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
/**
* @typedef {import('@rollup/pluginutils').FilterPattern} FilterPattern
* @typedef {import('rollup').Plugin<unknown>} Plugin
* @typedef {import('rollup').SourceDescription} SourceDescription
*/
/**
* @typedef {Omit<import('@mdx-js/mdx').CompileOptions, 'SourceMapGenerator'>} CompileOptions
* Default configuration.
*
* @typedef RollupPluginOptions
* Extra configuration.
* @property {FilterPattern | null | undefined} [include]
* List of picomatch patterns to include (optional).
* @property {FilterPattern | null | undefined} [exclude]
* List of picomatch patterns to exclude (optional).
*
* @typedef {CompileOptions & RollupPluginOptions} Options
* Configuration.
*/
import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {createFilter} from '@rollup/pluginutils'
import {SourceMapGenerator} from 'source-map'
import {VFile} from 'vfile'
/**
* Compile MDX w/ rollup.
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @return {Plugin}
* Rollup plugin.
*/
export function rollup(options) {
const {exclude, include, ...rest} = options || {}
const {extnames, process} = createFormatAwareProcessors({
SourceMapGenerator,
...rest
})
const filter = createFilter(include, exclude)
return {
name: '@mdx-js/rollup',
async transform(value, path) {
const file = new VFile({path, value})
if (
file.extname &&
filter(file.path) &&
extnames.includes(file.extname)
) {
const compiled = await process(file)
const code = String(compiled.value)
/** @type {SourceDescription} */
const result = {
code,
// @ts-expect-error: `rollup` is not compiled with `exactOptionalPropertyTypes`,
// so it does not allow `sourceRoot` in `file.map` to be `undefined` here.
map: compiled.map
}
return result
}
}
}
}