|
| 1 | +const fs = require('fs-extra') |
| 2 | +const path = require(`path`) |
| 3 | +const trash = require(`trash`) |
| 4 | +const { spawn, exec } = require('child_process') |
| 5 | +const {IDENTITY_START_STR, IDENTITY_END_STR, ANNOTATION_PREFIX} = require('./shared.js') |
| 6 | + |
| 7 | +const PATH_PLUGIN = path.resolve(__dirname, 'rollup-plugin-add-path-info-annoation-for-each-file.js').replace(/\\/g, '/') |
| 8 | +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, '/') |
| 17 | + |
| 18 | + |
| 19 | +const PATH_REACT_NODE_MODULES = path.resolve(PATH_REACT, 'node_modules').replace(/\\/g, '/') |
| 20 | +const PATH_REACT_ROLLUP_BUILD_JS = path.resolve(PATH_REACT, 'scripts/rollup/build.js').replace(/\\/g, '/') |
| 21 | +const PATH_NEW_REACT_ROLLUP_BUILD_JS = path.resolve(PATH_REACT, 'scripts/rollup/build.for-debug-react-code.js').replace(/\\/g, '/') |
| 22 | +const TMP = 'scripts/rollup/build.for-debug-react-code.js' |
| 23 | +const RELATIVE_PATH_NEW_REACT_ROLLUP_BUILD_JS_TO_PLUGIN = path.relative(path.resolve(PATH_REACT, 'scripts/rollup' ), PATH_PLUGIN).replace(/\\/g, '/') |
| 24 | +const RELATIVE_PATH_REACT_TO__NEW_REACT_ROLLUP_BUILD_JS = path.relative(PATH_REACT, PATH_NEW_REACT_ROLLUP_BUILD_JS).replace(/\\/g, '/') |
| 25 | +const RELATIVE_PATH_ROLLUP_BUILD_JS_TO_REACT = path.relative(PATH_REACT_ROLLUP_BUILD_JS, PATH_REACT).replace(/\\/g, '/') |
| 26 | + |
| 27 | +const NAME_PLUGIN = `$$addPathInfoAnnotationForEachFilePlugin` |
| 28 | + |
| 29 | +const NAME_DEPENDENCY_REACT = 'dependency-react.html' |
| 30 | +const NAME_DEPENDENCY_REACT_DOM = 'dependency-react-dom.html' |
| 31 | +const NAME_DEPENDENCY_DEFAULT_JS = `index.js` |
| 32 | + |
| 33 | +const PATH_DEPENDENCY_SOURCE_BABEL = path.resolve(PATH_DEPENDENCIES, 'source-babel.js').replace(/\\/g, '/') |
| 34 | +const PATH_DEPENDENCY_SOURCE_DEPENDENCY_MAIN = path.resolve(PATH_DEPENDENCIES, 'source-dependency-main.html').replace(/\\/g, '/') |
| 35 | +const PATH_DEPENDENCY_SOURCE_DEFAULT_JS = path.resolve(PATH_DEPENDENCIES, 'source-index.js').replace(/\\/g, '/') |
| 36 | +const PATH_DEPENDENCY_SOURCE_DEFAULT = path.resolve(PATH_DEPENDENCIES, 'source-index.html').replace(/\\/g, '/') |
| 37 | + |
| 38 | +// # generate a new build js |
| 39 | +function generateNewBuildJS() { |
| 40 | + const buildJsStr = fs.readFileSync(PATH_REACT_ROLLUP_BUILD_JS, {encoding: 'utf8'}) |
| 41 | + const lines = buildJsStr.split('\n') |
| 42 | + const newFistLine = `const ${NAME_PLUGIN} = require('${RELATIVE_PATH_NEW_REACT_ROLLUP_BUILD_JS_TO_PLUGIN}');\n` |
| 43 | + newBuildJsStr = newFistLine + lines.map(line => { |
| 44 | + if (line.match(/rollup\(/)) { |
| 45 | + return `rollupConfig.plugins.push(${NAME_PLUGIN}('${PATH_REACT}')); |
| 46 | + ${line}` |
| 47 | + } |
| 48 | + return line |
| 49 | + }).join('\n') |
| 50 | + fs.writeFileSync(PATH_NEW_REACT_ROLLUP_BUILD_JS, newBuildJsStr, {encoding: 'utf-8'}) |
| 51 | + console.log(`$$ DEBUG REACT SOURCE CODE: generated a new build js!`) |
| 52 | +} |
| 53 | + |
| 54 | +function installDependencies(cb) { |
| 55 | + const ls = spawn(`yarn.cmd`, [`install`], {cwd: PATH_REACT}) |
| 56 | + ls.stdout.on('data', data => console.log(data.toString())) |
| 57 | + ls.stdout.on('close', cb) |
| 58 | +} |
| 59 | + |
| 60 | +function build(cb) { |
| 61 | + 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}) |
| 63 | + ls.stdout.on('data', data => console.log(data.toString())) |
| 64 | + ls.stderr.on('data', data => console.log(data.toString())) |
| 65 | + ls.on('close', () => { |
| 66 | + console.log(`$$ DEBUG REACT SOURCE CODE: generated react.development.js and react-dom.development.js!`) |
| 67 | + cb && cb() |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +function getReactOrReactDOMNamespace(reactOrReactDOMDevelopmentFile) { |
| 72 | + const isReactFile = path.parse(reactOrReactDOMDevelopmentFile).name.replace(/\..*/, '') === 'react' |
| 73 | + return isReactFile ? 'React' : 'ReactDOM' |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * |
| 78 | + * @param {*} reactOrReactDOMDevelopmentFile |
| 79 | + * @return {{outputFile, text}[]} |
| 80 | + */ |
| 81 | +function getReactOrReactDOMSplitFilesData(reactOrReactDOMDevelopmentFile) { |
| 82 | + const getFileOnEndLine = (line) => { |
| 83 | + const isSpecialFormat = /commonjs-proxy-/.test( line ) |
| 84 | + |
| 85 | + if ( isSpecialFormat ) { |
| 86 | + let tmpPath = line.replace( /.*commonjs-proxy-/, '' ) |
| 87 | + tmpPath = path.relative( PATH_REACT, tmpPath ).replace(/\\/g,'/') |
| 88 | + |
| 89 | + return `commonjs-proxy-/${ tmpPath }` |
| 90 | + // return path |
| 91 | + } |
| 92 | + return line.replace( new RegExp(`.*${ANNOTATION_PREFIX.replace('/', '\\/')}${IDENTITY_END_STR.replace(/\$/g, '\\$')} `), '' ) |
| 93 | + } |
| 94 | + const isStartLine = line => new RegExp( `${IDENTITY_START_STR.replace(/\$/g, '\\$')}` ).test( line ) |
| 95 | + const isEndLine = line => new RegExp( `${IDENTITY_END_STR.replace(/\$/g, '\\$')}` ).test( line ) |
| 96 | + |
| 97 | + const namespace = getReactOrReactDOMNamespace(reactOrReactDOMDevelopmentFile) |
| 98 | + |
| 99 | + const sourceText = fs.readFileSync(reactOrReactDOMDevelopmentFile, {encoding: 'utf8'}) |
| 100 | + const lines = sourceText.split( '\n' ) |
| 101 | + /** @type {{outputFile, text}[]} */ |
| 102 | + const data = [] |
| 103 | + let hasStarted = false |
| 104 | + // resolve the content between **end** and **start** |
| 105 | + let isBetweenEndAndStart = false |
| 106 | + /** @type {string[]} */ |
| 107 | + let curry = [] |
| 108 | + let lineIndex = -1 |
| 109 | + for (let line of lines) { |
| 110 | + lineIndex++ |
| 111 | + |
| 112 | + if (isStartLine(line)) { |
| 113 | + hasStarted = true |
| 114 | + } |
| 115 | + if (hasStarted) { |
| 116 | + if (isStartLine(line) || lineIndex === lines.length - 2) { |
| 117 | + if (isBetweenEndAndStart && curry.length > 0 && !(curry.every(line => line.trim() === ''))) { |
| 118 | + const outputFile = `$$umd/line-number-${lineIndex}.js` |
| 119 | + let text = curry.join('\n') |
| 120 | + text = text.replace(/exports/g, namespace) |
| 121 | + data.push({outputFile, text}) |
| 122 | + } |
| 123 | + isBetweenEndAndStart = false |
| 124 | + curry = [] |
| 125 | + } |
| 126 | + if (!isStartLine(line) && !isEndLine(line)) {curry.push(line)} |
| 127 | + if (isEndLine(line)) { |
| 128 | + let text = curry.join('\n') |
| 129 | + text = text.replace(/exports/g, namespace) |
| 130 | + const outputFile = getFileOnEndLine(line) |
| 131 | + data.push({outputFile, text}) |
| 132 | + curry = [] |
| 133 | + isBetweenEndAndStart = true |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + return data |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * |
| 142 | + * @param {{outputFile, text}[]} filesData |
| 143 | + * @param {string} reactOrReactDOMDevelopmentFile |
| 144 | + */ |
| 145 | +function generateReactOrReactDOMSplitFiles(filesData, reactOrReactDOMDevelopmentFile) { |
| 146 | + for (let {outputFile, text} of filesData) { |
| 147 | + const folderName = path.parse(reactOrReactDOMDevelopmentFile).name |
| 148 | + const targetPath = path.resolve(PATH_BUILD_REACT, `${folderName}/${outputFile}`) |
| 149 | + const folerPath = path.dirname(targetPath) |
| 150 | + !fs.existsSync(folerPath) && fs.mkdirSync(folerPath, {recursive: true}) |
| 151 | + fs.writeFileSync(targetPath, text, {encoding: 'utf-8'}) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function copyDependencies() { |
| 156 | + const dependencyPaths = [ |
| 157 | + PATH_DEPENDENCY_SOURCE_BABEL, |
| 158 | + PATH_DEPENDENCY_SOURCE_DEPENDENCY_MAIN, |
| 159 | + PATH_DEPENDENCY_SOURCE_DEFAULT_JS, |
| 160 | + PATH_DEPENDENCY_SOURCE_DEFAULT, |
| 161 | + ] |
| 162 | + for (let file of dependencyPaths) { |
| 163 | + const filename = path.parse(file).base.replace(/^source-/, '') |
| 164 | + const targetPath = path.resolve(PATH_BUILD_REACT, filename) |
| 165 | + fs.copyFileSync(file, targetPath) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +function generateDependencyReactDOMHTML(reactDOMFilesData, reactDOMFile) { |
| 171 | + const namespace = getReactOrReactDOMNamespace(reactDOMFile) |
| 172 | + const dependencyFolerName = path.parse(reactDOMFile).name |
| 173 | + const mainText = reactDOMFilesData.map(v => `<script src="./${dependencyFolerName}/${v.outputFile}"></script>`).join('\n') |
| 174 | + const text = `<!DOCTYPE html> |
| 175 | + <html lang="en"> |
| 176 | + <head> |
| 177 | + <meta charset="UTF-8"> |
| 178 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 179 | + <title>Dependency ${namespace}</title> |
| 180 | + <script>var React = window.parent.React;</script> |
| 181 | + <script>var ${namespace} = {};</script> |
| 182 | + ${mainText} |
| 183 | + </head> |
| 184 | + <body> |
| 185 | + </body> |
| 186 | + </html>` |
| 187 | + const targetPath = path.resolve(PATH_BUILD_REACT, NAME_DEPENDENCY_REACT_DOM) |
| 188 | + fs.writeFileSync(targetPath, text, {encoding: 'utf-8'}) |
| 189 | +} |
| 190 | + |
| 191 | +function generateDependencyReactHTML(reactFilesData, reactFile) { |
| 192 | + const namespace = getReactOrReactDOMNamespace(reactFile) |
| 193 | + const dependencyFolerName = path.parse(reactFile).name |
| 194 | + const mainText = reactFilesData.map(v => `<script src="./${dependencyFolerName}/${v.outputFile}"></script>`).join('\n') |
| 195 | + const text = `<!DOCTYPE html> |
| 196 | + <html lang="en"> |
| 197 | + <head> |
| 198 | + <meta charset="UTF-8"> |
| 199 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 200 | + <title>Dependency ${namespace}</title> |
| 201 | + <script>var ${namespace} = {};</script> |
| 202 | + ${mainText} |
| 203 | + </head> |
| 204 | + <body> |
| 205 | + <iframe id="react-dom" src="./${NAME_DEPENDENCY_REACT_DOM}" frameborder="0"></iframe> |
| 206 | + <script> |
| 207 | + const domReactDOM = document.querySelector('#react-dom') |
| 208 | + domReactDOM.contentWindow.onload = () => { |
| 209 | + window.parent.run(React, domReactDOM.contentWindow.ReactDOM) |
| 210 | + } |
| 211 | + </script> |
| 212 | + </body> |
| 213 | + </html>` |
| 214 | + const targetPath = path.resolve(PATH_BUILD_REACT, NAME_DEPENDENCY_REACT) |
| 215 | + fs.ensureFileSync(targetPath) |
| 216 | + fs.writeFileSync(targetPath, text, {encoding: 'utf-8'}) |
| 217 | +} |
| 218 | + |
| 219 | +async function takeOnReactReactDOMFiles() { |
| 220 | + await trash(PATH_BUILD_REACT) |
| 221 | + const PATH_REACT_DEVELOPMENT = path.resolve(PATH_REACT, `build/node_modules/react/umd/react.development.js`) |
| 222 | + const PATH_REACT_DOM_DEVELOPMENT = path.resolve(PATH_REACT, `build/node_modules/react-dom/umd/react-dom.development.js`) |
| 223 | + |
| 224 | + const reactFilesData = getReactOrReactDOMSplitFilesData(PATH_REACT_DEVELOPMENT) |
| 225 | + generateReactOrReactDOMSplitFiles(reactFilesData, PATH_REACT_DEVELOPMENT) |
| 226 | + generateDependencyReactHTML(reactFilesData, PATH_REACT_DEVELOPMENT) |
| 227 | + |
| 228 | + const reactDOMFilesData = getReactOrReactDOMSplitFilesData(PATH_REACT_DOM_DEVELOPMENT) |
| 229 | + generateReactOrReactDOMSplitFiles(reactDOMFilesData, PATH_REACT_DOM_DEVELOPMENT) |
| 230 | + generateDependencyReactDOMHTML(reactDOMFilesData, PATH_REACT_DOM_DEVELOPMENT) |
| 231 | + |
| 232 | + copyDependencies() |
| 233 | + console.log(`$$ DEBUG REACT SOURCE CODE: built ${path.parse(PATH_BUILD_REACT).name} to ${PATH_BUILD_REACT}!`) |
| 234 | +} |
| 235 | + |
| 236 | + |
| 237 | +generateNewBuildJS();installDependencies( () => build( takeOnReactReactDOMFiles )) |
| 238 | +// build(takeOnReactReactDOMFiles) |
| 239 | +// takeOnReactReactDOMFiles() |
| 240 | + |
| 241 | + |
| 242 | +module.exports.getReactOrReactDOMSplitFilesData = getReactOrReactDOMSplitFilesData |
| 243 | +module.exports.generateReactOrReactDOMSplitFiles = generateReactOrReactDOMSplitFiles |
| 244 | +module.exports.copyDependencies = copyDependencies |
| 245 | +module.exports.generateDependencyReactDOMHTML = generateDependencyReactDOMHTML |
| 246 | +module.exports.generateDependencyReactHTML = generateDependencyReactHTML |
| 247 | +module.exports.PATH_REACT = PATH_REACT |
0 commit comments