Skip to content

Commit d2c10da

Browse files
geraintwhitefacebook-github-bot
authored andcommitted
Add bundleForVariant option (#32472)
Summary: Ref #30606 (comment) ## Changelog [Android] [Added] - Add bundleForVariant option Pull Request resolved: #32472 Test Plan: I added the following log into react.gradle and ran the Android build for my app: ``` println("bundleEnabled: ${targetName}, ${bundleForVariant(variant)}") ``` ``` # build.gradle project.ext.react = [ entryFile: "index.android.js", enableHermes: true, // clean and rebuild if changing bundleForVariant: { def variant -> variant.name.toLowerCase().contains('release') || variant.name.toLowerCase().contains('live') }, ] ... flavorDimensions 'branding' productFlavors { cve { dimension 'branding' } whce { dimension 'branding' } } ``` Console output: ``` Reading env from: env/cve/live bundleEnabled: CveDebug, false bundleEnabled: CveRelease, true bundleEnabled: CveLive, true bundleEnabled: WhceDebug, false bundleEnabled: WhceRelease, true bundleEnabled: WhceLive, true ``` Reviewed By: cortinico, ryancat Differential Revision: D31910406 Pulled By: ShikaSD fbshipit-source-id: baca5efaddedddad15d974cc7bb8f3c2a4c4f35b
1 parent d1439e8 commit d2c10da

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt

+11
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ abstract class ReactExtension @Inject constructor(project: Project) {
130130
val bundleIn: MapProperty<String, Boolean> =
131131
objects.mapProperty(String::class.java, Boolean::class.java).convention(emptyMap())
132132

133+
/**
134+
* Functional interface to toggle the bundle command only on specific [BaseVariant] Default: will
135+
* check [bundleIn] or return True for Release variants and False for Debug variants.
136+
*/
137+
var bundleForVariant: (BaseVariant) -> Boolean = { variant ->
138+
if (bundleIn.getting(variant.name).isPresent) bundleIn.getting(variant.name).get()
139+
else if (bundleIn.getting(variant.buildType.name).isPresent)
140+
bundleIn.getting(variant.buildType.name).get()
141+
else variant.isRelease
142+
}
143+
133144
/** Hermes Config */
134145

135146
/** The command to use to invoke hermes. Default is `hermesc` for the correct OS. */

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt

+1-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ private const val REACT_GROUP = "react"
2828
@Suppress("SpreadOperator")
2929
internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExtension) {
3030
val targetName = variant.name.capitalize(Locale.ROOT)
31-
val isRelease = variant.isRelease
3231
val targetPath = variant.dirName
3332

3433
// React js bundle directories
@@ -50,7 +49,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
5049
val execCommand = nodeExecutableAndArgs + cliPath
5150
val enableHermes = config.enableHermesForVariant(variant)
5251
val cleanup = config.deleteDebugFilesForVariant(variant)
53-
val bundleEnabled = variant.checkBundleEnabled(config)
52+
val bundleEnabled = config.bundleForVariant(variant)
5453

5554
val bundleTask =
5655
tasks.register("createBundle${targetName}JsAndAssets", BundleJsAndAssetsTask::class.java) {
@@ -258,17 +257,5 @@ private fun Project.cleanupVMFiles(
258257
}
259258
}
260259

261-
private fun BaseVariant.checkBundleEnabled(config: ReactExtension): Boolean {
262-
if (config.bundleIn.getting(name).isPresent) {
263-
return config.bundleIn.getting(name).get()
264-
}
265-
266-
if (config.bundleIn.getting(buildType.name).isPresent) {
267-
return config.bundleIn.getting(buildType.name).get()
268-
}
269-
270-
return isRelease
271-
}
272-
273260
internal val BaseVariant.isRelease: Boolean
274261
get() = name.toLowerCase(Locale.ROOT).contains("release")

react.gradle

+13-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,18 @@ def hermesFlagsForVariant = config.hermesFlagsForVariant ?: {
102102
// Set disableDevForVariant to a function to configure per variant,
103103
// defaults to `devDisabledIn${targetName}` or True for Release variants and False for debug variants
104104
def disableDevForVariant = config.disableDevForVariant ?: {
105-
def variant -> config."devDisabledIn${variant.name.capitalize()}" || variant.name.toLowerCase().contains("release")
105+
def variant ->
106+
config."devDisabledIn${variant.name.capitalize()}" ||
107+
variant.name.toLowerCase().contains("release")
108+
}
109+
110+
// Set bundleForVariant to a function to configure per variant,
111+
// defaults to `bundleIn${targetName}` or True for Release variants and False for debug variants
112+
def bundleForVariant = config.bundleForVariant ?: {
113+
def variant ->
114+
config."bundleIn${variant.name.capitalize()}" ||
115+
config."bundleIn${variant.buildType.name.capitalize()}" ||
116+
variant.name.toLowerCase().contains("release")
106117
}
107118

108119
// Set deleteDebugFilesForVariant to a function to configure per variant,
@@ -238,11 +249,7 @@ afterEvaluate {
238249
}
239250
}
240251

241-
enabled config."bundleIn${targetName}" != null
242-
? config."bundleIn${targetName}"
243-
: config."bundleIn${variant.buildType.name.capitalize()}" != null
244-
? config."bundleIn${variant.buildType.name.capitalize()}"
245-
: targetName.toLowerCase().contains("release")
252+
enabled bundleForVariant(variant)
246253
}
247254

248255
// Expose a minimal interface on the application variant and the task itself:

0 commit comments

Comments
 (0)