11const fs = require ( 'fs' )
22const path = require ( `path` )
3+ const trash = require ( `trash` )
34const { spawn, exec } = require ( 'child_process' )
45const { IDENTITY_START_STR , IDENTITY_END_STR , ANNOTATION_PREFIX } = require ( './shared.js' )
56
67const PATH_PLUGIN = path . resolve ( __dirname , 'rollup-plugin-add-path-info-annoation-for-each-file.js' ) . replace ( / \\ / g, '/' )
7- const PATH_BUILD = path . resolve ( __dirname , 'build' ) . replace ( / \\ / g, '/' )
88const PATH_DEPENDENCIES = path . resolve ( __dirname , 'dependencies' ) . replace ( / \\ / g, '/' )
9+ const PATH_BUILD = path . resolve ( __dirname , 'build' ) . replace ( / \\ / g, '/' )
10+
11+
12+ const PATH_REACT_CONTAINER = path . resolve ( __dirname , 'PLACE-REACT-SOURCE-CODE-HERE' ) . replace ( / \\ / g, '/' )
13+ const dirs = fs . readdirSync ( PATH_REACT_CONTAINER ) . filter ( v => fs . lstatSync ( path . resolve ( PATH_REACT_CONTAINER , v ) . replace ( / \\ / g, '/' ) ) . isDirectory ( ) )
14+ if ( dirs . length === 0 ) { throw new Error ( 'No React Source Code Directory Found!' ) }
15+ const PATH_REACT = path . resolve ( PATH_REACT_CONTAINER , dirs [ 0 ] ) . replace ( / \\ / g, '/' )
16+ const PATH_BUILD_REACT = path . resolve ( PATH_BUILD , path . basename ( PATH_REACT ) . replace ( / \. j s / , '' ) ) . replace ( / \\ / g, '/' )
917
1018
11- const PATH_REACT = path . resolve ( __dirname , 'PLACE-REACT-SOURCE-CODE-HERE' ) . replace ( / \\ / g, '/' )
1219const PATH_REACT_NODE_MODULES = path . resolve ( PATH_REACT , 'node_modules' ) . replace ( / \\ / g, '/' )
1320const PATH_REACT_ROLLUP_BUILD_JS = path . resolve ( PATH_REACT , 'scripts/rollup/build.js' ) . replace ( / \\ / g, '/' )
1421const PATH_NEW_REACT_ROLLUP_BUILD_JS = path . resolve ( PATH_REACT , 'scripts/rollup/build.for-debug-react-code.js' ) . replace ( / \\ / g, '/' )
@@ -34,7 +41,7 @@ function generateNewBuildJS() {
3441 const lines = buildJsStr . split ( '\n' )
3542 const newFistLine = `const ${ NAME_PLUGIN } = require('${ RELATIVE_PATH_NEW_REACT_ROLLUP_BUILD_JS_TO_PLUGIN } ');\n`
3643 newBuildJsStr = newFistLine + lines . map ( line => {
37- if ( line . match ( / i f \( i s W a t c h M o d e \) / ) ) {
44+ if ( line . match ( / r o l l u p \( / ) ) {
3845 return `rollupConfig.plugins.push(${ NAME_PLUGIN } ('${ PATH_REACT } '));
3946 ${ line } `
4047 }
@@ -52,11 +59,12 @@ function installDependencies(cb) {
5259
5360function build ( cb ) {
5461 const ls = spawn ( 'node' , [ TMP , `react/index,react-dom/index` , `--type` , `UMD_DEV` ] , { cwd : PATH_REACT } )
62+ // const ls = spawn('node', [TMP], {cwd: PATH_REACT})
5563 ls . stdout . on ( 'data' , data => console . log ( data . toString ( ) ) )
5664 ls . stderr . on ( 'data' , data => console . log ( data . toString ( ) ) )
5765 ls . on ( 'close' , ( ) => {
5866 console . log ( `$$ DEBUG REACT SOURCE CODE: generated react.development.js and react-dom.development.js!` )
59- cb ( )
67+ cb && cb ( )
6068 } )
6169}
6270
@@ -71,14 +79,14 @@ function getReactOrReactDOMNamespace(reactOrReactDOMDevelopmentFile) {
7179 * @return {{outputFile, text}[] }
7280 */
7381function getReactOrReactDOMSplitFilesData ( reactOrReactDOMDevelopmentFile ) {
74- const getFileOnEndLine = ( line , lineIndex ) => {
82+ const getFileOnEndLine = ( line ) => {
7583 const isSpecialFormat = / c o m m o n j s - p r o x y - / . test ( line )
7684
7785 if ( isSpecialFormat ) {
78- let path = line . replace ( / .* c o m m o n j s - p r o x y - / , '' )
79- path = PATH . relative ( PATH_REACT , path ) . replace ( / \\ / g, '/' )
86+ let tmpPath = line . replace ( / .* c o m m o n j s - p r o x y - / , '' )
87+ tmpPath = path . relative ( PATH_REACT , tmpPath ) . replace ( / \\ / g, '/' )
8088
81- return `commonjs-proxy-/${ path } `
89+ return `commonjs-proxy-/${ tmpPath } `
8290 // return path
8391 }
8492 return line . replace ( new RegExp ( `.*${ ANNOTATION_PREFIX . replace ( '/' , '\\/' ) } ${ IDENTITY_END_STR . replace ( / \$ / g, '\\$' ) } ` ) , '' )
@@ -137,7 +145,7 @@ function getReactOrReactDOMSplitFilesData(reactOrReactDOMDevelopmentFile) {
137145function generateReactOrReactDOMSplitFiles ( filesData , reactOrReactDOMDevelopmentFile ) {
138146 for ( let { outputFile, text} of filesData ) {
139147 const folderName = path . parse ( reactOrReactDOMDevelopmentFile ) . name
140- const targetPath = path . resolve ( PATH_BUILD , `${ folderName } /${ outputFile } ` )
148+ const targetPath = path . resolve ( PATH_BUILD_REACT , `${ folderName } /${ outputFile } ` )
141149 const folerPath = path . dirname ( targetPath )
142150 ! fs . existsSync ( folerPath ) && fs . mkdirSync ( folerPath , { recursive : true } )
143151 fs . writeFileSync ( targetPath , text , { encoding : 'utf-8' } )
@@ -153,7 +161,7 @@ function copyDependencies() {
153161 ]
154162 for ( let file of dependencyPaths ) {
155163 const filename = path . parse ( file ) . base . replace ( / ^ s o u r c e - / , '' )
156- const targetPath = path . resolve ( PATH_BUILD , filename )
164+ const targetPath = path . resolve ( PATH_BUILD_REACT , filename )
157165 fs . copyFileSync ( file , targetPath )
158166 }
159167}
@@ -176,7 +184,7 @@ function generateDependencyReactDOMHTML(reactDOMFilesData, reactDOMFile) {
176184 <body>
177185 </body>
178186 </html>`
179- const targetPath = path . resolve ( PATH_BUILD , NAME_DEPENDENCY_REACT_DOM )
187+ const targetPath = path . resolve ( PATH_BUILD_REACT , NAME_DEPENDENCY_REACT_DOM )
180188 fs . writeFileSync ( targetPath , text , { encoding : 'utf-8' } )
181189}
182190
@@ -203,12 +211,31 @@ function generateDependencyReactHTML(reactFilesData, reactFile) {
203211 </script>
204212 </body>
205213 </html>`
206- const targetPath = path . resolve ( PATH_BUILD , NAME_DEPENDENCY_REACT )
214+ const targetPath = path . resolve ( PATH_BUILD_REACT , NAME_DEPENDENCY_REACT )
207215 fs . writeFileSync ( targetPath , text , { encoding : 'utf-8' } )
208216}
209217
218+ async function takeOnReactReactDOMFiles ( ) {
219+ await trash ( PATH_BUILD_REACT )
220+ const PATH_REACT_DEVELOPMENT = path . resolve ( PATH_REACT , `build/node_modules/react/umd/react.development.js` )
221+ const PATH_REACT_DOM_DEVELOPMENT = path . resolve ( PATH_REACT , `build/node_modules/react-dom/umd/react-dom.development.js` )
222+
223+ const reactFilesData = getReactOrReactDOMSplitFilesData ( PATH_REACT_DEVELOPMENT )
224+ generateReactOrReactDOMSplitFiles ( reactFilesData , PATH_REACT_DEVELOPMENT )
225+ generateDependencyReactHTML ( reactFilesData , PATH_REACT_DEVELOPMENT )
226+
227+ const reactDOMFilesData = getReactOrReactDOMSplitFilesData ( PATH_REACT_DOM_DEVELOPMENT )
228+ generateReactOrReactDOMSplitFiles ( reactDOMFilesData , PATH_REACT_DOM_DEVELOPMENT )
229+ generateDependencyReactDOMHTML ( reactDOMFilesData , PATH_REACT_DOM_DEVELOPMENT )
230+
231+ copyDependencies ( )
232+ console . log ( `$$ DEBUG REACT SOURCE CODE: built ${ path . parse ( PATH_BUILD_REACT ) . name } to ${ PATH_BUILD_REACT } !` )
233+ }
210234
211235
236+ generateNewBuildJS ( ) ; installDependencies ( ( ) => build ( takeOnReactReactDOMFiles ) )
237+ // build()
238+ // takeOnReactReactDOMFiles()
212239
213240
214241module . exports . getReactOrReactDOMSplitFilesData = getReactOrReactDOMSplitFilesData
0 commit comments