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' ) ,
@@ -52,7 +84,9 @@ module.exports = {
52
84
testsSetup : resolveApp ( 'src/setupTests.js' ) ,
53
85
appNodeModules : resolveApp ( 'node_modules' ) ,
54
86
ownNodeModules : resolveApp ( 'node_modules' ) ,
55
- nodePaths : nodePaths
87
+ nodePaths : nodePaths ,
88
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
89
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
56
90
} ;
57
91
58
92
// @remove -on-eject-begin
@@ -73,7 +107,9 @@ module.exports = {
73
107
appNodeModules : resolveApp ( 'node_modules' ) ,
74
108
// this is empty with npm3 but node resolution searches higher anyway:
75
109
ownNodeModules : resolveOwn ( '../node_modules' ) ,
76
- nodePaths : nodePaths
110
+ nodePaths : nodePaths ,
111
+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
112
+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
77
113
} ;
78
114
79
115
// config before publish: we're in ./packages/react-scripts/config/
@@ -89,7 +125,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89
125
testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
90
126
appNodeModules : resolveOwn ( '../node_modules' ) ,
91
127
ownNodeModules : resolveOwn ( '../node_modules' ) ,
92
- nodePaths : nodePaths
128
+ nodePaths : nodePaths ,
129
+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
130
+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
93
131
} ;
94
132
}
95
133
// @remove -on-eject-end
0 commit comments