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
@@ -35,8 +36,26 @@ var nodePaths = (process.env.NODE_PATH || '')
3536 . filter ( Boolean )
3637 . map ( resolveApp ) ;
3738
39+ var envPublicUrl = process . env . PUBLIC_URL ;
40+
41+ function getPublicUrl ( appPackageJson ) {
42+ return envPublicUrl ? envPublicUrl : require ( appPackageJson ) . homepage ;
43+ }
44+
45+ // We use `PUBLIC_URL` environment variable or "homepage" field to infer
46+ // "public path" at which the app is served.
47+ // Webpack needs to know it to put the right <script> hrefs into HTML even in
48+ // single-page apps that may serve index.html for nested URLs like /todos/42.
49+ // We can't use a relative path in HTML because we don't want to load something
50+ // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
51+ function getServedPath ( appPackageJson ) {
52+ var homepagePath = getPublicUrl ( appPackageJson ) ;
53+ var homepagePathname = homepagePath ? url . parse ( homepagePath ) . pathname : '/' ;
54+ return envPublicUrl ? homepagePath : homepagePathname ;
55+ }
56+
3857// config after eject: we're in ./config/
39- var configs = {
58+ module . exports = {
4059 appBuild : resolveApp ( 'build' ) ,
4160 appPublic : resolveApp ( 'public' ) ,
4261 appHtml : resolveApp ( 'public/index.html' ) ,
@@ -47,7 +66,9 @@ var configs = {
4766 testsSetup : resolveApp ( 'src/setupTests.js' ) ,
4867 appNodeModules : resolveApp ( 'node_modules' ) ,
4968 ownNodeModules : resolveApp ( 'node_modules' ) ,
50- nodePaths : nodePaths
69+ nodePaths : nodePaths ,
70+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
71+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
5172} ;
5273
5374// @remove -on-eject-begin
@@ -56,7 +77,7 @@ function resolveOwn(relativePath) {
5677}
5778
5879// config before eject: we're in ./node_modules/react-scripts/config/
59- configs = {
80+ module . exports = {
6081 appBuild : resolveApp ( 'build' ) ,
6182 appPublic : resolveApp ( 'public' ) ,
6283 appHtml : resolveApp ( 'public/index.html' ) ,
@@ -68,12 +89,14 @@ configs = {
6889 appNodeModules : resolveApp ( 'node_modules' ) ,
6990 // this is empty with npm3 but node resolution searches higher anyway:
7091 ownNodeModules : resolveOwn ( '../node_modules' ) ,
71- nodePaths : nodePaths
92+ nodePaths : nodePaths ,
93+ publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
94+ servedPath : getServedPath ( resolveApp ( 'package.json' ) )
7295} ;
7396
7497// config before publish: we're in ./packages/react-scripts/config/
7598if ( __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1 ) {
76- configs = {
99+ module . exports = {
77100 appBuild : resolveOwn ( '../../../build' ) ,
78101 appPublic : resolveOwn ( '../template/public' ) ,
79102 appHtml : resolveOwn ( '../template/public/index.html' ) ,
@@ -84,11 +107,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
84107 testsSetup : resolveOwn ( '../template/src/setupTests.js' ) ,
85108 appNodeModules : resolveOwn ( '../node_modules' ) ,
86109 ownNodeModules : resolveOwn ( '../node_modules' ) ,
87- nodePaths : nodePaths
110+ nodePaths : nodePaths ,
111+ publicUrl : getPublicUrl ( resolveOwn ( '../package.json' ) ) ,
112+ servedPath : getServedPath ( resolveOwn ( '../package.json' ) )
88113 } ;
89114}
90115// @remove -on-eject-end
91-
92- configs . publicUrl = process . env . PUBLIC_URL ? process . env . PUBLIC_URL : require ( configs . appPackageJson ) . homepage ;
93-
94- module . exports = configs ;
0 commit comments