當我使用 webpack 5 構建一個 react 應用程式時,構建輸出專案主頁保持空白。我不知道哪里出了問題,這是我的 webpack 配置:
const path = require('path');
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin');
const CopyPlugin = require("copy-webpack-plugin");
module.exports = {
entry : './src/index.js',
devServer: {
port: 3000,
compress: true,
open: true,
hot: true,
},
resolve: {
extensions: ['.tsx', '.ts', '.js','.jsx'],
alias: {
'@': path.resolve(__dirname, '../src'),
},
},
output : {
path : path.resolve(__dirname, '../build') ,
filename : '[name].js'
},
module : {
rules : [
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
appendTsSuffixTo: [/\.vue$/]
},
include: [
path.resolve(__dirname, '../../../node_modules/js-wheel'),
path.resolve(__dirname, '../../../src')
],
exclude: /node_modules|\.d\.ts$/
},
{
test: /\.d\.ts$/,
loader: 'ignore-loader'
},
{
test: /\.jsx?$/,
loader: 'babel-loader'
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test : /\.(scss)$/ ,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
},
// https://stackoverflow.com/questions/69427025/programmatic-webpack-jest-esm-cant-resolve-module-without-js-file-exten
{
test: /\.m?js/,
type: "javascript/auto",
},
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file-loader',
options: {
name: '/public/icons/[name].[ext]'
}
}
]
},
plugins : [
new CopyPlugin({
patterns: [
{ from: "public/index.html", to: "index.html" },
{ from: "public/favicon.png", to: "public/favicon.png" },
{ from: "public/manifest.json", to: "public/manifest.json" },
],
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css",
}),
]
};
當我打開索引頁面時,沒有錯誤。頁面加載了這個請求:

為什么首頁一直空白?我應該怎么做才能解決這個問題?這是我的index.js:
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import { Provider } from 'react-redux'
import store from './store'
const AppView = (
<Provider store={store}>
<App />
</Provider>
)
ReactDOM.render(AppView, document.getElementById('root'))
這是我的一部分App.js:
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
class App extends React.Component {
render() {
return (<Router>
<Switch>
<Route path='/' exact render={() => <Redirect to='/index' />} />
<Route path='/500' component={View500} />
<Route path='/login' render={(props) => <Login user={this.props.user} />}/>
<Route path='/404' component={View404} />
<Route component={DefaultLayout} />
</Switch>
</Router>)
}
}
反應路由器 dom 版本是"react-router-dom": "^5.1.1". 我覺得應該不是代碼問題,因為我最近升級了webpack版本,重寫了webpack config。我應該是webpack config問題,但是我沒有弄清楚哪里出錯了。這是我的index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link href="main.css" rel="stylesheet" />
<link rel="shortcut icon" href="public/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<!-- <meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" /> -->
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="public/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
uj5u.com熱心網友回復:
從你的index.html,我沒有找到索引 js 參考,嘗試像這樣添加 js 參考:
<script src="bundle.js"></script>
僅捆綁 js 的示例名稱,替換為您的 webpack 配置的輸出名稱。
uj5u.com熱心網友回復:
嘗試使用這個。我改變了你Switch的Routes和你component的element
import { HashRouter as Router, Route, Routes, Redirect } from 'react-router-dom'
class App extends React.Component {
render() {
return (<Router>
<Routes>
<Route path='/' exact render={() => <Redirect to='/index' />} />
<Route path='/500' element={View500} />
<Route path='/login' render={(props) => <Login user={this.props.user}
/>}/>
<Route path='/404' element={View404} />
<Route element={DefaultLayout} />
</Routes>
</Router>)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432251.html
