From c77ae60124bbc6c00644a8b935a9189b687629d4 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 24 Jun 2020 13:20:57 +0800 Subject: [PATCH] feat: add compose-source-maps.js create sourcemap --- src/script/command-executor.ts | 108 +++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 32 deletions(-) diff --git a/src/script/command-executor.ts b/src/script/command-executor.ts index 7ecb5827..afb6fa6c 100644 --- a/src/script/command-executor.ts +++ b/src/script/command-executor.ts @@ -2125,48 +2125,78 @@ export function runHermesEmitBinaryCommand( ); } - const copyFiles = []; // Copy HBC bundle to overwrite JS bundle const source = path.join(outputFolder, bundleName + ".hbc"); const destination = path.join(outputFolder, bundleName); - - copyFiles.push([source, destination]); - if (sourcemapOutput) { - const sourceMap = path.join(outputFolder, bundleName + ".hbc" + ".map"); - if (fs.existsSync(sourceMap)) { - copyFiles.push([sourceMap, sourcemapOutput]); + + fs.copyFile(source, destination, (err) => { + if (err) { + console.error(err); + reject( + new Error( + `Copying file ${source} to ${destination} failed. "hermes" previously exited with code ${exitCode}.` + ) + ); } - } + fs.unlink(source, (err) => { + if (err) { + console.error(err); + reject(err); + } + + resolve(null as void); + }); + }); + }); + }).then(() => { + const composeSourceMapsPath = getComposeSourceMapsPath(); + if (sourcemapOutput && !composeSourceMapsPath) { + throw new Error('react-native compose-source-maps.js scripts is not found'); + } + + const jsCompilerSourceMapFile = path.join(outputFolder, bundleName + ".hbc" + ".map"); + if (!fs.existsSync(jsCompilerSourceMapFile)) { + throw new Error('sourcemap file is not found'); + } + + return new Promise((resolve, reject) => { + const composeSourceMapsArgs = [ + sourcemapOutput, + jsCompilerSourceMapFile, + "-o", + sourcemapOutput, + ]; + + // https://github.com/facebook/react-native/blob/master/react.gradle#L211 + // index.android.bundle.packager.map + index.android.bundle.compiler.map = index.android.bundle.map + const composeSourceMapsProcess = spawn(composeSourceMapsPath, composeSourceMapsArgs); + log(`${composeSourceMapsPath} ${composeSourceMapsArgs.join(" ")}`); - const recursiveCopyFile = (files) => { - const [file, target] = files.shift(); - log(`cp ${file} to ${target}`); - fs.copyFile(file, target, (err) => { + composeSourceMapsProcess.stdout.on("data", (data: Buffer) => { + log(data.toString().trim()); + }); + + composeSourceMapsProcess.stderr.on("data", (data: Buffer) => { + console.error(data.toString().trim()); + }); + + composeSourceMapsProcess.on("close", (exitCode: number) => { + if (exitCode) { + reject( + new Error(`"compose-source-maps" command exited with code ${exitCode}.`) + ); + } + + // Delete the HBC sourceMap, otherwise it will be included in 'code-push' bundle as well + fs.unlink(jsCompilerSourceMapFile, (err) => { if (err) { console.error(err); - reject( - new Error( - `Copying file ${file} to ${target} failed. "hermes" previously exited with code ${exitCode}.` - ) - ); - return; + reject(err); } - fs.unlink(file, (err) => { - if (err) { - console.error(err); - reject(err); - return; - } - if (files.length === 0) { - resolve(null as void); - } else { - recursiveCopyFile(files); - } - }); + resolve(null); }); - } - recursiveCopyFile(copyFiles); + }); }); }); } @@ -2242,6 +2272,20 @@ function getHermesCommand(): string { return path.join("node_modules", "hermesvm", getHermesOSBin(), "hermes"); } +function getComposeSourceMapsPath(): string { + // detect if compose-source-maps.js script exists + const composeSourceMaps = path.join( + "node_modules", + "react-native", + "scripts", + "compose-source-maps.js", + ); + if (fs.existsSync(composeSourceMaps)) { + return composeSourceMaps; + } + return null; +} + function serializeConnectionInfo( accessKey: string, preserveAccessKeyOnLogout: boolean,