如何使用WebPack 5和Docusaurus读取Markdown File的原始内容
我目前正在重构一个Docusaurus应用程序,该应用程序显示了Markdown Docs的开发人员文档。该应用具有“快速启动”功能,该功能具有向导,用户填写信息,然后填充Markdown模板。
该应用程序用于使用webpack 4和raw-loader
读取文档模板的内容,然后与用户传递的内容进行查找和替换。自从我切换到WebPack 5,raw--装载机不再起作用。我在WebPack 5中读到,由于该功能已内置在WebPack中,因此不再需要使用RAW-LOADER
。 https://webpack.js.s.js.org/guides/guides/guides/asset-modules/asset-modules/asset-modules/# source-assets
我创建了webpack.config.js
文件:
module.exports = {
module: {
rules: [
{
resourceQuery: /raw/,
type: 'asset/source'
}
]
}
};
我有一个markdown文件/docs/template.md
:
# My template
## Some section 1
some text
## Some section 2
some text
在我的js中
文件我试图导入类似的内容:
import template from '@site/docs/template.md?raw';
console.log(template);
但是,webpack没有将markdown文件的内容作为字符串加载,我会得到一些MDX内容:
ƒ MDXContent(_ref){let{components,...props}=_ref;return
(0,_mdx_js_react__WEBPACK_IMPORTED_MODULE_2__.mdx)(MDXLayout
(0,_Users_kevin_apps_docs_node_modules_babel_runtime_helpers_esm_ext…
对我做错了什么有任何想法吗?
I am currently refactoring a Docusaurus app that displays developer documentation from markdown docs. The app has a "quick start" feature which has a wizard that users fill out info and then it populates a markdown template.
The app used to use webpack 4 and raw-loader
to read the contents of an documentation template and then do a find and replace with what the user passed in. Ever since I switched to webpack 5, raw-loader no longer works. I read in webpack 5 that the use of raw-loader
was no longer necessary as that functionality is built into webpack; https://webpack.js.org/guides/asset-modules/#source-assets
I created a webpack.config.js
file:
module.exports = {
module: {
rules: [
{
resourceQuery: /raw/,
type: 'asset/source'
}
]
}
};
I have a markdown file /docs/template.md
:
# My template
## Some section 1
some text
## Some section 2
some text
And in my js
file I attempt to import the contents like so:
import template from '@site/docs/template.md?raw';
console.log(template);
However webpack is not loading the contents of markdown file as a string, I get some mdx content:
ƒ MDXContent(_ref){let{components,...props}=_ref;return
(0,_mdx_js_react__WEBPACK_IMPORTED_MODULE_2__.mdx)(MDXLayout
(0,_Users_kevin_apps_docs_node_modules_babel_runtime_helpers_esm_ext…
Any idea on what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为Docusaurus网站尊重
webpack.config.js
文件,所以我用docusaurus.config.js
的Inline插件模块解决了此问题:I don't think Docusaurus sites respect
webpack.config.js
files, so I resolved this issue with an inline plugin module indocusaurus.config.js
: