/
resolve-evaluate-options.js
86 lines (81 loc) · 3 KB
/
resolve-evaluate-options.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
/**
* @typedef {import('mdx/types.js').MDXComponents} Components
* @typedef {import('../compile.js').CompileOptions} CompileOptions
*/
/**
* @typedef {EvaluateProcessorOptions & RunOptions} EvaluateOptions
* Configuration for `evaluate`.
*
* @typedef {Omit<CompileOptions, 'baseUrl' | 'jsx' | 'jsxImportSource' | 'jsxRuntime' | 'outputFormat' | 'pragma' | 'pragmaFrag' | 'pragmaImportSource' | 'providerImportSource'> } EvaluateProcessorOptions
* Compile configuration without JSX options for evaluation.
*
* @typedef RunOptions
* Configuration to run compiled code.
*
* `Fragment`, `jsx`, and `jsxs` are used when the code is compiled in
* production mode (`development: false`).
* `Fragment` and `jsxDEV` are used when compiled in development mode
* (`development: true`).
* `useMDXComponents` is used when the code is compiled with
* `providerImportSource: '#'` (the exact value of this compile option
* doesn’t matter).
* @property {URL | string | null | undefined} [baseUrl]
* Use this URL as `import.meta.url` and resolve `import` and `export … from`
* relative to it (optional, example: `import.meta.url`);
* this option can also be given at compile time in `CompileOptions`;
* you should pass this (likely at runtime), as you might get runtime errors
* when using `import.meta.url` / `import` / `export … from ` otherwise.
* @property {unknown} Fragment
* Symbol to use for fragments (**required**).
* @property {unknown} [jsx]
* Function to generate an element with static children in production mode.
* @property {unknown} [jsxDEV]
* Function to generate an element in development mode.
* @property {unknown} [jsxs]
* Function to generate an element with dynamic children in production mode.
* @property {UseMdxComponents | null | undefined} [useMDXComponents]
* Function to get components from context.
*
* @callback UseMdxComponents
* Get components from context.
* @returns {Components}
* Current components.
*/
// Fix to show references to above types in VS Code.
''
/**
* Split compiletime options from runtime options.
*
* @param {Readonly<EvaluateOptions> | null | undefined} options
* Configuration.
* @returns {{compiletime: CompileOptions, runtime: RunOptions}}
* Split options.
*/
export function resolveEvaluateOptions(options) {
const {
Fragment,
baseUrl,
development,
jsx,
jsxDEV,
jsxs,
useMDXComponents,
...rest
} = options || {}
if (!Fragment) throw new Error('Expected `Fragment` given to `evaluate`')
if (development) {
if (!jsxDEV) throw new Error('Expected `jsxDEV` given to `evaluate`')
} else {
if (!jsx) throw new Error('Expected `jsx` given to `evaluate`')
if (!jsxs) throw new Error('Expected `jsxs` given to `evaluate`')
}
return {
compiletime: {
...rest,
development,
outputFormat: 'function-body',
providerImportSource: useMDXComponents ? '#' : undefined
},
runtime: {Fragment, baseUrl, jsx, jsxDEV, jsxs, useMDXComponents}
}
}