1111
1212var path = require ( 'path' ) ;
1313var fs = require ( 'fs' ) ;
14+ var url = require ( 'url' ) ;
1415
1516// ZEAL: Allow custom build paths via .env config.
1617var buildPath = process . env . BUILD_PATH || 'build'
@@ -43,6 +44,37 @@ var nodePaths = (process.env.NODE_PATH || '')
4344 . filter ( folder => ! path . isAbsolute ( folder ) )
4445 . map ( resolveApp ) ;
4546
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+
4678// config after eject: we're in ./config/
4779module . exports = {
4880 appBuild : resolveApp ( buildPath ) ,
@@ -55,7 +87,9 @@ module.exports = {
5587 testsSetup : resolveApp ( 'client/setupTests.js' ) ,
5688 appNodeModules : resolveApp ( 'node_modules' ) ,
5789 ownNodeModules : resolveApp ( 'node_modules' ) ,
58- nodePaths : nodePaths
90+ nodePaths : nodePaths ,
91+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
92+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
5993} ;
6094
6195// @remove -on-eject-begin
@@ -76,7 +110,9 @@ module.exports = {
76110 appNodeModules : resolveApp ( 'node_modules' ) ,
77111 // this is empty with npm3 but node resolution searches higher anyway:
78112 ownNodeModules : resolveOwn ( '../node_modules' ) ,
79- nodePaths : nodePaths
113+ nodePaths : nodePaths ,
114+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
115+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
80116} ;
81117
82118// config before publish: we're in ./packages/react-scripts/config/
@@ -92,7 +128,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
92128 testsSetup : resolveOwn ( '../template/client/setupTests.js' ) ,
93129 appNodeModules : resolveOwn ( '../node_modules' ) ,
94130 ownNodeModules : resolveOwn ( '../node_modules' ) ,
95- nodePaths : nodePaths
131+ nodePaths : nodePaths ,
132+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
133+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
96134 } ;
97135}
98136// @remove -on-eject-end
0 commit comments