Skip to content

Commit c403dd5

Browse files
author
Enoah Netzach
committed
Remove unnecessary duplications
1 parent e8b51e5 commit c403dd5

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

packages/react-scripts/config/paths.js

+31-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var path = require('path');
1313
var fs = require('fs');
14+
var url = require('url');
1415

1516
// Make sure any symlinks in the project folder are resolved:
1617
// https://github.com/facebookincubator/create-react-app/issues/637
@@ -35,8 +36,26 @@ var nodePaths = (process.env.NODE_PATH || '')
3536
.filter(Boolean)
3637
.map(resolveApp);
3738

39+
var envPublicUrl = process.env.PUBLIC_URL;
40+
41+
function getPublicUrl(appPackageJson) {
42+
return envPublicUrl ? envPublicUrl : require(appPackageJson).homepage;
43+
}
44+
45+
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
46+
// "public path" at which the app is served.
47+
// Webpack needs to know it to put the right <script> hrefs into HTML even in
48+
// single-page apps that may serve index.html for nested URLs like /todos/42.
49+
// We can't use a relative path in HTML because we don't want to load something
50+
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
51+
function getServedPath(appPackageJson) {
52+
var homepagePath = getPublicUrl(appPackageJson);
53+
var homepagePathname = homepagePath ? url.parse(homepagePath).pathname : '/';
54+
return envPublicUrl ? homepagePath : homepagePathname;
55+
}
56+
3857
// config after eject: we're in ./config/
39-
var configs = {
58+
module.exports = {
4059
appBuild: resolveApp('build'),
4160
appPublic: resolveApp('public'),
4261
appHtml: resolveApp('public/index.html'),
@@ -47,7 +66,9 @@ var configs = {
4766
testsSetup: resolveApp('src/setupTests.js'),
4867
appNodeModules: resolveApp('node_modules'),
4968
ownNodeModules: resolveApp('node_modules'),
50-
nodePaths: nodePaths
69+
nodePaths: nodePaths,
70+
publicUrl: getPublicUrl(resolveApp('package.json')),
71+
servedPath: getServedPath(resolveApp('package.json'))
5172
};
5273

5374
// @remove-on-eject-begin
@@ -56,7 +77,7 @@ function resolveOwn(relativePath) {
5677
}
5778

5879
// config before eject: we're in ./node_modules/react-scripts/config/
59-
configs = {
80+
module.exports = {
6081
appBuild: resolveApp('build'),
6182
appPublic: resolveApp('public'),
6283
appHtml: resolveApp('public/index.html'),
@@ -68,12 +89,14 @@ configs = {
6889
appNodeModules: resolveApp('node_modules'),
6990
// this is empty with npm3 but node resolution searches higher anyway:
7091
ownNodeModules: resolveOwn('../node_modules'),
71-
nodePaths: nodePaths
92+
nodePaths: nodePaths,
93+
publicUrl: getPublicUrl(resolveApp('package.json')),
94+
servedPath: getServedPath(resolveApp('package.json'))
7295
};
7396

7497
// config before publish: we're in ./packages/react-scripts/config/
7598
if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
76-
configs = {
99+
module.exports = {
77100
appBuild: resolveOwn('../../../build'),
78101
appPublic: resolveOwn('../template/public'),
79102
appHtml: resolveOwn('../template/public/index.html'),
@@ -84,11 +107,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
84107
testsSetup: resolveOwn('../template/src/setupTests.js'),
85108
appNodeModules: resolveOwn('../node_modules'),
86109
ownNodeModules: resolveOwn('../node_modules'),
87-
nodePaths: nodePaths
110+
nodePaths: nodePaths,
111+
publicUrl: getPublicUrl(resolveOwn('../package.json')),
112+
servedPath: getServedPath(resolveOwn('../package.json'))
88113
};
89114
}
90115
// @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

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

@@ -31,22 +30,13 @@ function ensureSlash(path, needsSlash) {
3130
}
3231
}
3332

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

0 commit comments

Comments
 (0)