feat: support automatic public path
This solution is incomplete - it will unfortunately need to touch a many more places in several packages to work in all cases.
WebPack 5 includes support for automatically determining the public path by passing publicPath='auto'. This change adds support for PUBLIC_URL="auto" or for including "homepage" = "auto" in package.json. When "auto" is specified, webpack is configured with publicPath='auto' and the resulting webpack build will automatically determine the base path on load.
This is useful when the root path is not known at build time. It has been possible to specify PUBLIC_URL=. which correctly resolves most resources relative to the root index.html - however, in some cases (for example when launching a WebWorker) this causes issues when a script attempts to reference another script with a path relative to the base path - with PUBLIC_URL=. the second script resolves to a path relative to the first script rather than relative to index.html.
It might be worth considering making "auto" the default public path value (instead of "/") when neither homepage nor PUBLIC_URL are specified. This should be a non-breaking change (and should reduce the need for specifying this configuration at all) but I opted for leaving it out of this change to keep the surface area small. I would suggest that after this has been used as an opt-in configuration for some time it could be promoted to the default in a future release of Create React App.
This allows PUBLIC_URL=auto as a workaround solution for #12503
Hi @amcgee!
Thank you for your pull request and welcome to our community.
Action Required
In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.
Process
In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.
Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
This is unfortunately an incomplete solution - PUBLIC_URL is used for index.html template substitutions and webpack's publicPath is also assumed to be a path in several CRA webpack plugins.
setting publicPath: 'auto' directly in webpack.config.js works better, but may clash with custom publicPath handling in i.e. InlineChunkHtmlPlugin