Created
February 15, 2025 18:15
-
-
Save mrousavy/d8d15eb1b8cffe60765f7fa7d67d6767 to your computer and use it in GitHub Desktop.
Expo Config Plugin to disable JS bundle compression for faster app startup
This file contains 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 {withAppBuildGradle} = require('@expo/config-plugins') | |
/** | |
* A Config Plugin to disable bundle compression in Android build.gradle. | |
* This makes the Android app start faster - in our tests by 400ms! | |
* @param {import('@expo/config-plugins').ConfigPlugin} config | |
* @returns {import('@expo/config-plugins').ConfigPlugin} | |
*/ | |
module.exports = function withNoBundleCompression(config) { | |
return withAppBuildGradle(config, androidConfig => { | |
let buildGradle = androidConfig.modResults.contents | |
const hasAndroidResources = buildGradle.includes('androidResources {') | |
const hasNoCompress = buildGradle.includes('noCompress') | |
if (hasAndroidResources) { | |
if (hasNoCompress) { | |
if ( | |
buildGradle.includes('noCompress += ["bundle"]') || | |
buildGradle.includes("noCompress += 'bundle'") || | |
buildGradle.includes('noCompress += "bundle"') | |
) { | |
return androidConfig | |
} | |
const lines = buildGradle.split('\n') | |
const modifiedLines = lines.map(line => { | |
if (line.trim().startsWith('noCompress')) { | |
if (line.includes('+=')) { | |
return line.replace(/\]/, ', "bundle"]') | |
} else if (line.includes('=')) { | |
return line.replace('=', '+= ["bundle",') + ']' | |
} | |
} | |
return line | |
}) | |
androidConfig.modResults.contents = modifiedLines.join('\n') | |
} else { | |
const androidResources = buildGradle.indexOf('androidResources {') | |
if (androidResources === -1) { | |
throw new Error( | |
`Cannot find androidResources { block in build.gradle!`, | |
) | |
} | |
const insertPosition = buildGradle.indexOf('\n', androidResources) + 1 | |
const newContent = | |
buildGradle.slice(0, insertPosition) + | |
' noCompress += ["bundle"]\n' + | |
buildGradle.slice(insertPosition) | |
androidConfig.modResults.contents = newContent | |
} | |
} else { | |
const androidBlock = buildGradle.indexOf('android {') | |
if (androidBlock === -1) { | |
throw new Error(`Cannot find android { block in build.gradle!`) | |
} | |
const insertPosition = buildGradle.indexOf('\n', androidBlock) + 1 | |
const newContent = | |
buildGradle.slice(0, insertPosition) + | |
' androidResources {\n' + | |
' noCompress += ["bundle"]\n' + | |
' }\n' + | |
buildGradle.slice(insertPosition) | |
androidConfig.modResults.contents = newContent | |
} | |
return androidConfig | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is originally based off of https://gist.github.com/theprashant-one/2caa96dfcb5e9958a7d9058c0d10c94e.