Created
February 10, 2025 07:54
-
-
Save kyo-ago/d454956019076a54b9f9fd64c49801ae to your computer and use it in GitHub Desktop.
react-native-compressed-jsbundle with expo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"eas-build-post-install": "node ./with-compressed-js-bundle.js" | |
}, | |
"devDependencies": { | |
"glob": "^11.0.1", | |
"react-native-compressed-jsbundle": "^0.1.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const { globSync } = require("glob"); | |
const addCompressScriptToBuildPhase = (projectFile) => { | |
let contents = fs.readFileSync(projectFile, "utf8"); | |
const newScriptLine = | |
"../node_modules/react-native-compressed-jsbundle/tool/compress-xcode.sh"; | |
if (contents.includes(newScriptLine)) { | |
return; | |
} | |
contents = contents.replace( | |
/(shellScript\s*=\s*".+?'\/scripts\/react-native-xcode\.sh'.+?)";[\r\n]/gims, | |
`$1\\n${newScriptLine}";\n`, | |
); | |
fs.writeFileSync(projectFile, contents, "utf8"); | |
}; | |
const isTargetProjectFile = (projectFile) => { | |
const contents = fs.readFileSync(projectFile, "utf8"); | |
const match = contents.match(/\/scripts\/react-native-xcode\.sh/g); | |
if (match?.length != 2) { | |
return false; | |
} | |
return true; | |
}; | |
const watchProjectFile = () => { | |
const projectFiles = globSync("./ios/**/project.pbxproj"); | |
const target = projectFiles | |
.filter((file) => !file.match(/ios\/Pods\//)) | |
.shift(); | |
if (!isTargetProjectFile(target)) { | |
throw new Error("not target project file"); | |
} | |
console.log("adding compress script to build phase"); | |
addCompressScriptToBuildPhase(target); | |
}; | |
const modifyAppDelegateFile = (appDelegateFile) => { | |
let contents = fs.readFileSync(appDelegateFile, "utf8"); | |
const importStatement = ` | |
#import <react-native-compressed-jsbundle/IMOCompressedBundleLoader.h> | |
`; | |
const methodImplementation = ` | |
- (void)loadSourceForBridge:(RCTBridge *)bridge onProgress:(RCTSourceLoadProgressBlock)onProgress onComplete:(RCTSourceLoadBlock)loadCallback { | |
[IMOCompressedBundleLoader loadSourceForBridge:bridge bridgeDelegate:self onProgress:onProgress onComplete:loadCallback]; | |
} | |
`; | |
if (contents.includes(importStatement)) { | |
return; | |
} | |
contents = contents | |
.replace(/#import "AppDelegate.h"/, `$&${importStatement}`) | |
.replace(/\@end/, `${methodImplementation}$&`); | |
fs.writeFileSync(appDelegateFile, contents, "utf8"); | |
}; | |
const watchAppDelegateFile = () => { | |
console.log("modifying app delegate files"); | |
const appDelegateFiles = globSync("./ios/**/AppDelegate.{m,mm}"); | |
if (!appDelegateFiles.length) { | |
throw new Error("no app delegate files"); | |
} | |
appDelegateFiles.forEach(modifyAppDelegateFile); | |
}; | |
if (process.env.EAS_BUILD_PLATFORM === "ios") { | |
watchAppDelegateFile(); | |
watchProjectFile(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment