-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdx-loader.js
57 lines (49 loc) · 1.65 KB
/
mdx-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const { createLoader } = require("simple-functional-loader");
const _estimateReadTime = require("reading-time");
const estimateReadTime = createLoader(function (src) {
const readTime = _estimateReadTime(src).text;
const withTime = [src, `export const metadata_readingTime = "${readTime}";`].join("\n");
return this.callback(null, withTime);
});
module.exports =
(pluginOptions = {}) =>
(nextConfig = {}) => {
const extension = pluginOptions.extension || /\.mdx$/;
const loader = nextConfig?.experimental?.mdxRs
? {
loader: require.resolve("@next/mdx/mdx-rs-loader.js"),
options: {
providerImportSource: "next-mdx-import-source-file",
...pluginOptions.options,
},
}
: {
loader: require.resolve("@mdx-js/loader"),
options: {
providerImportSource: "next-mdx-import-source-file",
...pluginOptions.options,
},
};
return Object.assign({}, nextConfig, {
webpack(config, options) {
config.resolve.alias["next-mdx-import-source-file"] = [
"private-next-root-dir/src/mdx-components",
"private-next-root-dir/mdx-components",
"@mdx-js/react",
];
const mdx = [
nextConfig?.experimental?.mdxRs ? undefined : options.defaultLoaders.babel,
loader,
estimateReadTime,
].filter(Boolean);
config.module.rules.push({
test: extension,
use: mdx,
});
if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options);
}
return config;
},
});
};