11
11
12
12
var path = require ( 'path' ) ;
13
13
var fs = require ( 'fs' ) ;
14
+ var url = require ( 'url' ) ;
14
15
15
16
// Make sure any symlinks in the project folder are resolved:
16
17
// https://github.com/facebookincubator/create-react-app/issues/637
@@ -40,8 +41,26 @@ var nodePaths = (process.env.NODE_PATH || '')
40
41
. filter ( folder => ! path . isAbsolute ( folder ) )
41
42
. map ( resolveApp ) ;
42
43
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
+
43
62
// config after eject: we're in ./config/
44
- var configs = {
63
+ module . exports = {
45
64
appBuild : resolveApp ( 'build' ) ,
46
65
appPublic : resolveApp ( 'public' ) ,
47
66
appHtml : resolveApp ( 'public/index.html' ) ,
@@ -52,7 +71,9 @@ var configs = {
52
71
testsSetup : resolveApp ( 'src/setupTests.js' ) ,
53
72
appNodeModules : resolveApp ( 'node_modules' ) ,
54
73
ownNodeModules : resolveApp ( 'node_modules' ) ,
55
- nodePaths : nodePaths
74
+ nodePaths : nodePaths ,
75
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
76
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
56
77
} ;
57
78
58
79
// @remove -on-eject-begin
@@ -61,7 +82,7 @@ function resolveOwn(relativePath) {
61
82
}
62
83
63
84
// config before eject: we're in ./node_modules/react-scripts/config/
64
- configs = {
85
+ module . exports = {
65
86
appBuild : resolveApp ( 'build' ) ,
66
87
appPublic : resolveApp ( 'public' ) ,
67
88
appHtml : resolveApp ( 'public/index.html' ) ,
@@ -73,12 +94,14 @@ configs = {
73
94
appNodeModules : resolveApp ( 'node_modules' ) ,
74
95
// this is empty with npm3 but node resolution searches higher anyway:
75
96
ownNodeModules : resolveOwn ( '../node_modules' ) ,
76
- nodePaths : nodePaths
97
+ nodePaths : nodePaths ,
98
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
99
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
77
100
} ;
78
101
79
102
// config before publish: we're in ./packages/react-scripts/config/
80
103
if ( __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1 ) {
81
- configs = {
104
+ module . exports = {
82
105
appBuild : resolveOwn ( '../../../build' ) ,
83
106
appPublic : resolveOwn ( '../template/public' ) ,
84
107
appHtml : resolveOwn ( '../template/public/index.html' ) ,
@@ -89,11 +112,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89
112
testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
90
113
appNodeModules : resolveOwn ( '../node_modules' ) ,
91
114
ownNodeModules : resolveOwn ( '../node_modules' ) ,
92
- nodePaths : nodePaths
115
+ nodePaths : nodePaths ,
116
+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
117
+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
93
118
} ;
94
119
}
95
120
// @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 ;
0 commit comments