@@ -11,6 +11,8 @@ process.on( 'unhandledRejection', err => {
1111const fs = require ( 'fs-extra' ) ;
1212const path = require ( 'path' ) ;
1313const chalk = require ( 'chalk' ) ;
14+ const os = require ( 'os' ) ;
15+ const rimraf = require ( 'rimraf' ) ;
1416
1517const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
1618
@@ -29,6 +31,8 @@ module.exports = function(
2931 const reactWPScriptsPath = path . join ( appPath , 'node_modules' , pkgName ) ;
3032 const appPackage = require ( path . join ( appPath , 'package.json' ) ) ;
3133
34+ const useTypeScript = appPackage . dependencies [ 'typescript' ] != null ;
35+
3236 const scriptsPath = path . resolve (
3337 process . cwd ( ) ,
3438 'node_modules' ,
@@ -43,32 +47,90 @@ module.exports = function(
4347 appPackage . scripts . start = 'react-wp-scripts start' ;
4448 appPackage . scripts . build = 'react-wp-scripts build' ;
4549
50+ // Set relative homepage
51+ appPackage . homepage = '.' ;
52+
4653 fs . writeFileSync (
4754 path . join ( appPath , 'package.json' ) ,
4855 JSON . stringify ( appPackage , null , 2 )
4956 ) ;
5057
51- // Copy the loader.php
58+ // Remove public folder
59+ rimraf ( path . join ( appPath , 'public' ) , ( ) => { } ) ;
60+
61+ // Derive a var name we can use for a dynamic public path
62+ const publicPathVar = `${ appName . replace ( / [ \W ] + / g, '' ) } BuildURL` ;
63+
64+ // Get relevant file paths
65+ const publicPathPath = path . join ( reactWPScriptsPath , 'template/src/publicPath.js' ) ;
66+ const publicPathDest = path . join ( appPath , 'src/publicPath.js' ) ;
67+ const srcIndexPath = path . join ( appPath , 'src' , useTypeScript ? 'index.tsx' : 'index.js' ) ;
5268 const loaderPath = path . join ( reactWPScriptsPath , 'loader.php' ) ;
69+ const loaderDest = path . join ( appPath , 'react-wp-scripts.php' ) ;
5370
54- const destinationFile = path . join ( appPath , 'react-wp-scripts.php' ) ;
55- fs . copy ( loaderPath , destinationFile )
71+ // Replace %%PUBLIC_PATH_VAR%% and process.env.PUBLIC_URL in these files
72+ const publicPathFiles = [
73+ path . join ( appPath , 'src' , 'serviceWorker.js' ) ,
74+ publicPathDest ,
75+ loaderDest ,
76+ ] ;
77+
78+ fs . copy ( publicPathPath , publicPathDest )
79+ // Insert import for public path file.
80+ . then ( ( ) => new Promise ( ( resolve , reject ) => {
81+ fs . readFile ( srcIndexPath , 'utf8' , function ( err , data ) {
82+ if ( err ) {
83+ return reject ( err ) ;
84+ }
85+
86+ var result = `import './publicPath';${ os . EOL } ${ data } ` ;
87+ fs . writeFile ( srcIndexPath , result , 'utf8' , function ( err ) {
88+ if ( err ) {
89+ return reject ( err ) ;
90+ }
91+ resolve ( ) ;
92+ } ) ;
93+ } ) ;
94+ } ) )
95+ // Copy the loader.php
96+ . then ( ( ) => fs . copy ( loaderPath , loaderDest ) )
5697 . then ( ( ) => new Promise ( ( resolve , reject ) => {
5798 // Replace %%NAMESPACE%% for the specified namespace
58- fs . readFile ( destinationFile , 'utf8' , function ( err , data ) {
99+ fs . readFile ( loaderDest , 'utf8' , function ( err , data ) {
59100 if ( err ) {
60101 return reject ( err ) ;
61102 }
62103
63104 var result = data . replace ( '%%NAMESPACE%%' , namespace ) ;
64- fs . writeFile ( destinationFile , result , 'utf8' , function ( err ) {
105+ fs . writeFile ( loaderDest , result , 'utf8' , function ( err ) {
65106 if ( err ) {
66107 return reject ( err ) ;
67108 }
68109 resolve ( ) ;
69110 } ) ;
70111 } ) ;
71112 } ) )
113+ . then ( ( ) => new Promise ( ( resolve , reject ) => {
114+ publicPathFiles . forEach ( ( filePath , i ) => {
115+ fs . readFile ( filePath , 'utf8' , function ( err , data ) {
116+ if ( err ) {
117+ return reject ( err ) ;
118+ }
119+
120+ var result = data
121+ . replace ( '%%PUBLIC_PATH_VAR%%' , publicPathVar )
122+ . replace ( 'process.env.PUBLIC_URL' , publicPathVar ) ;
123+ fs . writeFile ( filePath , result , 'utf8' , function ( err ) {
124+ if ( err ) {
125+ return reject ( err ) ;
126+ }
127+ if ( i + 1 === publicPathFiles . length ) {
128+ return resolve ( ) ;
129+ }
130+ } ) ;
131+ } ) ;
132+ } ) ;
133+ } ) )
72134 . then ( ( ) => {
73135 console . log ( chalk . green ( 'React WP Scripts Loader copied to your project root folder.' ) ) ;
74136 console . log ( chalk . green ( 'Please follow these instructions to enqueue your assets in PHP:' ) ) ;
@@ -78,4 +140,5 @@ module.exports = function(
78140 console . log ( chalk . bgRed ( 'React WP Scripts loader could not be copied to your root folder. Error details:' ) ) ;
79141 console . log ( chalk . red ( err ) ) ;
80142 } ) ;
143+
81144} ;
0 commit comments