Skip to content

Commit cff3ec4

Browse files
author
Enoah Netzach
committed
Add support for PUBLIC_URL env variable
1 parent dc6edce commit cff3ec4

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

packages/react-scripts/config/paths.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var nodePaths = (process.env.NODE_PATH || '')
3636
.map(resolveApp);
3737

3838
// config after eject: we're in ./config/
39-
module.exports = {
39+
var configs = {
4040
appBuild: resolveApp('build'),
4141
appPublic: resolveApp('public'),
4242
appHtml: resolveApp('public/index.html'),
@@ -56,7 +56,7 @@ function resolveOwn(relativePath) {
5656
}
5757

5858
// config before eject: we're in ./node_modules/react-scripts/config/
59-
module.exports = {
59+
configs = {
6060
appBuild: resolveApp('build'),
6161
appPublic: resolveApp('public'),
6262
appHtml: resolveApp('public/index.html'),
@@ -73,7 +73,7 @@ module.exports = {
7373

7474
// config before publish: we're in ./packages/react-scripts/config/
7575
if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
76-
module.exports = {
76+
configs = {
7777
appBuild: resolveOwn('../../../build'),
7878
appPublic: resolveOwn('../template/public'),
7979
appHtml: resolveOwn('../template/public/index.html'),
@@ -88,3 +88,7 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
8888
};
8989
}
9090
// @remove-on-eject-end
91+
92+
configs.publicUrl = process.env.PUBLIC_URL ? process.env.PUBLIC_URL : require(configs.appPackageJson).homepage;
93+
94+
module.exports = configs;

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ function ensureSlash(path, needsSlash) {
3131
}
3232
}
3333

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

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Configuration and scripts for Create React App.",
55
"repository": "facebookincubator/create-react-app",
66
"license": "BSD-3-Clause",
7+
"homepage": "https://s3.eu-central-1.amazonaws.com/my-project",
78
"engines": {
89
"node": ">=4"
910
},

packages/react-scripts/scripts/build.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var chalk = require('chalk');
2222
var fs = require('fs-extra');
2323
var path = require('path');
2424
var pathExists = require('path-exists');
25+
var url = require('url');
2526
var filesize = require('filesize');
2627
var gzipSize = require('gzip-size').sync;
2728
var webpack = require('webpack');
@@ -153,15 +154,16 @@ function build(previousSizeMap) {
153154
console.log();
154155

155156
var openCommand = process.platform === 'win32' ? 'start' : 'open';
156-
var homepagePath = require(paths.appPackageJson).homepage;
157+
var publicUrl = paths.publicUrl;
157158
var publicPath = config.output.publicPath;
158-
if (homepagePath && homepagePath.indexOf('.github.io/') !== -1) {
159+
var publicPathname = url.parse(publicPath).pathname;
160+
if (publicUrl && publicUrl.indexOf('.github.io/') !== -1) {
159161
// "homepage": "http://user.github.io/project"
160-
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
161-
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
162+
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPathname) + '.');
163+
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.');
162164
console.log();
163165
console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
164-
console.log('To publish it at ' + chalk.green(homepagePath) + ', run:');
166+
console.log('To publish it at ' + chalk.green(publicUrl) + ', run:');
165167
console.log();
166168
if (useYarn) {
167169
console.log(' ' + chalk.cyan('yarn') + ' add --dev gh-pages');
@@ -184,20 +186,20 @@ function build(previousSizeMap) {
184186
} else if (publicPath !== '/') {
185187
// "homepage": "http://mywebsite.com/project"
186188
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
187-
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
189+
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.');
188190
console.log();
189191
console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
190192
console.log();
191193
} else {
192-
// no homepage or "homepage": "http://mywebsite.com"
193-
console.log('The project was built assuming it is hosted at the server root.');
194-
if (homepagePath) {
194+
if (publicUrl) {
195195
// "homepage": "http://mywebsite.com"
196-
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
196+
console.log('The project was built assuming it is hosted at ' + chalk.green(publicUrl) + '.');
197+
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.');
197198
console.log();
198199
} else {
199200
// no homepage
200-
console.log('To override this, specify the ' + chalk.green('homepage') + ' in your ' + chalk.cyan('package.json') + '.');
201+
console.log('The project was built assuming it is hosted at the server root.');
202+
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.');
201203
console.log('For example, add this to build it for GitHub Pages:')
202204
console.log();
203205
console.log(' ' + chalk.green('"homepage"') + chalk.cyan(': ') + chalk.green('"http://myname.github.io/myapp"') + chalk.cyan(','));

0 commit comments

Comments
 (0)