diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..efa0fc0 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npm.taobao.org/ \ No newline at end of file diff --git a/local-cli/src/bundle.js b/local-cli/src/bundle.js index d6efafe..a9619cf 100644 --- a/local-cli/src/bundle.js +++ b/local-cli/src/bundle.js @@ -2,15 +2,16 @@ * Created by tdzl2003 on 2/22/16. */ -import * as path from 'path'; +const path = require('path'); import { getRNVersion, translateOptions } from './utils'; import * as fs from 'fs-extra'; import { ZipFile } from 'yazl'; import { open as openZipFile } from 'yauzl'; -// import {diff} from 'node-bsdiff'; import { question } from './utils'; 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; try { @@ -19,12 +20,22 @@ try { } catch (e) { diff = function() { 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.'); }; } +function exec(command) { + const commandResult = spawnSync(command, { + shell: true, + stdio: 'inherit', + }); + if (commandResult.error) { + throw commandResult.error; + } +} + async function runReactNativeBundleCommand( bundleName, development, @@ -45,8 +56,6 @@ async function runReactNativeBundleCommand( fs.emptyDirSync(outputFolder); Array.prototype.push.apply(reactNativeBundleArgs, [ - path.join('node_modules', 'react-native', 'local-cli', 'cli.js'), - 'bundle', '--assets-dest', outputFolder, '--bundle-output', @@ -67,27 +76,45 @@ async function runReactNativeBundleCommand( reactNativeBundleArgs.push('--config', config); } - console.log(`Running "react-native bundle" command:\n`); - const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs); - console.log(`node ${reactNativeBundleArgs.join(' ')}`); + try { + exec(` + 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) => { - reactNativeBundleProcess.stdout.on('data', data => { - console.log(data.toString().trim()); - }); +function getHermesOSBin() { + if (os.platform() === 'win32') return 'win64-bin'; + if (os.platform() === 'darwin') return 'osx-bin'; + if (os.platform() === 'linux') return 'linux64-bin'; +} - reactNativeBundleProcess.stderr.on('data', data => { - console.error(data.toString().trim()); - }); - - reactNativeBundleProcess.on('close', exitCode => { - if (exitCode) { - reject(new Error(`"react-native bundle" command exited with code ${exitCode}.`)); +async function compileHermesByteCode(bundleName, outputFolder) { + let enableHermes = false; + try { + const gradleConfig = await g2js.parseFile('android/app/build.gradle'); + const projectConfig = gradleConfig['project.ext.react']; + for (const packagerConfig of projectConfig) { + 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) { @@ -398,7 +425,6 @@ export const commands = { // console.log('Bundling with React Native version: ', version); - await runReactNativeBundleCommand(bundleName, dev, entryFile, intermediaDir, platform); await pack(path.resolve(intermediaDir), realOutput); diff --git a/package.json b/package.json index 5adb09b..50336e3 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "decompress-zip": "^0.3.1", "fs-extra": "^8.1.0", "glob": "^7.1.2", + "gradle-to-js": "^2.0.0", "isomorphic-fetch": "^2.2.1", "node-apk-parser": "^0.2.3", "progress": "^1.1.8", diff --git a/yarn.lock b/yarn.lock index ed91591..c54b049 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1201,6 +1201,13 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0: version "1.0.1" 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: version "2.0.6" 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.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: version "3.1.0" resolved "http://registry.npm.taobao.org/lodash.pick/download/lodash.pick-3.1.0.tgz#f252a855b2046b61bcd3904b26f76bd2efc65550"