Skip to content

Commit fe81e4a

Browse files
committed
Polish PUBLIC_URL
1 parent bb14761 commit fe81e4a

File tree

7 files changed

+30
-37
lines changed

7 files changed

+30
-37
lines changed

packages/react-scripts/config/paths.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ var nodePaths = (process.env.NODE_PATH || '')
4343

4444
var envPublicUrl = process.env.PUBLIC_URL;
4545

46+
function ensureSlash(path, needsSlash) {
47+
var hasSlash = path.endsWith('/');
48+
if (hasSlash && !needsSlash) {
49+
return path.substr(path, path.length - 1);
50+
} else if (!hasSlash && needsSlash) {
51+
return path + '/';
52+
} else {
53+
return path;
54+
}
55+
}
56+
4657
function getPublicUrl(appPackageJson) {
4758
return envPublicUrl || require(appPackageJson).homepage;
4859
}
@@ -55,12 +66,10 @@ function getPublicUrl(appPackageJson) {
5566
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
5667
function getServedPath(appPackageJson) {
5768
var publicUrl = getPublicUrl(appPackageJson);
58-
if (!publicUrl) {
59-
return '/';
60-
} else if (envPublicUrl) {
61-
return publicUrl;
62-
}
63-
return url.parse(publicUrl).pathname;
69+
var servedUrl = envPublicUrl || (
70+
publicUrl ? url.parse(publicUrl).pathname : '/'
71+
);
72+
return ensureSlash(servedUrl, true);
6473
}
6574

6675
// config after eject: we're in ./config/

packages/react-scripts/config/utils/ensureSlash.js

-10
This file was deleted.

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var HtmlWebpackPlugin = require('html-webpack-plugin');
1515
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1616
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1717
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
18-
var ensureSlash = require('./utils/ensureSlash');
1918
var getClientEnvironment = require('./env');
2019
var paths = require('./paths');
2120

@@ -30,7 +29,7 @@ var publicPath = '/';
3029
// `publicUrl` is just like `publicPath`, but we will provide it to our app
3130
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
3231
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
33-
var publicUrl = ensureSlash(paths.servedPath, false);
32+
var publicUrl = '';
3433
// Get environment variables to inject into our app.
3534
var env = getClientEnvironment(publicUrl);
3635

@@ -116,7 +115,7 @@ module.exports = {
116115
// ** ADDING/UPDATING LOADERS **
117116
// The "url" loader handles all assets unless explicitly excluded.
118117
// The `exclude` list *must* be updated with every change to loader extensions.
119-
// When adding a new loader, you must add its `test`
118+
// When adding a new loader, you must add its `test`
120119
// as a new entry in the `exclude` list for "url" loader.
121120

122121
// "url" loader embeds assets smaller than specified size as data URLs to avoid requests.

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var ExtractTextPlugin = require('extract-text-webpack-plugin');
1616
var ManifestPlugin = require('webpack-manifest-plugin');
1717
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1818
var url = require('url');
19-
var ensureSlash = require('./utils/ensureSlash');
2019
var paths = require('./paths');
2120
var getClientEnvironment = require('./env');
2221

@@ -27,11 +26,11 @@ var path = require('path');
2726

2827
// Webpack uses `publicPath` to determine where the app is being served from.
2928
// It requires a trailing slash, or the file assets will get an incorrect path.
30-
var publicPath = ensureSlash(paths.servedPath, true);
29+
var publicPath = paths.servedPath;
3130
// `publicUrl` is just like `publicPath`, but we will provide it to our app
3231
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
3332
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
34-
var publicUrl = ensureSlash(paths.servedPath, false);
33+
var publicUrl = publicPath.slice(0, -1);
3534
// Get environment variables to inject into our app.
3635
var env = getClientEnvironment(publicUrl);
3736

@@ -106,7 +105,7 @@ module.exports = {
106105
// ** ADDING/UPDATING LOADERS **
107106
// The "url" loader handles all assets unless explicitly excluded.
108107
// The `exclude` list *must* be updated with every change to loader extensions.
109-
// When adding a new loader, you must add its `test`
108+
// When adding a new loader, you must add its `test`
110109
// as a new entry in the `exclude` list in the "url" loader.
111110

112111
// "url" loader embeds assets smaller than specified size as data URLs to avoid requests.

packages/react-scripts/fixtures/kitchensink/integration/env.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ describe('Integration', () => {
1818
it('PUBLIC_URL', async () => {
1919
const doc = await initDOM('public-url')
2020

21-
expect(doc.getElementById('feature-public-url').textContent).to.equal('http://www.example.org/spa.')
21+
const prefix = process.env.NODE_ENV === 'development' ? '' : 'http://www.example.org/spa';
22+
expect(doc.getElementById('feature-public-url').textContent).to.equal(`${prefix}.`)
2223
expect(doc.querySelector('head link[rel="shortcut icon"]').getAttribute('href'))
23-
.to.equal('http://www.example.org/spa/favicon.ico')
24+
.to.equal(`${prefix}/favicon.ico`)
2425
})
2526

2627
it('shell env variables', async () => {

packages/react-scripts/scripts/eject.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ prompt(
4343
}
4444
}
4545

46+
var folders = [
47+
'config',
48+
path.join('config', 'jest'),
49+
'scripts'
50+
];
51+
4652
var files = [
4753
path.join('config', 'env.js'),
4854
path.join('config', 'paths.js'),
@@ -51,20 +57,11 @@ prompt(
5157
path.join('config', 'webpack.config.prod.js'),
5258
path.join('config', 'jest', 'cssTransform.js'),
5359
path.join('config', 'jest', 'fileTransform.js'),
54-
path.join('config', 'utils', 'ensureSlash.js'),
5560
path.join('scripts', 'build.js'),
5661
path.join('scripts', 'start.js'),
57-
path.join('scripts', 'test.js'),
62+
path.join('scripts', 'test.js')
5863
];
5964

60-
var folders = files.reduce(function(prevFolders, file) {
61-
var dirname = path.dirname(file);
62-
if (prevFolders.indexOf(dirname) === -1) {
63-
return prevFolders.concat(dirname);
64-
}
65-
return prevFolders;
66-
}, []);
67-
6865
// Ensure that the app folder is clean and we won't override any files
6966
folders.forEach(verifyAbsent);
7067
files.forEach(verifyAbsent);

tasks/e2e-kitchensink.sh

-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ tmp_server_log=`mktemp`
135135
PORT=3001 \
136136
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
137137
NODE_PATH=src \
138-
PUBLIC_URL=http://www.example.org/spa/ \
139138
nohup npm start &>$tmp_server_log &
140139
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
141140
E2E_URL="http://localhost:3001" \
@@ -193,7 +192,6 @@ tmp_server_log=`mktemp`
193192
PORT=3002 \
194193
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
195194
NODE_PATH=src \
196-
PUBLIC_URL=http://www.example.org/spa/ \
197195
nohup npm start &>$tmp_server_log &
198196
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
199197
E2E_URL="http://localhost:3002" \

0 commit comments

Comments
 (0)