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,8 +41,26 @@ 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 ? 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 homepagePath = getPublicUrl ( appPackageJson ) ;
58+ var homepagePathname = homepagePath ? url . parse ( homepagePath ) . pathname : '/' ;
59+ return envPublicUrl ? homepagePath : homepagePathname ;
60+ }
61+
4362// config after eject: we're in ./config/
44- var configs = {
63+ module . exports = {
4564 appBuild : resolveApp ( 'build' ) ,
4665 appPublic : resolveApp ( 'public' ) ,
4766 appHtml : resolveApp ( 'public/index.html' ) ,
@@ -52,7 +71,9 @@ var configs = {
5271 testsSetup : resolveApp ( 'src/setupTests.js' ) ,
5372 appNodeModules : resolveApp ( 'node_modules' ) ,
5473 ownNodeModules : resolveApp ( 'node_modules' ) ,
55- nodePaths : nodePaths
74+ nodePaths : nodePaths ,
75+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
76+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
5677} ;
5778
5879// @remove -on-eject-begin
@@ -61,7 +82,7 @@ function resolveOwn(relativePath) {
6182}
6283
6384// config before eject: we're in ./node_modules/react-scripts/config/
64- configs = {
85+ module . exports = {
6586 appBuild : resolveApp ( 'build' ) ,
6687 appPublic : resolveApp ( 'public' ) ,
6788 appHtml : resolveApp ( 'public/index.html' ) ,
@@ -73,12 +94,14 @@ configs = {
7394 appNodeModules : resolveApp ( 'node_modules' ) ,
7495 // this is empty with npm3 but node resolution searches higher anyway:
7596 ownNodeModules : resolveOwn ( '../node_modules' ) ,
76- nodePaths : nodePaths
97+ nodePaths : nodePaths ,
98+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
99+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
77100} ;
78101
79102// config before publish: we're in ./packages/react-scripts/config/
80103if ( __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1 ) {
81- configs = {
104+ module . exports = {
82105 appBuild : resolveOwn ( '../../../build' ) ,
83106 appPublic : resolveOwn ( '../template/public' ) ,
84107 appHtml : resolveOwn ( '../template/public/index.html' ) ,
@@ -89,11 +112,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
89112 testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
90113 appNodeModules : resolveOwn ( '../node_modules' ) ,
91114 ownNodeModules : resolveOwn ( '../node_modules' ) ,
92- nodePaths : nodePaths
115+ nodePaths : nodePaths ,
116+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
117+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
93118 } ;
94119}
95120// @remove -on-eject-end
96-
97- configs . publicUrl = process . env . PUBLIC_URL ? process . env . PUBLIC_URL : require ( configs . appPackageJson ) . homepage ;
98-
99- module . exports = configs ;
0 commit comments