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,6 +41,28 @@ 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 || 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 publicUrl = getPublicUrl ( appPackageJson ) ;
58
+ if ( ! publicUrl ) {
59
+ return '/' ;
60
+ } else if ( envPublicUrl ) {
61
+ return publicUrl ;
62
+ }
63
+ return url . parse ( publicUrl ) . pathname ;
64
+ }
65
+
43
66
// config after eject: we're in ./config/
44
67
module . exports = {
45
68
appBuild : resolveApp ( 'build' ) ,
@@ -52,7 +75,9 @@ module.exports = {
52
75
testsSetup : resolveApp ( 'src/setupTests.js' ) ,
53
76
appNodeModules : resolveApp ( 'node_modules' ) ,
54
77
ownNodeModules : resolveApp ( 'node_modules' ) ,
55
- nodePaths : nodePaths
78
+ nodePaths : nodePaths ,
79
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
80
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
56
81
} ;
57
82
58
83
// @remove -on-eject-begin
@@ -73,7 +98,9 @@ module.exports = {
73
98
appNodeModules : resolveApp ( 'node_modules' ) ,
74
99
// this is empty with npm3 but node resolution searches higher anyway:
75
100
ownNodeModules : resolveOwn ( '../node_modules' ) ,
76
- nodePaths : nodePaths
101
+ nodePaths : nodePaths ,
102
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
103
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
77
104
} ;
78
105
79
106
// config before publish: we're in ./packages/react-scripts/config/
@@ -89,7 +116,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89
116
testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
90
117
appNodeModules : resolveOwn ( '../node_modules' ) ,
91
118
ownNodeModules : resolveOwn ( '../node_modules' ) ,
92
- nodePaths : nodePaths
119
+ nodePaths : nodePaths ,
120
+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
121
+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
93
122
} ;
94
123
}
95
124
// @remove -on-eject-end
0 commit comments