Seb*_*dez 4 webpack react-router webpack-dev-server html-webpack-plugin
由于小提示我不能得到像这样的路线
<Route path="/events/:id" component={EventDetailRoute} />
Run Code Online (Sandbox Code Playgroud)
工作,因为我读过似乎是index.html中的bundle必须是绝对的,但是我使用的是HtmlWebpackPlugin,所以bundle被注入一个相对路径.
我试图为webpack设置输出配置如下:
output: {
path: path.resolve('dist'),
filename: 'index_bundle.js',
publicPath: '/'
},
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.
如果我尝试这条路线:http:// localhost:8080/events/7,我在尝试查找http:// localhost:8080/events/index_bundle.js时收到404错误
这是我的webpack.config:
const path = require('path'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
webpack = require('webpack');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: "/" + path.resolve('dist', 'build'),
filename: 'index_bundle.js',
publicPath: '/'
},
devServer: {
historyApiFallback: true,
hot: true,
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
camelCase: 'dashes',
localIdentName: '[name]__[local]'
}
},
{
loader: 'resolve-url-loader'
},
{
loader: 'sass-loader'
}
]
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: {
loader: 'file-loader?name=[name].[ext]&outputPath=fonts/',
}
},
{
test: /\.(png|jpg)$/,
use: {
loader: 'file-loader?name=[name].[ext]&outputPath=assets/',
}
}
]
},
plugins: [
HtmlWebpackPluginConfig,
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
]
}
Run Code Online (Sandbox Code Playgroud)
我使用webpack 3.1.0,webpack-dev-server 2.5.1和react-router-dom 4.1.1
小智 16
添加<base href="/" />到index.html文件的head标记
<head>
<meta charset="utf-8">
<title>React App Setup</title>
<base href="/" />
</head>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2024 次 |
| 最近记录: |