我想在我的react项目中添加多个html页面,因为目前我不能添加react-router。我有一个侧边菜单组件,在我的设计中(我还没有尝试过)可以做到这一点:
<div className="menu">
<ul className="menu-list">
<li><a href="page1.html">Page1</a></li>
<li><a href="page2.html">Page2</a></li>
</ul>
</div>这是我实际的web包配置:
var webpack = require('webpack');
var path = require('path');
var loaders = require('./webpack.loaders');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var DashboardPlugin = require('webpack-dashboard/plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
const HOST = process.env.HOST || "127.0.0.1";
const PORT = process.env.PORT || "8888";
loaders.push({
test: /\.scss$/,
loaders: ['style-loader', 'css-loader?importLoaders=1', 'sass-loader'],
exclude: ['node_modules']
});
module.exports = {
entry: [
'react-hot-loader/patch',
'./src/index.jsx', // your app's entry point
],
devtool: process.env.WEBPACK_DEVTOOL || 'eval-source-map',
output: {
publicPath: '/',
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
loaders
},
devServer: {
contentBase: "./public",
// do not print bundle build stats
noInfo: true,
// enable HMR
hot: true,
// embed the webpack-dev-server runtime into the bundle
inline: true,
// serve index.html in place of 404 responses to allow HTML5 history
historyApiFallback: true,
port: PORT,
host: HOST
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"development"'
}
}),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin({
filename: 'style.css',
allChunks: true
}),
new DashboardPlugin(),
new HtmlWebpackPlugin({
template: './src/template.html',
files: {
css: ['style.css'],
js: [ "bundle.js"],
}
}),
]
};目前我只有一个html页面(template.html),我的想法是这样做:
entry: {
page1: './src/page1',
page2: './src/page2.js'
},
output: {
path: path.resolve(__dirname, 'build', 'target'),
publicPath: '/',
filename: '[name].bundle.js',
chunkFilename: '[id].bundle_[chunkhash].js',
sourceMapFilename: '[file].map'
},然后是这个:
plugins: [
new HtmlWebpackPlugin({
filename: 'page1.html',
template: 'src/page1.html',
chunks: ['page1']
}),
new HtmlWebpackPlugin({
filename: 'page2.html',
template: 'src/page2.html',
chunks: ['page2']
})
]但是我不能理解在哪里插入react-hot-loader,我也不知道这是否是我的问题的最佳解决方案。
发布于 2018-05-04 21:21:18
这是一个古老的问题,但我也在寻找解决方案。
你需要做的就是这样:
entry: {
page1: ['react-hot-loader/patch','./src/page1.js'],
page2: ['react-hot-loader/patch','./src/page2.js'],
},https://stackoverflow.com/questions/48171544
复制相似问题