-
Notifications
You must be signed in to change notification settings - Fork 9
/
loader.js
39 lines (34 loc) · 931 Bytes
/
loader.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
const { compile } = require('./dist/index');
// FIXME: we shouldn't be doing this, but we need it
// for react MDX story definitions, e.g.
//
// <Story name="foo"><div>hi></div></Story>
//
// Which generates the code:
//
// export const foo = () => <div>hi</div>;
const DEFAULT_RENDERER = `
import React from 'react';
`;
// Lifted from MDXv1 loader
// https://github.com/mdx-js/mdx/blob/v1/packages/loader/index.js
//
// Added
// - webpack5 support
// - MDX compiler built in
const loader = async function (content) {
const callback = this.async();
const options = Object.assign({}, this.getOptions(), {
filepath: this.resourcePath,
});
let result;
try {
result = await compile(content, options);
} catch (err) {
console.error('Error loading:', this.resourcePath)
return callback(err);
}
const code = `${DEFAULT_RENDERER}\n${result}`;
return callback(null, code);
};
module.exports = loader;