1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

Support hermes bytecode

This commit is contained in:
sunnylqm 2019-07-22 22:48:39 +08:00
parent 6f0755e571
commit 31a0eadbbf
4 changed files with 65 additions and 25 deletions

1
.npmrc Normal file

@ -0,0 +1 @@
registry=https://registry.npm.taobao.org/

@ -2,15 +2,16 @@
* Created by tdzl2003 on 2/22/16. * Created by tdzl2003 on 2/22/16.
*/ */
import * as path from 'path'; const path = require('path');
import { getRNVersion, translateOptions } from './utils'; import { getRNVersion, translateOptions } from './utils';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import { ZipFile } from 'yazl'; import { ZipFile } from 'yazl';
import { open as openZipFile } from 'yauzl'; import { open as openZipFile } from 'yauzl';
// import {diff} from 'node-bsdiff';
import { question } from './utils'; import { question } from './utils';
import { checkPlatform } from './app'; import { checkPlatform } from './app';
const { spawn } = require('child_process'); const { spawn, spawnSync } = require('child_process');
const g2js = require('gradle-to-js/lib/parser');
const os = require('os');
var diff; var diff;
try { try {
@ -19,12 +20,22 @@ try {
} catch (e) { } catch (e) {
diff = function() { diff = function() {
console.warn( console.warn(
'This function needs "node-bsdiff". Please run "npm i node-bsdiff -S" from your project directory first!', 'This function needs "node-bsdiff". Please run "npm i node-bsdiff" from your project directory first!',
); );
throw new Error('This function needs module "node-bsdiff". Please install it first.'); throw new Error('This function needs module "node-bsdiff". Please install it first.');
}; };
} }
function exec(command) {
const commandResult = spawnSync(command, {
shell: true,
stdio: 'inherit',
});
if (commandResult.error) {
throw commandResult.error;
}
}
async function runReactNativeBundleCommand( async function runReactNativeBundleCommand(
bundleName, bundleName,
development, development,
@ -45,8 +56,6 @@ async function runReactNativeBundleCommand(
fs.emptyDirSync(outputFolder); fs.emptyDirSync(outputFolder);
Array.prototype.push.apply(reactNativeBundleArgs, [ Array.prototype.push.apply(reactNativeBundleArgs, [
path.join('node_modules', 'react-native', 'local-cli', 'cli.js'),
'bundle',
'--assets-dest', '--assets-dest',
outputFolder, outputFolder,
'--bundle-output', '--bundle-output',
@ -67,27 +76,45 @@ async function runReactNativeBundleCommand(
reactNativeBundleArgs.push('--config', config); reactNativeBundleArgs.push('--config', config);
} }
console.log(`Running "react-native bundle" command:\n`); try {
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs); exec(`
console.log(`node ${reactNativeBundleArgs.join(' ')}`); echo Running "react-native bundle" command:
react-native bundle ${reactNativeBundleArgs.join(' ')}
`);
if (platform === 'android') {
await compileHermesByteCode(bundleName, outputFolder);
}
} catch (e) {
console.log(e);
process.exit(1);
}
}
return new Promise((resolve, reject, notify) => { function getHermesOSBin() {
reactNativeBundleProcess.stdout.on('data', data => { if (os.platform() === 'win32') return 'win64-bin';
console.log(data.toString().trim()); if (os.platform() === 'darwin') return 'osx-bin';
}); if (os.platform() === 'linux') return 'linux64-bin';
}
reactNativeBundleProcess.stderr.on('data', data => { async function compileHermesByteCode(bundleName, outputFolder) {
console.error(data.toString().trim()); let enableHermes = false;
}); try {
const gradleConfig = await g2js.parseFile('android/app/build.gradle');
reactNativeBundleProcess.on('close', exitCode => { const projectConfig = gradleConfig['project.ext.react'];
if (exitCode) { for (const packagerConfig of projectConfig) {
reject(new Error(`"react-native bundle" command exited with code ${exitCode}.`)); if (packagerConfig.includes('enableHermes') && packagerConfig.includes('true')) {
enableHermes = true;
break;
} }
}
resolve(null); } catch (e) {}
}); if (enableHermes) {
}); console.log(`Hermes enabled, now compiling to hermes bytecode:\n`);
exec(`
node_modules/hermesvm/${getHermesOSBin()}/hermes -emit-binary -out ${outputFolder}/${bundleName} ${outputFolder}/${bundleName} -O
echo Compiling done.
`);
}
} }
async function pack(dir, output) { async function pack(dir, output) {
@ -398,7 +425,6 @@ export const commands = {
// console.log('Bundling with React Native version: ', version); // console.log('Bundling with React Native version: ', version);
await runReactNativeBundleCommand(bundleName, dev, entryFile, intermediaDir, platform); await runReactNativeBundleCommand(bundleName, dev, entryFile, intermediaDir, platform);
await pack(path.resolve(intermediaDir), realOutput); await pack(path.resolve(intermediaDir), realOutput);

@ -32,6 +32,7 @@
"decompress-zip": "^0.3.1", "decompress-zip": "^0.3.1",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"glob": "^7.1.2", "glob": "^7.1.2",
"gradle-to-js": "^2.0.0",
"isomorphic-fetch": "^2.2.1", "isomorphic-fetch": "^2.2.1",
"node-apk-parser": "^0.2.3", "node-apk-parser": "^0.2.3",
"progress": "^1.1.8", "progress": "^1.1.8",

@ -1201,6 +1201,13 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "1.0.1" version "1.0.1"
resolved "http://registry.npm.taobao.org/graceful-readlink/download/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725" resolved "http://registry.npm.taobao.org/graceful-readlink/download/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
gradle-to-js@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/gradle-to-js/download/gradle-to-js-2.0.0.tgz#b790a97376d3d713105a086590e569610f7e6bc4"
integrity sha1-t5Cpc3bT1xMQWghlkOVpYQ9+a8Q=
dependencies:
lodash.merge "4.6.2"
har-validator@~2.0.6: har-validator@~2.0.6:
version "2.0.6" version "2.0.6"
resolved "http://registry.npm.taobao.org/har-validator/download/har-validator-2.0.6.tgz#cdcbc08188265ad119b6a5a7c8ab70eecfb5d27d" resolved "http://registry.npm.taobao.org/har-validator/download/har-validator-2.0.6.tgz#cdcbc08188265ad119b6a5a7c8ab70eecfb5d27d"
@ -1601,6 +1608,11 @@ lodash.keysin@^3.0.0:
lodash.isarguments "^3.0.0" lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0" lodash.isarray "^3.0.0"
lodash.merge@4.6.2:
version "4.6.2"
resolved "https://registry.npm.taobao.org/lodash.merge/download/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha1-VYqlO0O2YeGSWgr9+japoQhf5Xo=
lodash.pick@^3.1.0: lodash.pick@^3.1.0:
version "3.1.0" version "3.1.0"
resolved "http://registry.npm.taobao.org/lodash.pick/download/lodash.pick-3.1.0.tgz#f252a855b2046b61bcd3904b26f76bd2efc65550" resolved "http://registry.npm.taobao.org/lodash.pick/download/lodash.pick-3.1.0.tgz#f252a855b2046b61bcd3904b26f76bd2efc65550"