javascript - 如何让gh-pages找到bundle.js

标签 javascript reactjs webpack bundle github-pages

我正在尝试将 webpack React 应用程序部署到我的 gh-pages。

这是我用来构建和部署的脚本。

  "scripts": {
    "start": "webpack-dev-server --open --mode development",
    "build": "webpack --mode production",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "watch": "webpack-dev-server --progress",
    "deploy": "gh-pages -d dist"
  },

运行后:

npm run build
npm run deploy

我的/dist/文件夹已部署到 gh-pages 分支。 https://github.com/Damhan/Serify/tree/gh-pages

导航至 https://damhan.github.io/Serify/ 时该页为空白。

控制台显示以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found) bundle.js:1

这是我的 webpack 配置:

const path = require("path");

module.exports = {
    entry: path.resolve(__dirname, "./src/index.js"),
    output: {
      filename:"bundle.js",
      path: path.resolve(__dirname, '/dist')
    },
    module: {
        rules: [
          {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: {
              loader: "babel-loader"
            }
          },
          {
            test: /\.s[ac]ss$/i,
            use: [
              // Creates `style` nodes from JS strings
              'style-loader',
              // Translates CSS into CommonJS
              'css-loader',
              // Compiles Sass to CSS
              'sass-loader',
            ],
          },
          {
            test: /\.css$/,
            loaders: ["style-loader", "css-loader"]
          }
        ]
    },
    devServer: {
      contentBase: path.join(__dirname, "/dist"),
      historyApiFallback:true

    }


};

最佳答案

我相信当您运行构建和部署时,它认为您的主页位于 / 。但事实上,你的主页是https://damhan.github.io/Serify/

在 package.json 中添加以下内容:

{
  "name": "serify",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://damhan.github.io/Serify/",
  ...
}

我还看了你的index.html在公共(public)文件夹中,您手动添加了 <script src=".bundle.js"></script> 。在这种情况下,我会添加 <script src="/Serify/bundle.js"></script>相反。

关于javascript - 如何让gh-pages找到bundle.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59940102/

相关文章:

javascript - 哪一个更快? `array.findIndex` 与 `array.reduce` + `object[index]`

node.js - 如何在 ReactJs API 调用上维护 Node 后端的 session

reactjs - 将resourceQuery 与Webpack v3.8.1 结合使用

javascript - Webpack 在 JS 文件中抛出一个奇怪的语法错误

javascript - Chrome 浏览器中带有固定列的 DataTable 在标题和正文之间显示空格

javascript - 为相同的属性添加对象和数组

javascript - 从 Action 设置模型并在 ember js 中重新加载模板

asp.net-mvc - 如何让reactJS在IIS上运行

javascript - 在不使用 () => function(someVal) 的情况下停止在挂载上执行的 onClick 函数

javascript - 如何使用 webpack 将 html、js 和 css 打包到一个 html 文件中?