1111
1212var path = require ( 'path' ) ;
1313var fs = require ( 'fs' ) ;
14+ var url = require ( 'url' ) ;
1415
1516// Make sure any symlinks in the project folder are resolved:
1617// https://github.com/facebookincubator/create-react-app/issues/637
@@ -40,6 +41,37 @@ var nodePaths = (process.env.NODE_PATH || '')
4041 . filter ( folder => ! path . isAbsolute ( folder ) )
4142 . map ( resolveApp ) ;
4243
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+
4375// config after eject: we're in ./config/
4476module . exports = {
4577 appBuild : resolveApp ( 'build' ) ,
@@ -52,7 +84,9 @@ module.exports = {
5284 testsSetup : resolveApp ( 'src/setupTests.js' ) ,
5385 appNodeModules : resolveApp ( 'node_modules' ) ,
5486 ownNodeModules : resolveApp ( 'node_modules' ) ,
55- nodePaths : nodePaths
87+ nodePaths : nodePaths ,
88+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
89+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
5690} ;
5791
5892// @remove -on-eject-begin
@@ -73,7 +107,9 @@ module.exports = {
73107 appNodeModules : resolveApp ( 'node_modules' ) ,
74108 // this is empty with npm3 but node resolution searches higher anyway:
75109 ownNodeModules : resolveOwn ( '../node_modules' ) ,
76- nodePaths : nodePaths
110+ nodePaths : nodePaths ,
111+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
112+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
77113} ;
78114
79115// config before publish: we're in ./packages/react-scripts/config/
@@ -89,7 +125,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89125 testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
90126 appNodeModules : resolveOwn ( '../node_modules' ) ,
91127 ownNodeModules : resolveOwn ( '../node_modules' ) ,
92- nodePaths : nodePaths
128+ nodePaths : nodePaths ,
129+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
130+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
93131 } ;
94132}
95133// @remove -on-eject-end
0 commit comments