/
index.test.js
221 lines (193 loc) · 5.74 KB
/
index.test.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
/**
* @typedef {import('mdx/types.js').MDXContent} MDXContent
* @typedef {import('preact').FunctionComponent<unknown>} PreactComponent
* @typedef {import('vue').Component} VueComponent
*/
import {promises as fs} from 'fs'
import {promisify} from 'util'
import {fileURLToPath} from 'url'
import {test} from 'uvu'
import * as assert from 'uvu/assert'
import webpack from 'webpack'
import React from 'react'
import {renderToStaticMarkup} from 'react-dom/server'
import {h} from 'preact'
import {render} from 'preact-render-to-string'
import * as vue from 'vue'
import serverRenderer from '@vue/server-renderer'
test('@mdx-js/loader', async () => {
// Setup.
const base = new URL('.', import.meta.url)
await fs.writeFile(new URL('webpack.mdx', base), '# Hello, {<Message />')
// Errors.
const failedResult = await promisify(webpack)({
// @ts-expect-error To do: webpack types miss support for `context`.
context: fileURLToPath(base),
entry: './webpack.mdx',
mode: 'none',
module: {
rules: [
{
test: /\.mdx$/,
use: [fileURLToPath(new URL('../index.cjs', import.meta.url))]
}
]
},
output: {
path: fileURLToPath(base),
filename: 'react.cjs',
libraryTarget: 'commonjs'
}
})
const error = failedResult?.toJson()?.errors?.[0]
assert.ok(error)
assert.equal(
error.message,
`Module build failed (from ../index.cjs):
webpack.mdx:1:22: Unexpected end of file in expression, expected a corresponding closing brace for \`{\``,
'received expected error message'
)
await fs.writeFile(
new URL('webpack.mdx', base),
'export const Message = () => <>World!</>\n\n# Hello, <Message />'
)
// React.
const reactBuild = await promisify(webpack)({
// @ts-expect-error To do: webpack types miss support for `context`.
context: fileURLToPath(base),
entry: './webpack.mdx',
mode: 'none',
module: {
rules: [
{
test: /\.mdx$/,
use: [fileURLToPath(new URL('../index.cjs', import.meta.url))]
}
]
},
output: {
path: fileURLToPath(base),
filename: 'react.cjs',
libraryTarget: 'commonjs'
}
})
assert.not.ok(reactBuild?.hasErrors())
// One for ESM loading CJS, one for webpack.
const modReact = /** @type {{default: {default: MDXContent}}} */ (
// @ts-expect-error file is dynamically generated
await import('./react.cjs')
)
assert.equal(
renderToStaticMarkup(React.createElement(modReact.default.default)),
'<h1>Hello, World!</h1>',
'should compile (react)'
)
const reactOutput = await fs.readFile(new URL('react.cjs', base), 'utf8')
assert.not.match(
reactOutput,
/react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_\d+__\.jsxDEV/,
'should infer the development option from webpack’s production mode'
)
await fs.unlink(new URL('react.cjs', base))
// Preact and source maps
const preactBuild = await promisify(webpack)({
// @ts-expect-error To do: webpack types miss support for `context`.
context: fileURLToPath(base),
entry: './webpack.mdx',
mode: 'development',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.mdx$/,
use: [
{
loader: fileURLToPath(new URL('../index.cjs', import.meta.url)),
options: {jsxImportSource: 'preact'}
}
]
}
]
},
output: {
path: fileURLToPath(base),
filename: 'preact.cjs',
libraryTarget: 'commonjs'
}
})
assert.not.ok(preactBuild?.hasErrors())
// One for ESM loading CJS, one for webpack.
const modPreact = /** @type {{default: {default: PreactComponent}}} */ (
// @ts-expect-error file is dynamically generated.
await import('./preact.cjs')
)
assert.equal(
render(h(modPreact.default.default, {})),
'<h1>Hello, World!</h1>',
'should compile (preact)'
)
const preactOutput = await fs.readFile(new URL('preact.cjs', base), 'utf8')
assert.match(
preactOutput,
/preact_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_\d+__\.jsxDEV/,
'should infer the development option from webpack’s development mode'
)
assert.match(
preactOutput,
/\/\/# sourceMappingURL/,
'should add a source map if requested'
)
await fs.unlink(new URL('preact.cjs', base))
// Vue.
const vueBuild = await promisify(webpack)({
// @ts-expect-error To do: webpack types miss support for `context`.
context: fileURLToPath(base),
entry: './webpack.mdx',
mode: 'none',
externals: ['vue'],
module: {
rules: [
{
test: /\.mdx$/,
use: [
{
loader: 'babel-loader',
options: {configFile: false, plugins: ['@vue/babel-plugin-jsx']}
},
{
loader: fileURLToPath(new URL('../index.cjs', import.meta.url)),
options: {jsx: true}
}
]
}
]
},
output: {
path: fileURLToPath(base),
filename: 'vue.cjs',
libraryTarget: 'commonjs'
}
})
assert.not.ok(vueBuild?.hasErrors())
// One for ESM loading CJS, one for webpack.
const modVue = /** @type {{default: {default: VueComponent}}} */ (
// @ts-expect-error file is dynamically generated
await import('./vue.cjs')
)
const vueResult = await serverRenderer.renderToString(
vue.createSSRApp({
components: {Content: modVue.default.default},
template: '<Content />'
})
)
assert.equal(
// Remove SSR comments used to hydrate (I guess).
vueResult.replace(/<!--[[\]]-->/g, ''),
'<h1>Hello, World!</h1>',
'should compile (vue)'
)
await fs.unlink(new URL('vue.cjs', base))
// Clean.
await fs.unlink(new URL('webpack.mdx', base))
})
test.run()