forked from storybookjs/mdx2-csf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
33 lines (28 loc) · 817 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
const { getOptions } = require('loader-utils');
const { compile } = require('./compiler');
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();
// this.getOptions() is webpack5 only
const queryOptions = this.getOptions ? this.getOptions() : getOptions(this);
const options = Object.assign({}, queryOptions, {
filepath: this.resourcePath,
});
let result;
try {
result = await compile(content, options);
} catch (err) {
return callback(err);
}
const code = `${DEFAULT_RENDERER}\n${result}`;
return callback(null, code);
};
module.exports = loader;