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,28 @@ 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 getPublicUrl ( appPackageJson ) {
47+ return envPublicUrl || require ( appPackageJson ) . homepage ;
48+ }
49+
50+ // We use `PUBLIC_URL` environment variable or "homepage" field to infer
51+ // "public path" at which the app is served.
52+ // Webpack needs to know it to put the right <script> hrefs into HTML even in
53+ // single-page apps that may serve index.html for nested URLs like /todos/42.
54+ // We can't use a relative path in HTML because we don't want to load something
55+ // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
56+ function getServedPath ( appPackageJson ) {
57+ var publicUrl = getPublicUrl ( appPackageJson ) ;
58+ if ( ! publicUrl ) {
59+ return '/' ;
60+ } else if ( envPublicUrl ) {
61+ return publicUrl ;
62+ }
63+ return url . parse ( publicUrl ) . pathname ;
64+ }
65+
4366// config after eject: we're in ./config/
4467module . exports = {
4568 appBuild : resolveApp ( 'build' ) ,
@@ -52,7 +75,9 @@ module.exports = {
5275 testsSetup : resolveApp ( 'src/setupTests.js' ) ,
5376 appNodeModules : resolveApp ( 'node_modules' ) ,
5477 ownNodeModules : resolveApp ( 'node_modules' ) ,
55- nodePaths : nodePaths
78+ nodePaths : nodePaths ,
79+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
80+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
5681} ;
5782
5883// @remove -on-eject-begin
@@ -73,7 +98,9 @@ module.exports = {
7398 appNodeModules : resolveApp ( 'node_modules' ) ,
7499 // this is empty with npm3 but node resolution searches higher anyway:
75100 ownNodeModules : resolveOwn ( '../node_modules' ) ,
76- nodePaths : nodePaths
101+ nodePaths : nodePaths ,
102+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
103+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
77104} ;
78105
79106// config before publish: we're in ./packages/react-scripts/config/
@@ -89,7 +116,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89116 testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
90117 appNodeModules : resolveOwn ( '../node_modules' ) ,
91118 ownNodeModules : resolveOwn ( '../node_modules' ) ,
92- nodePaths : nodePaths
119+ nodePaths : nodePaths ,
120+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
121+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
93122 } ;
94123}
95124// @remove -on-eject-end
0 commit comments