将 index.html 移动到 public 文件夹时设置新 React 项目时出错
Error setting up new React project when moving index.html to public folder
我一直在努力解决一个看似简单的问题。我正在努力建立一个新的 React 项目并构建我的文件夹。我有这样的事情:
/node_modules
/public
|__ 0.bundle.js
|__ bundle.js
// the goal is to move index.html in this folder
/src
|__components
App.js
ClientApp.js
.babelrc
.eslintrc
index.html
package.json
server.js
webpack.config.js
yarn.lock
现在我的索引文件看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Test</title>
</head>
<body>
<div id="app"><%= body %></div>
<script src="/public/bundle.js"></script>
</body>
</html>
而我的 server.js 是:
require('babel-register')
const express = require('express')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const ReactRouter = require('react-router-dom')
const StaticRouter = ReactRouter.StaticRouter
const _ = require('lodash')
const fs = require('fs')
const PORT = 5000
const baseTemplate = fs.readFileSync('./index.html')
const template = _.template(baseTemplate)
const App = require('./src/App').default
const server = express()
server.use('/public', express.static('./public'))
server.use((req, res) => {
const body = ReactDOMServer.renderToString(
React.createElement(StaticRouter, {location: req.url, context: {}},
React.createElement(App)
)
)
res.write(template({body: body}))
res.end()
})
console.log('listening on port', PORT)
server.listen(PORT)
一切都很顺利(服务器和客户端呈现),现在我想做一个简单的更改并将 index.html
文件移动到 /public
文件夹中。我更新了指向它的相应文件以及 index
正在寻找 bundle.js.
文件的位置。所以我的新 index.html
是:
...
<body>
<div id="app"><%= body %></div>
<!-- <script src="/public/bundle.js"></script> I actually removed this -->
<script src="bundle.js"></script>
</body>
...
我的服务器更新为:
...
const baseTemplate = fs.readFileSync('./public/index.html')
const template = _.template(baseTemplate)
const App = require('./src/App').default
...
server.listen(PORT)
使用此设置,客户端渲染仍然有效,但服务器端渲染会抛出错误:
Uncaught SyntaxError: Unexpected token <
我花了一段时间才查明问题所在。显然 server.js
在我将文件移动到 public
文件夹后能够检测到该文件(如果我检查 HTML
它实际上就在那里),但加载实际文件时遇到了麻烦应用程序(意思是 js)。我觉得进行此更改会破坏我现在认为理所当然的一些参考资料。有没有想过在哪里寻找我的错误?
有趣的是我发现了问题所在,我的配置也有涉及 webpack
的问题,我跳过了。我没有根据我的新配置更新 output
路径。
output: {
path: path.join(__dirname, '/public'),
filename: 'bundle.js',
// publicPath: '/public/' --> This was causing the error
publicPath: '/js/'
},
导致错误的原因是如果 index.html
被放置在主文件夹中(因为它最初是这样的),那么它需要在 [=14] 中查找包=] 文件夹作为用 publicPath
指定的旧 webpack
配置。将 index.html
移动到 public
文件夹后,包现在将与 index.html
文件位于同一文件夹中,因此我们现在应该参考的路径是 index
自己的文件夹.此 t运行slates 到上面代码段中指定的新 publicPath
。
我希望这可以帮助其他人,我应该更加了解我的 webpack
配置,我不会 运行 进入这个问题。编码愉快!
我一直在努力解决一个看似简单的问题。我正在努力建立一个新的 React 项目并构建我的文件夹。我有这样的事情:
/node_modules
/public
|__ 0.bundle.js
|__ bundle.js
// the goal is to move index.html in this folder
/src
|__components
App.js
ClientApp.js
.babelrc
.eslintrc
index.html
package.json
server.js
webpack.config.js
yarn.lock
现在我的索引文件看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Test</title>
</head>
<body>
<div id="app"><%= body %></div>
<script src="/public/bundle.js"></script>
</body>
</html>
而我的 server.js 是:
require('babel-register')
const express = require('express')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const ReactRouter = require('react-router-dom')
const StaticRouter = ReactRouter.StaticRouter
const _ = require('lodash')
const fs = require('fs')
const PORT = 5000
const baseTemplate = fs.readFileSync('./index.html')
const template = _.template(baseTemplate)
const App = require('./src/App').default
const server = express()
server.use('/public', express.static('./public'))
server.use((req, res) => {
const body = ReactDOMServer.renderToString(
React.createElement(StaticRouter, {location: req.url, context: {}},
React.createElement(App)
)
)
res.write(template({body: body}))
res.end()
})
console.log('listening on port', PORT)
server.listen(PORT)
一切都很顺利(服务器和客户端呈现),现在我想做一个简单的更改并将 index.html
文件移动到 /public
文件夹中。我更新了指向它的相应文件以及 index
正在寻找 bundle.js.
文件的位置。所以我的新 index.html
是:
...
<body>
<div id="app"><%= body %></div>
<!-- <script src="/public/bundle.js"></script> I actually removed this -->
<script src="bundle.js"></script>
</body>
...
我的服务器更新为:
...
const baseTemplate = fs.readFileSync('./public/index.html')
const template = _.template(baseTemplate)
const App = require('./src/App').default
...
server.listen(PORT)
使用此设置,客户端渲染仍然有效,但服务器端渲染会抛出错误:
Uncaught SyntaxError: Unexpected token <
我花了一段时间才查明问题所在。显然 server.js
在我将文件移动到 public
文件夹后能够检测到该文件(如果我检查 HTML
它实际上就在那里),但加载实际文件时遇到了麻烦应用程序(意思是 js)。我觉得进行此更改会破坏我现在认为理所当然的一些参考资料。有没有想过在哪里寻找我的错误?
有趣的是我发现了问题所在,我的配置也有涉及 webpack
的问题,我跳过了。我没有根据我的新配置更新 output
路径。
output: {
path: path.join(__dirname, '/public'),
filename: 'bundle.js',
// publicPath: '/public/' --> This was causing the error
publicPath: '/js/'
},
导致错误的原因是如果 index.html
被放置在主文件夹中(因为它最初是这样的),那么它需要在 [=14] 中查找包=] 文件夹作为用 publicPath
指定的旧 webpack
配置。将 index.html
移动到 public
文件夹后,包现在将与 index.html
文件位于同一文件夹中,因此我们现在应该参考的路径是 index
自己的文件夹.此 t运行slates 到上面代码段中指定的新 publicPath
。
我希望这可以帮助其他人,我应该更加了解我的 webpack
配置,我不会 运行 进入这个问题。编码愉快!