@@ -11,6 +11,8 @@ process.on( 'unhandledRejection', err => {
11
11
const fs = require ( 'fs-extra' ) ;
12
12
const path = require ( 'path' ) ;
13
13
const chalk = require ( 'chalk' ) ;
14
+ const os = require ( 'os' ) ;
15
+ const rimraf = require ( 'rimraf' ) ;
14
16
15
17
const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
16
18
@@ -29,6 +31,8 @@ module.exports = function(
29
31
const reactWPScriptsPath = path . join ( appPath , 'node_modules' , pkgName ) ;
30
32
const appPackage = require ( path . join ( appPath , 'package.json' ) ) ;
31
33
34
+ const useTypeScript = appPackage . dependencies [ 'typescript' ] != null ;
35
+
32
36
const scriptsPath = path . resolve (
33
37
process . cwd ( ) ,
34
38
'node_modules' ,
@@ -43,32 +47,90 @@ module.exports = function(
43
47
appPackage . scripts . start = 'react-wp-scripts start' ;
44
48
appPackage . scripts . build = 'react-wp-scripts build' ;
45
49
50
+ // Set relative homepage
51
+ appPackage . homepage = '.' ;
52
+
46
53
fs . writeFileSync (
47
54
path . join ( appPath , 'package.json' ) ,
48
55
JSON . stringify ( appPackage , null , 2 )
49
56
) ;
50
57
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' ) ;
52
68
const loaderPath = path . join ( reactWPScriptsPath , 'loader.php' ) ;
69
+ const loaderDest = path . join ( appPath , 'react-wp-scripts.php' ) ;
53
70
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 ) )
56
97
. then ( ( ) => new Promise ( ( resolve , reject ) => {
57
98
// Replace %%NAMESPACE%% for the specified namespace
58
- fs . readFile ( destinationFile , 'utf8' , function ( err , data ) {
99
+ fs . readFile ( loaderDest , 'utf8' , function ( err , data ) {
59
100
if ( err ) {
60
101
return reject ( err ) ;
61
102
}
62
103
63
104
var result = data . replace ( '%%NAMESPACE%%' , namespace ) ;
64
- fs . writeFile ( destinationFile , result , 'utf8' , function ( err ) {
105
+ fs . writeFile ( loaderDest , result , 'utf8' , function ( err ) {
65
106
if ( err ) {
66
107
return reject ( err ) ;
67
108
}
68
109
resolve ( ) ;
69
110
} ) ;
70
111
} ) ;
71
112
} ) )
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
+ } ) )
72
134
. then ( ( ) => {
73
135
console . log ( chalk . green ( 'React WP Scripts Loader copied to your project root folder.' ) ) ;
74
136
console . log ( chalk . green ( 'Please follow these instructions to enqueue your assets in PHP:' ) ) ;
@@ -78,4 +140,5 @@ module.exports = function(
78
140
console . log ( chalk . bgRed ( 'React WP Scripts loader could not be copied to your root folder. Error details:' ) ) ;
79
141
console . log ( chalk . red ( err ) ) ;
80
142
} ) ;
143
+
81
144
} ;
0 commit comments