-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
evaluate.js
382 lines (323 loc) · 10.5 KB
/
evaluate.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* @typedef {import('../lib/util/resolve-evaluate-options.js').Fragment} Fragment
* @typedef {import('../lib/util/resolve-evaluate-options.js').Jsx} Jsx
* @typedef {import('../lib/util/resolve-evaluate-options.js').JsxDev} JsxDev
*/
import assert from 'node:assert/strict'
import {test} from 'node:test'
import {evaluate, evaluateSync, compile} from '@mdx-js/mdx'
import * as provider from '@mdx-js/react'
import {renderToStaticMarkup} from 'react-dom/server'
import * as runtime_ from 'react/jsx-runtime'
import * as devRuntime_ from 'react/jsx-dev-runtime'
import React from 'react'
/** @type {{Fragment: Fragment, jsx: Jsx, jsxs: Jsx}} */
// @ts-expect-error: the automatic react runtime is untyped.
const runtime = runtime_
/** @type {{Fragment: Fragment, jsxDEV: JsxDev}} */
// @ts-expect-error: the automatic dev react runtime is untyped.
const devRuntime = devRuntime_
test('@mdx-js/mdx: evaluate', async function (t) {
await t.test('should throw on missing `Fragment`', async function () {
try {
// @ts-expect-error: check how the runtime handles missing options.
await evaluate('a')
assert.fail()
} catch (error) {
assert.match(String(error), /Expected `Fragment` given to `evaluate`/)
}
})
await t.test('should throw on missing `jsx`', async function () {
try {
await evaluate('a', {Fragment: runtime.Fragment})
assert.fail()
} catch (error) {
assert.match(String(error), /Expected `jsx` given to `evaluate`/)
}
})
await t.test('should throw on missing `jsxs`', async function () {
try {
await evaluate('a', {Fragment: runtime.Fragment, jsx: runtime.jsx})
assert.fail()
} catch (error) {
assert.match(String(error), /Expected `jsxs` given to `evaluate`/)
}
})
await t.test(
'should throw on missing `jsxDEV` in dev mode',
async function () {
try {
await evaluate('a', {Fragment: runtime.Fragment, development: true})
assert.fail()
} catch (error) {
assert.match(String(error), /Expected `jsxDEV` given to `evaluate`/)
}
}
)
await t.test('should evaluate', async function () {
const mod = await evaluate('# hi!', runtime)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<h1>hi!</h1>'
)
})
await t.test('should evaluate (sync)', async function () {
const mod = evaluateSync('# hi!', runtime)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<h1>hi!</h1>'
)
})
await t.test('should evaluate (dev)', async function () {
const mod = await evaluate('# hi dev!', {development: true, ...devRuntime})
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<h1>hi dev!</h1>'
)
})
await t.test('should evaluate (async, dev)', async function () {
const mod = await evaluate('# hi dev!', {development: true, ...devRuntime})
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<h1>hi dev!</h1>'
)
})
await t.test(
'should support an `import` of a relative url w/ `baseUrl`',
async function () {
const mod = await evaluate(
'import {number} from "./context/data.js"\n\n{number}',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'3.14'
)
}
)
await t.test(
'should support an `import` of a full url w/ `baseUrl`',
async function () {
const mod = await evaluate(
'import {number} from "' +
new URL('context/data.js', import.meta.url) +
'"\n\n{number}',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'3.14'
)
}
)
await t.test(
'should support an `import` w/o specifiers w/o `baseUrl`',
async function () {
assert.match(
String(await compile('import "a"', {outputFormat: 'function-body'})),
/\nawait import\("a"\);?\n/
)
}
)
await t.test(
'should support an `import` w/ 0 specifiers w/o `baseUrl`',
async function () {
assert.match(
String(
await compile('import {} from "a"', {outputFormat: 'function-body'})
),
/\nawait import\("a"\);?\n/
)
}
)
await t.test(
'should support a namespace import w/ `baseUrl`',
async function () {
const mod = await evaluate(
'import * as x from "./context/components.js"\n\n<x.Pill>Hi!</x.Pill>',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<span style="color:red">Hi!</span>'
)
}
)
await t.test(
'should support a namespace import and a bare specifier w/ `baseUrl`',
async function () {
const mod = await evaluate(
'import Div, * as x from "./context/components.js"\n\n<x.Pill>a</x.Pill> and <Div>b</Div>',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<p><span style="color:red">a</span> and <div style="color:red">b</div></p>'
)
}
)
await t.test('should support an `export`', async function () {
const mod = await evaluate('export const a = 1\n\n{a}', runtime)
assert.equal(renderToStaticMarkup(React.createElement(mod.default)), '1')
assert.equal(mod.a, 1)
})
await t.test('should support an `export function`', async function () {
const mod = await evaluate(
'export function a() { return 1 }\n\n{a()}',
runtime
)
assert.equal(renderToStaticMarkup(React.createElement(mod.default)), '1')
assert.ok(typeof mod.a === 'function')
assert.equal(mod.a(), 1)
})
await t.test('should support an `export class`', async function () {
const mod = await evaluate(
'export class A { constructor() { this.b = 1 } }\n\n{new A().b}',
runtime
)
assert.equal(renderToStaticMarkup(React.createElement(mod.default)), '1')
const A = /** @type {new () => {b: number}} */ (mod.A)
const a = new A()
assert.equal(a.b, 1)
})
await t.test('should support an `export as`', async function () {
const mod = await evaluate(
'export const a = 1\nexport {a as b}\n\n{a}',
runtime
)
assert.equal(renderToStaticMarkup(React.createElement(mod.default)), '1')
assert.equal(mod.a, 1, 'should support an `export as` (2)')
assert.equal(mod.b, 1, 'should support an `export as` (3)')
})
await t.test('should support an `export default`', async function () {
const mod = await evaluate(
'export default function Layout({components, ...props}) { return <section {...props} /> }\n\na',
runtime
)
assert.equal(
renderToStaticMarkup(React.createElement(mod.default)),
'<section><p>a</p></section>'
)
})
await t.test(
'should support an `export from` w/ `baseUrl`',
async function () {
const mod = await evaluate('export {number} from "./context/data.js"', {
baseUrl: import.meta.url,
...runtime
})
assert.equal(mod.number, 3.14)
}
)
await t.test('should support an `export` w/ `baseUrl`', async function () {
const mod = await evaluate(
'import {number} from "./context/data.js"\nexport {number}',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(mod.number, 3.14)
})
await t.test(
'should support an `export as from` w/ `baseUrl`',
async function () {
const mod = await evaluate(
'export {number as data} from "./context/data.js"',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(mod.data, 3.14)
}
)
await t.test(
'should support an `export default as from` w/ `baseUrl`',
async function () {
const mod = await evaluate(
'export {default as data} from "./context/data.js"',
{baseUrl: import.meta.url, ...runtime}
)
assert.equal(mod.data, 6.28)
}
)
await t.test(
'should support an `export all from` w/ `baseUrl`',
async function () {
const mod = await evaluate('export * from "./context/data.js"', {
baseUrl: import.meta.url,
...runtime
})
assert.deepEqual(
{...mod, default: undefined},
{array: [1, 2], default: undefined, number: 3.14, object: {a: 1, b: 2}}
)
}
)
await t.test(
'should support an `export * from`, but prefer explicit exports, w/ `baseUrl`',
async function () {
const mod = await evaluate(
'export {default as number} from "./context/data.js"\nexport * from "./context/data.js"',
{baseUrl: import.meta.url, ...runtime}
)
// I’m not sure if this makes sense, but it is how Node works.
assert.deepEqual(
{...mod, default: undefined},
{array: [1, 2], default: undefined, number: 6.28, object: {a: 1, b: 2}}
)
}
)
await t.test(
'should support rewriting `import.meta.url` w/ `baseUrl`',
async function () {
const mod = await evaluate(
'export const x = new URL("example.png", import.meta.url).href',
{baseUrl: 'https://example.com', ...runtime}
)
assert.equal(mod.x, 'https://example.com/example.png')
}
)
await t.test(
'should support rewriting `import.meta.url` w/ `baseUrl` as an URL',
async function () {
const mod = await evaluate(
'export const x = new URL("example.png", import.meta.url).href',
{baseUrl: new URL('https://example.com'), ...runtime}
)
assert.equal(mod.x, 'https://example.com/example.png')
}
)
await t.test('should support a given components', async function () {
const mod = await evaluate('<X/>', runtime)
assert.equal(
renderToStaticMarkup(
React.createElement(mod.default, {
components: {
X() {
return React.createElement('span', {}, '!')
}
}
})
),
'<span>!</span>'
)
})
await t.test(
'should support a provider w/ `useMDXComponents`',
async function () {
const mod = await evaluate('<X/>', {...runtime, ...provider})
assert.equal(
renderToStaticMarkup(
React.createElement(
provider.MDXProvider,
{
components: {
X() {
return React.createElement('span', {}, '!')
}
}
},
React.createElement(mod.default)
)
),
'<span>!</span>'
)
}
)
})