Webpack 无法解析 html-loader
Webpack can't resolve html-loader
我正在将一个使用 requirejs 的项目转换为 webpack,但在使用 "html-loader" 加载器时遇到了问题。
package.json:
"html-loader": "^0.3.0",
"webpack": "^1.11.0",
"webpack-dev-server": "^1.10.1"
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']
)
)
]
当我 运行 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
我尝试了以下方法,但没有用:
resolveLoader: {
root: [path.join(__dirname, 'node_modules')],
},
还尝试将 "webpack.config.js" 移动到项目根目录。这也没有帮助。
而且,甚至尝试使用 "raw-loader" 加载程序,这也导致了相同的 "Cannot resolve module 'raw'" 错误。
如有任何帮助,我们将不胜感激。谢谢。
更新这个完全失误了。
1) 我应该一直使用 text-loader
,这样我就可以在每个 require('text!some/template.html');
.
2) 我的根路径是 not absolute。根据手册,"It must be an absolute path! Don’t pass something like ./app/modules."
3) 如果您的 require
看起来像 require('text!some/file.html')
,那么您就完成了。在 webpack.config.js 中定义加载程序是多余的。如果这样做,您的模板最终会看起来像 module.exports="module.exports=\"...\""
。
更新配置:
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']
)
)
]
我正在将一个使用 requirejs 的项目转换为 webpack,但在使用 "html-loader" 加载器时遇到了问题。
package.json:
"html-loader": "^0.3.0",
"webpack": "^1.11.0",
"webpack-dev-server": "^1.10.1"
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']
)
)
]
当我 运行 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
我尝试了以下方法,但没有用:
resolveLoader: {
root: [path.join(__dirname, 'node_modules')],
},
还尝试将 "webpack.config.js" 移动到项目根目录。这也没有帮助。
而且,甚至尝试使用 "raw-loader" 加载程序,这也导致了相同的 "Cannot resolve module 'raw'" 错误。
如有任何帮助,我们将不胜感激。谢谢。
更新这个完全失误了。
1) 我应该一直使用 text-loader
,这样我就可以在每个 require('text!some/template.html');
.
2) 我的根路径是 not absolute。根据手册,"It must be an absolute path! Don’t pass something like ./app/modules."
3) 如果您的 require
看起来像 require('text!some/file.html')
,那么您就完成了。在 webpack.config.js 中定义加载程序是多余的。如果这样做,您的模板最终会看起来像 module.exports="module.exports=\"...\""
。
更新配置:
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']
)
)
]