【问题标题】:Why does %PUBLIC_URL% not get replaced in Webpack bundled index.html?为什么 %PUBLIC_URL% 在 Webpack 捆绑的 index.html 中没有被替换?
【发布时间】:2020-04-11 22:10:16
【问题描述】:

我正在尝试部署我的 Web 应用程序的前端,但是当我部署到我的托管平台(当前为 AWS Amplify/S3)时 - website 上没有显示任何内容

我使用create-react-app 创建了应用程序,因此项目结构遵循他们的标准(publicsrc 文件夹等)。

当我在本地运行应用程序时,它工作正常。

从控制台错误来看,并查看 webpack 生成的 index.html 页面,它似乎没有替换应该在构建时替换为公共目录的 %PUBLIC_URL% 字段。

谁能解释一下如何解决这个问题?

我在下面包含了我的 webpack.config 文件,完整的 repo 可以在 here 找到

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const config = {
  entry: ['react-hot-loader/patch', './src/index.js'],
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.svg$/,
        use: 'file-loader',
      },
      {
        test: /\.png$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              mimetype: 'image/png',
            },
          },
        ],
      },
      {
        test: /\.(ttf|eot|woff|woff2)$/,
        use: 'file-loader',
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    alias: {
      'react-dom': '@hot-loader/react-dom',
    },
  },
  devServer: {
    contentBase: './build',
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve('./public/index.html'),
    }),
  ],
}

module.exports = config

【问题讨论】:

    标签: reactjs webpack deployment web-deployment aws-amplify


    【解决方案1】:

    原来这是我尝试将自定义 Webpack 配置添加到 create-react-app 项目的问题。

    我现在已经删除了 Webpack 配置,而是使用了在使用 create-react-app 构建过程时创建的 dist 文件。

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多