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,37 @@ 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 ensureSlash ( path , needsSlash ) {
47
+ var hasSlash = path . endsWith ( '/' ) ;
48
+ if ( hasSlash && ! needsSlash ) {
49
+ return path . substr ( path , path . length - 1 ) ;
50
+ } else if ( ! hasSlash && needsSlash ) {
51
+ return path + '/' ;
52
+ } else {
53
+ return path ;
54
+ }
55
+ }
56
+
57
+ function getPublicUrl ( appPackageJson ) {
58
+ return envPublicUrl || require ( appPackageJson ) . homepage ;
59
+ }
60
+
61
+ // We use `PUBLIC_URL` environment variable or "homepage" field to infer
62
+ // "public path" at which the app is served.
63
+ // Webpack needs to know it to put the right <script> hrefs into HTML even in
64
+ // single-page apps that may serve index.html for nested URLs like /todos/42.
65
+ // We can't use a relative path in HTML because we don't want to load something
66
+ // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
67
+ function getServedPath ( appPackageJson ) {
68
+ var publicUrl = getPublicUrl ( appPackageJson ) ;
69
+ var servedUrl = envPublicUrl || (
70
+ publicUrl ? url . parse ( publicUrl ) . pathname : '/'
71
+ ) ;
72
+ return ensureSlash ( servedUrl , true ) ;
73
+ }
74
+
43
75
// config after eject: we're in ./config/
44
76
module . exports = {
45
77
appBuild : resolveApp ( 'build' ) ,
@@ -54,7 +86,9 @@ module.exports = {
54
86
ownNodeModules : resolveApp ( 'node_modules' ) ,
55
87
nodePaths : nodePaths ,
56
88
testsCustomConfig : resolveApp ( 'jest-config.json' ) ,
57
- browsersFile : resolveApp ( 'browsers.json' )
89
+ browsersFile : resolveApp ( 'browsers.json' ) ,
90
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
91
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
58
92
} ;
59
93
60
94
// @remove -on-eject-begin
@@ -77,7 +111,9 @@ module.exports = {
77
111
ownNodeModules : resolveOwn ( '../node_modules' ) ,
78
112
nodePaths : nodePaths ,
79
113
testsCustomConfig : resolveApp ( 'jest-config.json' ) ,
80
- browsersFile : resolveApp ( 'browsers.json' )
114
+ browsersFile : resolveApp ( 'browsers.json' ) ,
115
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
116
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
81
117
} ;
82
118
83
119
// config before publish: we're in ./packages/react-scripts/config/
@@ -95,7 +131,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
95
131
ownNodeModules : resolveOwn ( '../node_modules' ) ,
96
132
nodePaths : nodePaths ,
97
133
testsCustomConfig : resolveApp ( 'jest-config.json' ) ,
98
- browsersFile : resolveApp ( 'browsers.json' )
134
+ browsersFile : resolveApp ( 'browsers.json' ) ,
135
+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
136
+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
99
137
} ;
100
138
}
101
139
// @remove -on-eject-end
0 commit comments