@@ -40,6 +40,37 @@ function getGitStatus() {
40
40
}
41
41
}
42
42
43
+ function ejectContent ( content , { additionalDeps, pluginPaths } ) {
44
+ const { code, dependencies, paths : newPaths } = ejectFile ( {
45
+ code : content ,
46
+ existingDependencies : additionalDeps ,
47
+ } ) ;
48
+ for ( const [ key , value ] of dependencies ) {
49
+ additionalDeps . set ( key , value ) ;
50
+ }
51
+ for ( const newPath of newPaths ) {
52
+ pluginPaths . add ( newPath ) ;
53
+ }
54
+ return code ;
55
+ }
56
+
57
+ function addPlugins ( pluginPaths ) {
58
+ if ( pluginPaths . size < 1 ) {
59
+ return ;
60
+ }
61
+
62
+ console . log ( cyan ( 'Adding plugins' ) ) ;
63
+
64
+ for ( const pluginPath of pluginPaths ) {
65
+ const pluginName = / .* r e a c t - s c r i p t s - p l u g i n - ( [ \w - ] + ) / . exec ( pluginPath ) . pop ( ) ;
66
+ console . log ( ` Applying ${ cyan ( pluginName ) } ` ) ;
67
+ const { eject } = require ( pluginPath ) ;
68
+ eject ( { paths } ) ;
69
+ }
70
+
71
+ console . log ( ) ;
72
+ }
73
+
43
74
inquirer
44
75
. prompt ( {
45
76
type : 'confirm' ,
@@ -115,28 +146,20 @@ inquirer
115
146
fs . mkdirSync ( path . join ( appPath , folder ) ) ;
116
147
} ) ;
117
148
118
- let addtlDeps = new Map ( ) ;
119
- let pluginPaths = new Set ( ) ;
149
+ const additionalDeps = new Map ( ) ,
150
+ pluginPaths = new Set ( ) ;
120
151
files . forEach ( file => {
121
152
let content = fs . readFileSync ( file , 'utf8' ) ;
122
153
123
154
// Skip flagged files
124
155
if ( content . match ( / \/ \/ @ r e m o v e - f i l e - o n - e j e c t / ) ) {
125
156
return ;
126
157
}
127
- // Inline plugins
128
- if (
129
- file . endsWith ( 'webpack.config.dev.js' ) ||
130
- file . endsWith ( 'webpack.config.prod.js' )
131
- ) {
132
- const { code, dependencies, paths : newPaths } = ejectFile ( {
133
- code : content ,
134
- existingDependencies : addtlDeps ,
135
- } ) ;
136
- content = code ;
137
- addtlDeps = new Map ( [ ...addtlDeps , ...dependencies ] ) ;
138
- pluginPaths = new Set ( [ ...pluginPaths , ...newPaths ] ) ;
158
+ // Remove plugins
159
+ if ( content . match ( / \/ \/ @ r e m o v e - p l u g i n s - o n - e j e c t / ) ) {
160
+ content = ejectContent ( content , { additionalDeps, pluginPaths } ) ;
139
161
}
162
+
140
163
content =
141
164
content
142
165
// Remove dead code from .js files on eject
@@ -155,39 +178,13 @@ inquirer
155
178
} ) ;
156
179
console . log ( ) ;
157
180
158
- if ( pluginPaths . size > 0 ) {
159
- console . log ( cyan ( 'Adding plugins' ) ) ;
160
- }
161
- for ( const pluginPath of pluginPaths ) {
162
- const pluginName = / .* r e a c t - s c r i p t s - p l u g i n - ( [ \w - ] + ) /
163
- . exec ( pluginPath )
164
- . pop ( ) ;
165
- console . log ( ` Applying ${ cyan ( pluginName ) } ` ) ;
166
- const { eject } = require ( pluginPath ) ;
167
- eject ( { paths } ) ;
168
- }
169
- if ( pluginPaths . size > 0 ) {
170
- console . log ( ) ;
171
- }
181
+ addPlugins ( pluginPaths ) ;
172
182
173
- const {
174
- name : ownPackageName ,
175
- dependencies : _ownDependencies ,
176
- optionalDependencies : ownOptionalDependencies ,
177
- bin : ownBin ,
178
- } = require ( path . join ( ownPath , 'package.json' ) ) ;
183
+ const ownPackage = require ( path . join ( ownPath , 'package.json' ) ) ;
179
184
const appPackage = require ( path . join ( appPath , 'package.json' ) ) ;
180
185
181
- const ownDependencies = Object . assign (
182
- { } ,
183
- _ownDependencies ,
184
- Array . from ( addtlDeps ) . reduce (
185
- ( prev , [ pkg , version ] ) => Object . assign ( prev , { [ pkg ] : version } ) ,
186
- { }
187
- )
188
- ) ;
189
-
190
186
console . log ( cyan ( 'Updating the dependencies' ) ) ;
187
+ const ownPackageName = ownPackage . name ;
191
188
if ( appPackage . devDependencies ) {
192
189
// We used to put react-scripts in devDependencies
193
190
if ( appPackage . devDependencies [ ownPackageName ] ) {
@@ -200,9 +197,18 @@ inquirer
200
197
console . log ( ` Removing ${ cyan ( ownPackageName ) } from dependencies` ) ;
201
198
delete appPackage . dependencies [ ownPackageName ] ;
202
199
}
200
+ // Combine `react-scripts` dependencies with additional dependencies
201
+ const ownDependencies = Object . assign (
202
+ { } ,
203
+ ownPackage . dependencies ,
204
+ Array . from ( additionalDeps ) . reduce (
205
+ ( prev , [ pkg , version ] ) => Object . assign ( prev , { [ pkg ] : version } ) ,
206
+ { }
207
+ )
208
+ ) ;
203
209
Object . keys ( ownDependencies ) . forEach ( key => {
204
210
// For some reason optionalDependencies end up in dependencies after install
205
- if ( ownOptionalDependencies [ key ] ) {
211
+ if ( ownPackage . optionalDependencies [ key ] ) {
206
212
return ;
207
213
}
208
214
console . log ( ` Adding ${ cyan ( key ) } to dependencies` ) ;
@@ -219,7 +225,7 @@ inquirer
219
225
console . log ( cyan ( 'Updating the scripts' ) ) ;
220
226
delete appPackage . scripts [ 'eject' ] ;
221
227
Object . keys ( appPackage . scripts ) . forEach ( key => {
222
- Object . keys ( ownBin ) . forEach ( binKey => {
228
+ Object . keys ( ownPackage . bin ) . forEach ( binKey => {
223
229
const regex = new RegExp ( binKey + ' (\\w+)' , 'g' ) ;
224
230
if ( ! regex . test ( appPackage . scripts [ key ] ) ) {
225
231
return ;
@@ -264,7 +270,7 @@ inquirer
264
270
if ( ownPath . indexOf ( appPath ) === 0 ) {
265
271
try {
266
272
// remove react-scripts and react-scripts binaries from app node_modules
267
- Object . keys ( ownBin ) . forEach ( binKey => {
273
+ Object . keys ( ownPackage . bin ) . forEach ( binKey => {
268
274
fs . removeSync ( path . join ( appPath , 'node_modules' , '.bin' , binKey ) ) ;
269
275
} ) ;
270
276
fs . removeSync ( ownPath ) ;
0 commit comments