我无法在webpack中使用单独的javascript文件创建多个HTML文件。
我设置了多个条目,其输出配置为[name].bundle.js
。我还使用多个new HtmlWebpackPlugin()
来创建html文件。
我确定此配置确实可以实现预期的功能,但是我不知道如何配置它,因此每个javascript都单独引用了自己的html文件。我在网上和相关文档中找不到更多信息。
// webpack.config.js
module.exports = {
...
entry: {
background: './src/browser/chrome/background/index.js',
panel: './src/browser/chrome/panel/index.js',
popup: './src/browser/chrome/popup/index.js',
devtools: './src/browser/chrome/devtools/index.js',
},
output: {
path: path.resolve(__dirname, './build'),
filename: 'js/[name].bundle.js',
chunkFilename: '[id].chunk.js',
},
...
plugins: [
new HtmlWebpackPlugin({
template: './src/browser/chrome/popup/index.html',
filename: 'popup.html',
}),
new HtmlWebpackPlugin({
template: './src/browser/chrome/devtools/index.html',
filename: 'devtools.html',
}),
...
],
...
}
// devtools.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
This is the dev tools page
<script type="text/javascript" src="js/background.bundle.js"></script>
<script type="text/javascript" src="js/panel.bundle.js"></script>
<script type="text/javascript" src="js/popup.bundle.js"></script>
<script type="text/javascript" src="js/devtools.bundle.js"></script>
</body>
</html>
答案 0 :(得分:0)
您可以在多个页面上执行此操作,配置模块条目和条目
const appConfig = {
entry: {
background: './src/browser/chrome/background/index.js',
panel: './src/browser/chrome/panel/index.js',
popup: './src/browser/chrome/popup/index.js',
devtools: './src/browser/chrome/devtools/index.js',
},
port: 3002,
entries: {
background: {
title: 'background',
template: './src/browser/chrome/background/index.html',
chunks: ['background','vendors'],
},
popup: {
title: 'background',
template: './src/browser/chrome/popup/index.html',
chunks: ['popup','vendors'],
},
},
};
然后配置html-webpack-plugin
let plugins = []
_.each(appConfig.entries, (entryInfo, entryName) => {
plugins.push(new HtmlWebpackPlugin({
title: entryInfo.title,
template: entryInfo.template,
chunks: entryInfo.chunks,
chunksSortMode: 'manual',
inject: 'body',
favicon: entryInfo.favicon,
resources: entryInfo.resources
}));
});
然后配置模块和条目
let entry = _.reduce(appConfig.entry, (entries, entry, entryName) => {
entries[entryName] = entry;
return entries;
}, {});
module.export = {
entry,
plugins,
}
完成!