简体   繁体   English

Webpack raw-loader 在需要 markdown 文件时出错

[英]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-loaderawards.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.

相关问题 Webpack - 找不到模块 raw-loader - Webpack - Cannot find module raw-loader 尝试使用raw-loader加载svg时出现包错误 - getting bundle error when trying to load svg with raw-loader 如何从Webpack的Raw-loader中排除html-webpack-plugin模板 - How to exclude html-webpack-plugin templates from raw-loader in webpack 使用 webpacks raw-loader 读取 markdown 文件会导致 index.html 的内容 - Using webpacks raw-loader to read markdown files results in contents of index.html 最新的raw-loader版本在webpack配置中不起作用。 怎么解决? - Latest raw-loader version is not working in webpack config. How to fix it? 如何在 vue 中使用 raw-loader,在 scss 中使用 file-loader - How can I use raw-loader in vue, and file-loader in scss 如何借助 raw-loader 在 vue 组件中导入和使用 bpmn 文件 - How to import and use bpmn file in vue component with aid of raw-loader 如何在从raw-loader导入文本并使用showdown格式化时保留VueJS中的换行符 - How to preserve newlines in VueJS when importing text from raw-loader and formatted using showdown Webpack - 使用文件加载器需要图像 - Webpack - Require Images Using File-Loader 如何在 Angular 7 中配置 raw-loader 来加载文本文件? - How to configure raw-loader in Angular 7 to load text files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM