create-react-app 脚手架指令可以生成 React 项目,在项目完成之后执行打包命令 npm run build,文件夹内生成 build文件夹,就是打包后的文件,在浏览器运行 index.html 发现页面空白,打开控制台有报错,css 和 js 文件的引用路径有问题。

浏览 index.html 代码可以发现:

<!-- index.html -->

<!doctype html>
<html lang="zh">

<head>
    <meta charset="utf-8" />
    <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" />
    <title>React App</title>
    <link href="/static/css/main.3394d54e.chunk.css" rel="stylesheet">
</head>

<body><noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <script>!function (e) { function r(r) { for (var n, l, f = r[0], i = r[1], a = r[2], c = 0, s = []; c < f.length; c++)l = f[c], Object.prototype.hasOwnProperty.call(o, l) && o[l] && s.push(o[l][0]), o[l] = 0; for (n in i) Object.prototype.hasOwnProperty.call(i, n) && (e[n] = i[n]); for (p && p(r); s.length;)s.shift()(); return u.push.apply(u, a || []), t() } function t() { for (var e, r = 0; r < u.length; r++) { for (var t = u[r], n = !0, f = 1; f < t.length; f++) { var i = t[f]; 0 !== o[i] && (n = !1) } n && (u.splice(r--, 1), e = l(l.s = t[0])) } return e } var n = {}, o = { 1: 0 }, u = []; function l(r) { if (n[r]) return n[r].exports; var t = n[r] = { i: r, l: !1, exports: {} }; return e[r].call(t.exports, t, t.exports, l), t.l = !0, t.exports } l.m = e, l.c = n, l.d = function (e, r, t) { l.o(e, r) || Object.defineProperty(e, r, { enumerable: !0, get: t }) }, l.r = function (e) { "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e, "__esModule", { value: !0 }) }, l.t = function (e, r) { if (1 & r && (e = l(e)), 8 & r) return e; if (4 & r && "object" == typeof e && e && e.__esModule) return e; var t = Object.create(null); if (l.r(t), Object.defineProperty(t, "default", { enumerable: !0, value: e }), 2 & r && "string" != typeof e) for (var n in e) l.d(t, n, function (r) { return e[r] }.bind(null, n)); return t }, l.n = function (e) { var r = e && e.__esModule ? function () { return e.default } : function () { return e }; return l.d(r, "a", r), r }, l.o = function (e, r) { return Object.prototype.hasOwnProperty.call(e, r) }, l.p = "./"; var f = this.webpackJsonpdemo = this.webpackJsonpdemo || [], i = f.push.bind(f); f.push = r, f = f.slice(); for (var a = 0; a < f.length; a++)r(f[a]); var p = i; t() }([])</script>
    <script src="/static/js/2.1902eda2.chunk.js"></script>
    <script src="/static/js/main.cbf35db7.chunk.js"></script>
</body>

</html>

发现 css 和 js 文件都是用绝对路径引用的,这是 webpack 打包一贯拥有的问题,将 /static 改成 ./static ,再运行,页面没有问题成功显示。

但是在写实际项目的时候手动修改相对路径,不但麻烦,而且容易引起其他问题。因此直接在项目的 package.json 配置文件进行修改,添加 "homepage": "."

// package.json

{
  "name": "demo",
  "version": "0.1.0",
  "private": true,
  "homepage": ".",  // 添加这一行
  "dependencies": {
    "@babel/core": "7.12.3",
    ...

然后再执行 npm run build ,运行打包后的文件,没有任何问题。