1111const path = require ( 'path' ) ;
1212const fs = require ( 'fs' ) ;
1313const url = require ( 'url' ) ;
14+ const findPkg = require ( 'find-pkg' ) ;
15+ const globby = require ( 'globby' ) ;
1416
1517// Make sure any symlinks in the project folder are resolved:
1618// https://github.com/facebook/create-react-app/issues/637
@@ -92,6 +94,8 @@ module.exports = {
9294 servedPath : getServedPath ( resolveApp ( 'package.json' ) ) ,
9395} ;
9496
97+ let checkForMonorepo = true ;
98+
9599// @remove -on-eject-begin
96100const resolveOwn = relativePath => path . resolve ( __dirname , '..' , relativePath ) ;
97101
@@ -119,17 +123,13 @@ module.exports = {
119123 ownTypeDeclarations : resolveOwn ( 'lib/react-app.d.ts' ) ,
120124} ;
121125
122- const ownPackageJson = require ( '../package.json' ) ;
123- const reactScriptsPath = resolveApp ( `node_modules/${ ownPackageJson . name } ` ) ;
124- const reactScriptsLinked =
125- fs . existsSync ( reactScriptsPath ) &&
126- fs . lstatSync ( reactScriptsPath ) . isSymbolicLink ( ) ;
127-
128- // config before publish: we're in ./packages/react-scripts/config/
129- if (
130- ! reactScriptsLinked &&
131- __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1
132- ) {
126+ // detect if template should be used, ie. when cwd is react-scripts itself
127+ const useTemplate =
128+ appDirectory === fs . realpathSync ( path . join ( __dirname , '..' ) ) ;
129+
130+ checkForMonorepo = ! useTemplate ;
131+
132+ if ( useTemplate ) {
133133 module . exports = {
134134 dotenv : resolveOwn ( 'template/.env' ) ,
135135 appPath : resolveApp ( '.' ) ,
@@ -156,3 +156,40 @@ if (
156156// @remove -on-eject-end
157157
158158module . exports . moduleFileExtensions = moduleFileExtensions ;
159+
160+ module . exports . srcPaths = [ module . exports . appSrc ] ;
161+
162+ const findPkgs = ( rootPath , globPatterns ) => {
163+ const globOpts = {
164+ cwd : rootPath ,
165+ strict : true ,
166+ absolute : true ,
167+ } ;
168+ return globPatterns
169+ . reduce (
170+ ( pkgs , pattern ) =>
171+ pkgs . concat ( globby . sync ( path . join ( pattern , 'package.json' ) , globOpts ) ) ,
172+ [ ]
173+ )
174+ . map ( f => path . dirname ( path . normalize ( f ) ) ) ;
175+ } ;
176+
177+ const getMonorepoPkgPaths = ( ) => {
178+ const monoPkgPath = findPkg . sync ( path . resolve ( appDirectory , '..' ) ) ;
179+ if ( monoPkgPath ) {
180+ // get monorepo config from yarn workspace
181+ const pkgPatterns = require ( monoPkgPath ) . workspaces ;
182+ const pkgPaths = findPkgs ( path . dirname ( monoPkgPath ) , pkgPatterns ) ;
183+ // only include monorepo pkgs if app itself is included in monorepo
184+ if ( pkgPaths . indexOf ( appDirectory ) !== - 1 ) {
185+ return pkgPaths . filter ( f => fs . realpathSync ( f ) !== appDirectory ) ;
186+ }
187+ }
188+ return [ ] ;
189+ } ;
190+
191+ if ( checkForMonorepo ) {
192+ // if app is in a monorepo (lerna or yarn workspace), treat other packages in
193+ // the monorepo as if they are app source
194+ Array . prototype . push . apply ( module . exports . srcPaths , getMonorepoPkgPaths ( ) ) ;
195+ }
0 commit comments