Skip to content

Commit 939ae3b

Browse files
committed
complement
1 parent 6e67295 commit 939ae3b

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"skipFiles": [
1313
"<node_internals>/**"
1414
],
15-
"program": "${workspaceFolder}/test/test.js",
15+
"program": "${workspaceFolder}/index.js",
16+
// "program": "${workspaceFolder}/test/test.js",
1617
"cwd": "${workspaceFolder}/PLACE-REACT-SOURCE-CODE-HERE"
1718
}
1819
]

index.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
const fs = require('fs')
22
const path = require(`path`)
3+
const trash = require(`trash`)
34
const { spawn, exec } = require('child_process')
45
const {IDENTITY_START_STR, IDENTITY_END_STR, ANNOTATION_PREFIX} = require('./shared.js')
56

67
const 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, '/')
88
const 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(/\.js/, '')).replace(/\\/g, '/')
917

1018

11-
const PATH_REACT = path.resolve(__dirname, 'PLACE-REACT-SOURCE-CODE-HERE').replace(/\\/g, '/')
1219
const PATH_REACT_NODE_MODULES = path.resolve(PATH_REACT, 'node_modules').replace(/\\/g, '/')
1320
const PATH_REACT_ROLLUP_BUILD_JS = path.resolve(PATH_REACT, 'scripts/rollup/build.js').replace(/\\/g, '/')
1421
const 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(/if \(isWatchMode\)/)) {
44+
if (line.match(/rollup\(/)) {
3845
return `rollupConfig.plugins.push(${NAME_PLUGIN}('${PATH_REACT}'));
3946
${line}`
4047
}
@@ -52,11 +59,12 @@ function installDependencies(cb) {
5259

5360
function 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
*/
7381
function getReactOrReactDOMSplitFilesData(reactOrReactDOMDevelopmentFile) {
74-
const getFileOnEndLine = (line, lineIndex) => {
82+
const getFileOnEndLine = (line) => {
7583
const isSpecialFormat = /commonjs-proxy-/.test( line )
7684

7785
if ( isSpecialFormat ) {
78-
let path = line.replace( /.*commonjs-proxy-/, '' )
79-
path = PATH.relative( PATH_REACT, path ).replace(/\\/g,'/')
86+
let tmpPath = line.replace( /.*commonjs-proxy-/, '' )
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) {
137145
function 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(/^source-/, '')
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

214241
module.exports.getReactOrReactDOMSplitFilesData = getReactOrReactDOMSplitFilesData

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"homepage": "https://github.com/Terry-Su/debug-react-source-code#readme",
2222
"devDependencies": {
23-
"serve": "^11.3.2"
23+
"serve": "^11.3.2",
24+
"trash": "^6.1.1"
2425
}
2526
}

rollup-plugin-add-path-info-annoation-for-each-file.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ module.exports = function addPathInfoAnnotationForEachFile (root) {
55
return {
66
name: 'add path info annotation for each file', // this name will show up in warnings and errors
77
transform(source, id) {
8+
id = id.replace(/\\/g, '/')
89
let name = root == null ? path.basename( id ) : path.relative( root, id )
910
name = name.replace(/\\/g, '/')
10-
1111
// deal with special situation
1212
const REGEXP = /commonjs\-proxy\:/
1313
const isCommonJsProxyPath = path => REGEXP.test( path )
1414
if ( isCommonJsProxyPath( id ) ) {
15-
id = id.replace( REGEXP, '/' )
16-
name = id.replace( `${root}`, '' )
15+
// id = id.replace( REGEXP, '/' )
16+
// name = id.replace( `${root}`, '' )
17+
return source
1718
}
19+
// console.log(name)
1820
return `${ANNOTATION_PREFIX}${IDENTITY_START_STR} ${ name }
1921
${source}
2022
${ANNOTATION_PREFIX}${IDENTITY_END_STR} ${ name }`

0 commit comments

Comments
 (0)