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
@@ -35,8 +36,26 @@ var nodePaths = (process.env.NODE_PATH || '')
35
36
. filter ( Boolean )
36
37
. map ( resolveApp ) ;
37
38
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
+
38
57
// config after eject: we're in ./config/
39
- var configs = {
58
+ module . exports = {
40
59
appBuild : resolveApp ( 'build' ) ,
41
60
appPublic : resolveApp ( 'public' ) ,
42
61
appHtml : resolveApp ( 'public/index.html' ) ,
@@ -47,7 +66,9 @@ var configs = {
47
66
testsSetup : resolveApp ( 'src/setupTests.js' ) ,
48
67
appNodeModules : resolveApp ( 'node_modules' ) ,
49
68
ownNodeModules : resolveApp ( 'node_modules' ) ,
50
- nodePaths : nodePaths
69
+ nodePaths : nodePaths ,
70
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
71
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
51
72
} ;
52
73
53
74
// @remove -on-eject-begin
@@ -56,7 +77,7 @@ function resolveOwn(relativePath) {
56
77
}
57
78
58
79
// config before eject: we're in ./node_modules/react-scripts/config/
59
- configs = {
80
+ module . exports = {
60
81
appBuild : resolveApp ( 'build' ) ,
61
82
appPublic : resolveApp ( 'public' ) ,
62
83
appHtml : resolveApp ( 'public/index.html' ) ,
@@ -68,12 +89,14 @@ configs = {
68
89
appNodeModules : resolveApp ( 'node_modules' ) ,
69
90
// this is empty with npm3 but node resolution searches higher anyway:
70
91
ownNodeModules : resolveOwn ( '../node_modules' ) ,
71
- nodePaths : nodePaths
92
+ nodePaths : nodePaths ,
93
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
94
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
72
95
} ;
73
96
74
97
// config before publish: we're in ./packages/react-scripts/config/
75
98
if ( __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1 ) {
76
- configs = {
99
+ module . exports = {
77
100
appBuild : resolveOwn ( '../../../build' ) ,
78
101
appPublic : resolveOwn ( '../template/public' ) ,
79
102
appHtml : resolveOwn ( '../template/public/index.html' ) ,
@@ -84,11 +107,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
84
107
testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
85
108
appNodeModules : resolveOwn ( '../node_modules' ) ,
86
109
ownNodeModules : resolveOwn ( '../node_modules' ) ,
87
- nodePaths : nodePaths
110
+ nodePaths : nodePaths ,
111
+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
112
+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
88
113
} ;
89
114
}
90
115
// @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 ;
0 commit comments