-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (81 loc) · 2.41 KB
/
index.js
File metadata and controls
88 lines (81 loc) · 2.41 KB
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
/**
* @import {FormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
* @import {CompileOptions} from '@mdx-js/mdx'
* @import * as vite from 'vite'
*/
/**
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} ApplicableOptions
* Applicable compile configuration.
*
* @typedef {string | RegExp | Array<string | RegExp>} FilterPattern
* A Rollup filter pattern.
*
* @typedef ExtraOptions
* Extra configuration.
* @property {FilterPattern | null | undefined} [exclude]
* Picomatch patterns to exclude (optional).
* @property {FilterPattern | null | undefined} [include]
* Picomatch patterns to include (optional).
*
* @typedef {ApplicableOptions & ExtraOptions} Options
* Configuration.
*
* @typedef Plugin
* Plugin that is compatible with both Rollup and Vite.
* @property {string} name
* The name of the plugin
*/
import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {SourceMapGenerator} from 'source-map'
import {VFile} from 'vfile'
/**
* Plugin to 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 || {}
/** @type {FormatAwareProcessors} */
let formatAwareProcessors
/** @type {vite.Plugin<never>} */
const plugin = {
name: '@mdx-js/rollup',
config(config, env) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
development: env.mode === 'development',
...rest
})
},
transform: {
filter: {
id: {
include: /** @type {FilterPattern} */ (include),
exclude: /** @type {FilterPattern} */ (exclude)
}
},
async handler(value, id) {
if (!formatAwareProcessors) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
...rest
})
}
const [path] = id.split('?')
const file = new VFile({path, value})
if (
file.extname &&
formatAwareProcessors.extnames.includes(file.extname)
) {
const compiled = await formatAwareProcessors.process(file)
const code = String(compiled.value)
return {code, map: compiled.map}
}
}
}
}
return plugin
}