[英]Webpack raw-loader errors out when require markdown file
raw-loader
errors out when attempting to require any .md
file.尝试要求任何
.md
文件时出现raw-loader
错误。
Adding raw loader to import markdown files:添加原始加载程序以导入 markdown 文件:
test: /\.md$/i,
use: [{
loader: 'raw-loader',
options: {
esModule: false
}
}],
In .js
file, require the markdown file..在
.js
文件中,需要 markdown 文件。
return require(postPath)
// postPath is '../posts/awards.md'
Error: Cannot find module '../posts/awards.md'
at webpackEmptyContext (eval at <path to file>)
....
the path to markdown file is the relative path: /posts/awards.md
markdown 文件的路径是相对路径:
/posts/awards.md
If I change awards.md
to awards.json
it works.如果我将
awards.md
更改为awards.json
它可以工作。 So maybe it is an issue with raw-loader
looking for a export
in awards.md
and not find one, thus erroring out?所以也许这是
raw-loader
在awards.md
中寻找export
但没有找到的问题,因此出错了? Isn't the point of esModule: false
to instruct Webpack to NOT treat it as module? esModule: false
的目的不是指示 Webpack 不将其视为模块吗?
Seems like you're having the same problem as this person.看来你和这个人有同样的问题。
Quoting an answer:引用一个答案:
Webpack performs a static analyse at build time.
Webpack 在构建时执行 static 分析。
It doesn't try to infer variables which that import(test) could be anything, hence the failure.
它不会尝试推断 import(test) 可能是任何变量的变量,因此失败。
It is also the case for import(path+"a.js").
import(path+"a.js") 也是如此。
If you need truly dynamic imports, you have to restrict it to a known path:
如果您需要真正的动态导入,则必须将其限制为已知路径:
import("./locales/" + locale + ".js")
I re-created your problem and sure enough:我重新创建了您的问题,果然:
function test(mod) {
return require(mod)
}
console.log(test("./test.md"))
Doesn't work.不工作。 HOWEVER this works:
然而这有效:
function test(mod) {
return require("./" + mod)
}
console.log(test("test.md"))
So it should suffice to change your code to this:因此,将您的代码更改为以下内容就足够了:
return require("../" + postPath + ".md")
And change postPath
to:并将
postPath
更改为:
// postPath is 'posts/awards'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.