-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.js
111 lines (105 loc) · 3.29 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
/**
* @typedef {import('@mdx-js/mdx/internal-create-format-aware-processors').FormatAwareProcessors} FormatAwareProcessors
* @typedef {import('@mdx-js/mdx').CompileOptions} CompileOptions
* @typedef {import('@rollup/pluginutils').FilterPattern} FilterPattern
* @typedef {import('rollup').SourceDescription} SourceDescription
*/
/**
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} ApplicableOptions
* Applicable compile configuration.
*
* @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
* @property {ViteConfig} config
* Function used by Vite to set additional configuration options.
* @property {Transform} transform
* Function to transform the source content.
*
* @callback Transform
* Callback called by Rollup and Vite to transform.
* @param {string} value
* File contents.
* @param {string} path
* File path.
* @returns {Promise<SourceDescription | undefined>}
* Result.
*
* @callback ViteConfig
* Callback called by Vite to set additional configuration options.
* @param {unknown} config
* Configuration object (unused).
* @param {ViteEnv} env
* Environment variables.
* @returns {undefined}
* Nothing.
*
* @typedef ViteEnv
* Environment variables used by Vite.
* @property {string} mode
* Mode.
*/
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'
/**
* 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
const filter = createFilter(include, exclude)
return {
name: '@mdx-js/rollup',
config(config, env) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
development: env.mode === 'development',
...rest
})
},
async transform(value, path) {
if (!formatAwareProcessors) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
...rest
})
}
const file = new VFile({path, value})
if (
file.extname &&
filter(file.path) &&
formatAwareProcessors.extnames.includes(file.extname)
) {
const compiled = await formatAwareProcessors.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
}
}
}
}