カスタム PUBLIC_URL を使用して create-react-app プロジェクトをビルドできない 質問する

カスタム PUBLIC_URL を使用して create-react-app プロジェクトをビルドできない 質問する

私はしようとしています

PUBLIC_URL=http://example.com npm run build

最新の create-react-script を使用して構築されたプロジェクトを使用します。

%PUBLIC_URL%ただし、の出現は、public/index.html期待される値ではなく、空の文字列に置き換えられますPUBLIC_URL

public/index.html次のようなコードが含まれています

<script src="%PUBLIC_URL%/static/js/jarvis.widget.min.js"></script>

インターネットと Stack Overflow を何時間も検索した結果、 について書かれたものはほとんどないことがわかりましたPUBLIC_URL。 GitHub から create-react-app をクローンし、コードを閲覧していますが、まだわかりません。

私が何を間違っているのか、何かアドバイスはありますか?

ベストアンサー1

他の回答が役に立たない場合は、 にもフィールドがありますhomepagepackage.json実行すると、npm run build次のようなメッセージが表示されます。

The project was built assuming it is hosted at the server root.
To override this, specify the homepage in your package.json.
For example, add this to build it for GitHub Pages:

  "homepage" : "http://myname.github.io/myapp",

ルートフィールドの1つとして追加するだけですpackage.json。例:

{
  // ...
  "scripts": {
    // ...
  },
  "homepage": "https://example.com"
}

homepageまたは経由で正常に設定された場合PUBLIC_URL、代わりに次のようなメッセージが表示されます。

The project was built assuming it is hosted at https://example.com.
You can control this with the homepage field in your package.json.

おすすめ記事