我正在将大型RequireJS应用程序迁移到Webpack。 Webpack的基本构建似乎可以正常工作-我已将“路径”定义移至“别名”,并且已为我的内容和填充设置了加载器,例如jQuery。
但是,还有一个不确定的问题,我不确定如何解决。基本上,RequireJS应用程序使用“文本插件”来包含HTML模板,而Webpack为HTML模板抛出“找不到模块”错误。
我要捆绑的AMD模块示例如下所示:
带有文本插件的AMD模块
define([
'security',
'modals',
'text!../templates/contact_info.html'
], function(security, modals, contactInfoTemplate) {
return {
foo: function() { return "bar"; }
};
});
我考虑我可以使用原始加载器加载模板文件。我将'text'别名为'raw-loader':
text: {
test: /\.html$/,
loader: "raw-loader"
},
但是,我在上面需要的所有模板中看到以下错误:
Module not found: Error: Can't resolve 'text'
BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders. You need to specify 'text-loader' instead of 'text'.
我尝试用'text-loader!...'替换'text!...',然后我看到此错误,抱怨它无法加载/查找HTML模块!
Module not found: Error: Can't resolve '../templates/contact_info.html'
webpack.config.js,版本3.9.1
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
let basePath = path.join(__dirname, '/');
module.exports = {
entry: {
'main': basePath + 'js/main.js',
},
context: __dirname,
output: {
path: __dirname + '/build',
filename: '[name].min.js',
libraryTarget: 'amd',
umdNamedDefine: true
},
module: {
rules: [
{
test: /(\.js)$/,
exclude: /(node_modules)/,
use: {
// babel-loader to convert ES6 code to ES5 + amdCleaning requirejs code into simple JS code, taking care of modules to load as desired
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: []
}
}
},
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
},
{ test: /\.jpg$/, use: [ "file-loader" ] },
{ test: /\.png$/, use: [ "url-loader?mimetype=image/png" ] },
{
test: /\.(html)$/,
use: {
loader: 'raw-loader',
options: {
minimize: true
}
}
}
]
},
resolve: {
modules: [
'js/**/*.js',
'node_modules',
path.resolve('./js')
],
extensions: ['.js'], // File types,
alias: {
text: {
test: /\.html$/,
loader: "raw-loader"
},
bridge: 'libs/bridge',
cache: 'libs/cache',
cards: 'libs/cards',
moment: 'libs/moment',
underscore: 'libs/underscore',
}
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
filename: 'index.html',
template: '../index.html'
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
]
};
任何人都知道如何使Webpack与RequireJS Text插件完美配合吗?
谢谢!
答案 0 :(得分:1)
也许尝试安装text-loader
?
为使'text!../templates/contact_info.html'
之类的东西不是JS来正确地“加载”,您需要安装text-loader
才能使Webpack理解语法text!
。
npm install text-loader --save-dev
嗯...我刚刚安装了text-loaded
,看来我们还必须将text!
更改为text-loader!