要使用Webpack Dev Server为单页面应用程序的historyApiFallback,需要进行以下步骤:
npm install webpack-dev-server html-webpack-plugin --save-dev
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
// ...其他配置项
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
],
devServer: {
historyApiFallback: true
}
};
My App
npx webpack-dev-server
现在,当你访问http://localhost:8080/时,Webpack Dev Server会自动将请求重定向到index.html文件,并呈现你的单页面应用程序。
注意:如果你的单页面应用程序中有其他静态资源(例如图像、样式表等),你需要在webpack配置中添加其他规则,以便Webpack Dev Server可以正确地处理它们。