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
@@ -64,6 +66,8 @@ module.exports = {
6466 servedPath : getServedPath ( resolveApp ( 'package.json' ) ) ,
6567} ;
6668
69+ let checkForMonorepo = true ;
70+
6771// @remove -on-eject-begin
6872const resolveOwn = relativePath => path . resolve ( __dirname , '..' , relativePath ) ;
6973
@@ -88,17 +92,13 @@ module.exports = {
8892 ownNodeModules : resolveOwn ( 'node_modules' ) , // This is empty on npm 3
8993} ;
9094
91- const ownPackageJson = require ( '../package.json' ) ;
92- const reactScriptsPath = resolveApp ( `node_modules/${ ownPackageJson . name } ` ) ;
93- const reactScriptsLinked =
94- fs . existsSync ( reactScriptsPath ) &&
95- fs . lstatSync ( reactScriptsPath ) . isSymbolicLink ( ) ;
96-
97- // config before publish: we're in ./packages/react-scripts/config/
98- if (
99- ! reactScriptsLinked &&
100- __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1
101- ) {
95+ // detect if template should be used, ie. when cwd is react-scripts itself
96+ const useTemplate =
97+ appDirectory === fs . realpathSync ( path . join ( __dirname , '..' ) ) ;
98+
99+ checkForMonorepo = ! useTemplate ;
100+
101+ if ( useTemplate ) {
102102 module . exports = {
103103 dotenv : resolveOwn ( 'template/.env' ) ,
104104 appPath : resolveApp ( '.' ) ,
@@ -120,3 +120,40 @@ if (
120120 } ;
121121}
122122// @remove -on-eject-end
123+
124+ module . exports . srcPaths = [ module . exports . appSrc ] ;
125+
126+ const findPkgs = ( rootPath , globPatterns ) => {
127+ const globOpts = {
128+ cwd : rootPath ,
129+ strict : true ,
130+ absolute : true ,
131+ } ;
132+ return globPatterns
133+ . reduce (
134+ ( pkgs , pattern ) =>
135+ pkgs . concat ( globby . sync ( path . join ( pattern , 'package.json' ) , globOpts ) ) ,
136+ [ ]
137+ )
138+ . map ( f => path . dirname ( path . normalize ( f ) ) ) ;
139+ } ;
140+
141+ const getMonorepoPkgPaths = ( ) => {
142+ const monoPkgPath = findPkg . sync ( path . resolve ( appDirectory , '..' ) ) ;
143+ if ( monoPkgPath ) {
144+ // get monorepo config from yarn workspace
145+ const pkgPatterns = require ( monoPkgPath ) . workspaces ;
146+ const pkgPaths = findPkgs ( path . dirname ( monoPkgPath ) , pkgPatterns ) ;
147+ // only include monorepo pkgs if app itself is included in monorepo
148+ if ( pkgPaths . indexOf ( appDirectory ) !== - 1 ) {
149+ return pkgPaths . filter ( f => fs . realpathSync ( f ) !== appDirectory ) ;
150+ }
151+ }
152+ return [ ] ;
153+ } ;
154+
155+ if ( checkForMonorepo ) {
156+ // if app is in a monorepo (lerna or yarn workspace), treat other packages in
157+ // the monorepo as if they are app source
158+ Array . prototype . push . apply ( module . exports . srcPaths , getMonorepoPkgPaths ( ) ) ;
159+ }
0 commit comments