Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create React App 配置静态资源文件相对路径 #183

Closed
Dream4ever opened this issue Mar 25, 2022 · 0 comments
Closed

Create React App 配置静态资源文件相对路径 #183

Dream4ever opened this issue Mar 25, 2022 · 0 comments
Labels

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Mar 25, 2022

需求

在编译一个用 Create React App 写的项目时,控制台说可以配置 package.json 文件中的 homepage 字段,来指定编译后的静态资源的路径。

方案

Google package.json homepage field,找到了官方文档 Building for Relative Paths ,在 package.json 文件中配置 homepage 字段,即可指定 build 阶段所有静态资源相对于 index.html 的路径。

默认情况下 package.json 中没有配置该字段,Create React App 认为网站部署在根目录下,index.html 中引用的静态资源路径都是 /static 这种绝对路径。

如果配置为 homepage: '.',那么 index.html 中的所有静态资源则都是相对路径,在编译后的 index.html 中可以看到静态资源的路径为 ./static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant