Skip to content

Commit

Permalink
feat: add compose-source-maps.js create sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan committed Jun 24, 2020
1 parent 4de2b12 commit c77ae60
Showing 1 changed file with 76 additions and 32 deletions.
108 changes: 76 additions & 32 deletions src/script/command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c77ae60

Please sign in to comment.