Skip to content

Commit 3b1563e

Browse files
Enoah NetzachEnoahNetzach
authored andcommitted
Remove unnecessary duplications
1 parent 7add7ae commit 3b1563e

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

packages/react-scripts/config/paths.js

Lines changed: 31 additions & 10 deletions
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
@@ -40,8 +41,26 @@ var nodePaths = (process.env.NODE_PATH || '')
4041
.filter(folder => !path.isAbsolute(folder))
4142
.map(resolveApp);
4243

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

5879
// @remove-on-eject-begin
@@ -61,7 +82,7 @@ function resolveOwn(relativePath) {
6182
}
6283

6384
// config before eject: we're in ./node_modules/react-scripts/config/
64-
configs = {
85+
module.exports = {
6586
appBuild: resolveApp('build'),
6687
appPublic: resolveApp('public'),
6788
appHtml: resolveApp('public/index.html'),
@@ -73,12 +94,14 @@ configs = {
7394
appNodeModules: resolveApp('node_modules'),
7495
// this is empty with npm3 but node resolution searches higher anyway:
7596
ownNodeModules: resolveOwn('../node_modules'),
76-
nodePaths: nodePaths
97+
nodePaths: nodePaths,
98+
publicUrl: getPublicUrl(resolveApp('package.json')),
99+
servedPath: getServedPath(resolveApp('package.json'))
77100
};
78101

79102
// config before publish: we're in ./packages/react-scripts/config/
80103
if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
81-
configs = {
104+
module.exports = {
82105
appBuild: resolveOwn('../../../build'),
83106
appPublic: resolveOwn('../template/public'),
84107
appHtml: resolveOwn('../template/public/index.html'),
@@ -89,11 +112,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89112
testsSetup: resolveOwn('../template/src/setupTests.js'),
90113
appNodeModules: resolveOwn('../node_modules'),
91114
ownNodeModules: resolveOwn('../node_modules'),
92-
nodePaths: nodePaths
115+
nodePaths: nodePaths,
116+
publicUrl: getPublicUrl(resolveOwn('../package.json')),
117+
servedPath: getServedPath(resolveOwn('../package.json'))
93118
};
94119
}
95120
// @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: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,13 @@ function ensureSlash(path, needsSlash) {
3535
}
3636
}
3737

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

0 commit comments

Comments
 (0)