Skip to content

Commit 7add7ae

Browse files
Enoah NetzachEnoahNetzach
authored andcommitted
Add support for PUBLIC_URL env variable
1 parent 8964dd6 commit 7add7ae

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

packages/react-scripts/config/paths.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var nodePaths = (process.env.NODE_PATH || '')
4141
.map(resolveApp);
4242

4343
// config after eject: we're in ./config/
44-
module.exports = {
44+
var configs = {
4545
appBuild: resolveApp('build'),
4646
appPublic: resolveApp('public'),
4747
appHtml: resolveApp('public/index.html'),
@@ -61,7 +61,7 @@ function resolveOwn(relativePath) {
6161
}
6262

6363
// config before eject: we're in ./node_modules/react-scripts/config/
64-
module.exports = {
64+
configs = {
6565
appBuild: resolveApp('build'),
6666
appPublic: resolveApp('public'),
6767
appHtml: resolveApp('public/index.html'),
@@ -78,7 +78,7 @@ module.exports = {
7878

7979
// config before publish: we're in ./packages/react-scripts/config/
8080
if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
81-
module.exports = {
81+
configs = {
8282
appBuild: resolveOwn('../../../build'),
8383
appPublic: resolveOwn('../template/public'),
8484
appHtml: resolveOwn('../template/public/index.html'),
@@ -93,3 +93,7 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
9393
};
9494
}
9595
// @remove-on-eject-end
96+
97+
configs.publicUrl = process.env.PUBLIC_URL ? process.env.PUBLIC_URL : require(configs.appPackageJson).homepage;
98+
99+
module.exports = configs;

packages/react-scripts/config/webpack.config.prod.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ function ensureSlash(path, needsSlash) {
3535
}
3636
}
3737

38-
// We use "homepage" field to infer "public path" at which the app is served.
38+
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
39+
// "public path" at which the app is served.
3940
// Webpack needs to know it to put the right <script> hrefs into HTML even in
4041
// single-page apps that may serve index.html for nested URLs like /todos/42.
4142
// We can't use a relative path in HTML because we don't want to load something
4243
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
43-
var homepagePath = require(paths.appPackageJson).homepage;
44+
var homepagePath = paths.publicUrl;
4445
var homepagePathname = homepagePath ? url.parse(homepagePath).pathname : '/';
46+
var servedPath = process.env.PUBLIC_URL ? homepagePath : homepagePathname;
4547
// Webpack uses `publicPath` to determine where the app is being served from.
4648
// It requires a trailing slash, or the file assets will get an incorrect path.
47-
var publicPath = ensureSlash(homepagePathname, true);
49+
var publicPath = ensureSlash(servedPath, true);
4850
// `publicUrl` is just like `publicPath`, but we will provide it to our app
4951
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
50-
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
51-
var publicUrl = ensureSlash(homepagePathname, false);
52+
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
53+
var publicUrl = ensureSlash(servedPath, false);
5254
// Get environment variables to inject into our app.
5355
var env = getClientEnvironment(publicUrl);
5456

packages/react-scripts/scripts/build.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require('dotenv').config({silent: true});
2121
var chalk = require('chalk');
2222
var fs = require('fs-extra');
2323
var path = require('path');
24+
var url = require('url');
2425
var filesize = require('filesize');
2526
var gzipSize = require('gzip-size').sync;
2627
var webpack = require('webpack');
@@ -158,15 +159,16 @@ function build(previousSizeMap) {
158159

159160
var openCommand = process.platform === 'win32' ? 'start' : 'open';
160161
var appPackage = require(paths.appPackageJson);
161-
var homepagePath = appPackage.homepage;
162+
var publicUrl = paths.publicUrl;
162163
var publicPath = config.output.publicPath;
163-
if (homepagePath && homepagePath.indexOf('.github.io/') !== -1) {
164+
var publicPathname = url.parse(publicPath).pathname;
165+
if (publicUrl && publicUrl.indexOf('.github.io/') !== -1) {
164166
// "homepage": "http://user.github.io/project"
165-
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
166-
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
167+
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPathname) + '.');
168+
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + ', or with the ' + chalk.green('PUBLIC_URL') + ' environment variable.');
167169
console.log();
168170
console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
169-
console.log('To publish it at ' + chalk.green(homepagePath) + ', run:');
171+
console.log('To publish it at ' + chalk.green(publicUrl) + ', run:');
170172
// If script deploy has been added to package.json, skip the instructions
171173
if (typeof appPackage.scripts.deploy === 'undefined') {
172174
console.log();
@@ -193,20 +195,20 @@ function build(previousSizeMap) {
193195
} else if (publicPath !== '/') {
194196
// "homepage": "http://mywebsite.com/project"
195197
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
196-
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
198+
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + ', or with the ' + chalk.green('PUBLIC_URL') + ' environment variable.');
197199
console.log();
198200
console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
199201
console.log();
200202
} else {
201-
// no homepage or "homepage": "http://mywebsite.com"
202-
console.log('The project was built assuming it is hosted at the server root.');
203-
if (homepagePath) {
203+
if (publicUrl) {
204204
// "homepage": "http://mywebsite.com"
205-
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
205+
console.log('The project was built assuming it is hosted at ' + chalk.green(publicUrl) + '.');
206+
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + ', or with the ' + chalk.green('PUBLIC_URL') + ' environment variable.');
206207
console.log();
207208
} else {
208209
// no homepage
209-
console.log('To override this, specify the ' + chalk.green('homepage') + ' in your ' + chalk.cyan('package.json') + '.');
210+
console.log('The project was built assuming it is hosted at the server root.');
211+
console.log('To override this, specify the ' + chalk.green('homepage') + ' in your ' + chalk.cyan('package.json') + ', or with the ' + chalk.green('PUBLIC_URL') + ' environment variable.');
210212
console.log('For example, add this to build it for GitHub Pages:')
211213
console.log();
212214
console.log(' ' + chalk.green('"homepage"') + chalk.cyan(': ') + chalk.green('"http://myname.github.io/myapp"') + chalk.cyan(','));

0 commit comments

Comments
 (0)