javascript Webpack 无法解析 html-loader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31958317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:38:45  来源:igfitidea点击:

Webpack can't resolve html-loader

javascriptwebpack

提问by jbarreiros

I am converting a project using requirejs to webpack and having trouble with the "html-loader" loader.

我正在将一个使用 requirejs 的项目转换为 webpack,但在使用“html-loader”加载器时遇到了问题。

package.json:

包.json:

"html-loader": "^0.3.0",
"webpack": "^1.11.0",
"webpack-dev-server": "^1.10.1"

app/js/webpack.config.js:

app/js/webpack.config.js:

  // folder structure:
  // root
  //   app/js
  //   bower_components/
  //   dist/
  //   node_modules/

  entry: './app/js/main.js',
  output: {
    path: 'dist/js/',
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      // Note, via requirejs's "text" plugin, I included templates
      // like this: var tpl = require('text!sample.html');
      // For webpack, I went through the codebase and cleaned up
      // every instance of "text!".
      {test: /\.html$/, loader: 'html'}
    ]
  },
  resolve: {
    root: ['app/js', 'bower_components'],
    alias: {
      ...
    }
  },
  plugins: [
    new webpack.ResolverPlugin(
      new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin(
        'bower.json', ['main']
      )
    )
  ]

When I run webpack -- webpack --config app/js/webpack.config.js-- I get the following error message:

当我运行 webpack -- webpack --config app/js/webpack.config.js-- 我收到以下错误消息:

ERROR in app/js/some/file.js
Module not found: Error: Cannot resolve module 'html' in app/js/some/file.js

app/js/some/file.js 中的
错误模块未找到:错误:无法解析 app/js/some/file.js 中的模块“html”

I tried the following, which didn't work:

我尝试了以下方法,但没有用:

  resolveLoader: {
    root: [path.join(__dirname, 'node_modules')],
  },

Also tried moving "webpack.config.js" to the project root. That didn't help either.

还尝试将“webpack.config.js”移动到项目根目录。那也没有帮助。

And, even tried using the "raw-loader" loader, which also resulted in the same "Cannot resolve module 'raw'" error.

而且,甚至尝试使用“raw-loader”加载器,这也导致了同样的“无法解析模块‘原始’”错误。

Any help would be appreciated. Thanks.

任何帮助,将不胜感激。谢谢。

采纳答案by jbarreiros

Totally dropped the ball on updating this.

在更新这个时完全放弃了。

1) I should have been using text-loader, which allows me to leave in place every require('text!some/template.html');.

1)我应该一直使用text-loader,这使我可以在每个require('text!some/template.html');.

2) My root path was not absolute. Per the manual, "It must be an absolute path! Don't pass something like ./app/modules."

2)我的根路径不是绝对的。根据手册,“它必须是绝对路径!不要传递类似 ./app/modules 的东西。”

3) If your requires look like require('text!some/file.html'), then you are done. Defining the loader in webpack.config.js is redundant. If you do, your templates will end up looking like module.exports="module.exports=\"...\"".

3) 如果您的requires 看起来像require('text!some/file.html'),那么您就完成了。在 webpack.config.js 中定义加载器是多余的。如果这样做,您的模板最终将看起来像module.exports="module.exports=\"...\"".

Updated config:

更新配置:

entry: './app/js/main.js',
output: {
    path: 'dist/js/',
    filename: 'bundle.js',
},
module: {
    loaders: [/*nothing*/]
},
resolve: {
    root: [
        path.resolve('app/js'), 
        path.resolve('bower_components')
    ],
    alias: {
        ...
    }
},
plugins: [
    new webpack.ResolverPlugin(
        new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin(
            'bower.json', ['main']
        )
    )
]