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' ) ,
@@ -54,7 +86,9 @@ module.exports = {
5486 ownNodeModules : resolveApp ( 'node_modules' ) ,
5587 nodePaths : nodePaths ,
5688 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' ) )
5892} ;
5993
6094// @remove -on-eject-begin
@@ -77,7 +111,9 @@ module.exports = {
77111 ownNodeModules : resolveOwn ( '../node_modules' ) ,
78112 nodePaths : nodePaths ,
79113 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' ) )
81117} ;
82118
83119// config before publish: we're in ./packages/react-scripts/config/
@@ -95,7 +131,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
95131 ownNodeModules : resolveOwn ( '../node_modules' ) ,
96132 nodePaths : nodePaths ,
97133 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' ) )
99137 } ;
100138}
101139// @remove -on-eject-end
0 commit comments