11
11
12
12
var path = require ( 'path' ) ;
13
13
var fs = require ( 'fs' ) ;
14
+ var url = require ( 'url' ) ;
14
15
15
16
// ZEAL: Allow custom build paths via .env config.
16
17
var buildPath = process . env . BUILD_PATH || 'build'
@@ -43,6 +44,37 @@ var nodePaths = (process.env.NODE_PATH || '')
43
44
. filter ( folder => ! path . isAbsolute ( folder ) )
44
45
. map ( resolveApp ) ;
45
46
47
+ var envPublicUrl = process . env . PUBLIC_URL ;
48
+
49
+ function ensureSlash ( path , needsSlash ) {
50
+ var hasSlash = path . endsWith ( '/' ) ;
51
+ if ( hasSlash && ! needsSlash ) {
52
+ return path . substr ( path , path . length - 1 ) ;
53
+ } else if ( ! hasSlash && needsSlash ) {
54
+ return path + '/' ;
55
+ } else {
56
+ return path ;
57
+ }
58
+ }
59
+
60
+ function getPublicUrl ( appPackageJson ) {
61
+ return envPublicUrl || require ( appPackageJson ) . homepage ;
62
+ }
63
+
64
+ // We use `PUBLIC_URL` environment variable or "homepage" field to infer
65
+ // "public path" at which the app is served.
66
+ // Webpack needs to know it to put the right <script> hrefs into HTML even in
67
+ // single-page apps that may serve index.html for nested URLs like /todos/42.
68
+ // We can't use a relative path in HTML because we don't want to load something
69
+ // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
70
+ function getServedPath ( appPackageJson ) {
71
+ var publicUrl = getPublicUrl ( appPackageJson ) ;
72
+ var servedUrl = envPublicUrl || (
73
+ publicUrl ? url . parse ( publicUrl ) . pathname : '/'
74
+ ) ;
75
+ return ensureSlash ( servedUrl , true ) ;
76
+ }
77
+
46
78
// config after eject: we're in ./config/
47
79
module . exports = {
48
80
appBuild : resolveApp ( buildPath ) ,
@@ -55,7 +87,9 @@ module.exports = {
55
87
testsSetup : resolveApp ( 'client/setupTests.js' ) ,
56
88
appNodeModules : resolveApp ( 'node_modules' ) ,
57
89
ownNodeModules : resolveApp ( 'node_modules' ) ,
58
- nodePaths : nodePaths
90
+ nodePaths : nodePaths ,
91
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
92
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
59
93
} ;
60
94
61
95
// @remove -on-eject-begin
@@ -76,7 +110,9 @@ module.exports = {
76
110
appNodeModules : resolveApp ( 'node_modules' ) ,
77
111
// this is empty with npm3 but node resolution searches higher anyway:
78
112
ownNodeModules : resolveOwn ( '../node_modules' ) ,
79
- nodePaths : nodePaths
113
+ nodePaths : nodePaths ,
114
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
115
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
80
116
} ;
81
117
82
118
// config before publish: we're in ./packages/react-scripts/config/
@@ -92,7 +128,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
92
128
testsSetup : resolveOwn ( '../template/client/setupTests.js' ) ,
93
129
appNodeModules : resolveOwn ( '../node_modules' ) ,
94
130
ownNodeModules : resolveOwn ( '../node_modules' ) ,
95
- nodePaths : nodePaths
131
+ nodePaths : nodePaths ,
132
+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
133
+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
96
134
} ;
97
135
}
98
136
// @remove -on-eject-end
0 commit comments