1010
1111const path = require ( 'path' ) ;
1212const fs = require ( 'fs' ) ;
13- const url = require ( 'url ' ) ;
13+ const getPublicUrlOrPath = require ( '@devloco/create-react-wptheme-utils/getPublicUrlOrPath ' ) ;
1414
1515// Make sure any symlinks in the project folder are resolved:
1616// https://github.com/facebook/create-react-app/issues/637
1717const appDirectory = fs . realpathSync ( process . cwd ( ) ) ;
1818const resolveApp = relativePath => path . resolve ( appDirectory , relativePath ) ;
1919
20- const envPublicUrl = process . env . PUBLIC_URL ;
21-
22- function ensureSlash ( inputPath , needsSlash ) {
23- const hasSlash = inputPath . endsWith ( '/' ) ;
24- if ( hasSlash && ! needsSlash ) {
25- return inputPath . substr ( 0 , inputPath . length - 1 ) ;
26- } else if ( ! hasSlash && needsSlash ) {
27- return `${ inputPath } /` ;
28- } else {
29- return inputPath ;
30- }
31- }
32-
33- const getPublicUrl = appPackageJson =>
34- envPublicUrl || require ( appPackageJson ) . homepage ;
35-
3620// We use `PUBLIC_URL` environment variable or "homepage" field to infer
3721// "public path" at which the app is served.
38- // Webpack needs to know it to put the right <script> hrefs into HTML even in
22+ // webpack needs to know it to put the right <script> hrefs into HTML even in
3923// single-page apps that may serve index.php for nested URLs like /todos/42.
4024// We can't use a relative path in HTML because we don't want to load something
4125// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
42- function getServedPath ( appPackageJson ) {
43- const publicUrl = getPublicUrl ( appPackageJson ) ;
44- const servedUrl =
45- envPublicUrl || ( publicUrl ? url . parse ( publicUrl ) . pathname : '/' ) ;
46- return ensureSlash ( servedUrl , true ) ;
47- }
26+ const publicUrlOrPath = getPublicUrlOrPath (
27+ process . env . NODE_ENV === 'development' ,
28+ require ( resolveApp ( 'package.json' ) ) . homepage ,
29+ process . env . PUBLIC_URL
30+ ) ;
4831
4932const moduleFileExtensions = [
5033 'web.mjs' ,
@@ -89,8 +72,7 @@ module.exports = {
8972 testsSetup : resolveModule ( resolveApp , 'src/setupTests' ) ,
9073 proxySetup : resolveApp ( 'src/setupProxy.js' ) ,
9174 appNodeModules : resolveApp ( 'node_modules' ) ,
92- publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
93- servedPath : getServedPath ( resolveApp ( 'package.json' ) ) ,
75+ publicUrlOrPath,
9476} ;
9577
9678// @remove -on-eject-begin
@@ -112,8 +94,7 @@ module.exports = {
11294 testsSetup : resolveModule ( resolveApp , 'src/setupTests' ) ,
11395 proxySetup : resolveApp ( 'src/setupProxy.js' ) ,
11496 appNodeModules : resolveApp ( 'node_modules' ) ,
115- publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
116- servedPath : getServedPath ( resolveApp ( 'package.json' ) ) ,
97+ publicUrlOrPath,
11798 // These properties only exist before ejecting:
11899 ownPath : resolveOwn ( '.' ) ,
119100 ownNodeModules : resolveOwn ( 'node_modules' ) , // This is empty on npm 3
148129 testsSetup : resolveModule ( resolveOwn , `${ templatePath } /src/setupTests` ) ,
149130 proxySetup : resolveOwn ( `${ templatePath } /src/setupProxy.js` ) ,
150131 appNodeModules : resolveOwn ( 'node_modules' ) ,
151- publicUrl : getPublicUrl ( resolveOwn ( 'package.json' ) ) ,
152- servedPath : getServedPath ( resolveOwn ( 'package.json' ) ) ,
132+ publicUrlOrPath,
153133 // These properties only exist before ejecting:
154134 ownPath : resolveOwn ( '.' ) ,
155135 ownNodeModules : resolveOwn ( 'node_modules' ) ,
0 commit comments