From ac21ef9f801e6e742f75f87496d7712dc90e2adf Mon Sep 17 00:00:00 2001 From: bryanprimus Date: Tue, 12 May 2026 22:13:27 +0800 Subject: [PATCH] feat: add Expo config plugin for MMKV log level --- README.md | 15 ++++ packages/react-native-mmkv/app.plugin.js | 87 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 packages/react-native-mmkv/app.plugin.js diff --git a/README.md b/README.md index 844a1ba7..5de985e6 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,21 @@ If a user chooses to disable LocalStorage in their browser, the library will aut ## Integrations +#### Expo + +If you use Expo Prebuild or EAS Build, configure the log level through the config plugin: + +```json +{ + "expo": { + "plugins": [ + ["react-native-mmkv", { "logLevel": 4 }] + ] + } +} +``` + + ### Rozenite Use [@rozenite/mmkv-plugin](https://www.rozenite.dev/docs/official-plugins/mmkv) to debug your MMKV storage using Rozenite. diff --git a/packages/react-native-mmkv/app.plugin.js b/packages/react-native-mmkv/app.plugin.js new file mode 100644 index 00000000..2be771c4 --- /dev/null +++ b/packages/react-native-mmkv/app.plugin.js @@ -0,0 +1,87 @@ +const { + createRunOncePlugin, + withGradleProperties, + withPodfile, +} = require('@expo/config-plugins') +const pkg = require('./package.json') + +const ANDROID_LOG_LEVEL_PROPERTY = 'MMKV_logLevel' +const IOS_LOG_LEVEL_VARIABLE = '$MMKVLogLevel' + +function normalizeLogLevel(logLevel) { + if (logLevel == null) { + return null + } + + if (typeof logLevel === 'string' && logLevel.trim() === '') { + throw new Error('react-native-mmkv: logLevel must be an integer between 0 and 4.') + } + + const value = Number(logLevel) + + if (!Number.isInteger(value) || value < 0 || value > 4) { + throw new Error('react-native-mmkv: logLevel must be an integer between 0 and 4.') + } + + return String(value) +} + +function setGradleProperty(properties, key, value) { + const existing = properties.find( + (item) => item.type === 'property' && item.key === key + ) + + if (existing != null) { + existing.value = value + } else { + properties.push({ + type: 'property', + key, + value, + }) + } + + return properties +} + +function setPodfileVariable(contents, value) { + const line = `${IOS_LOG_LEVEL_VARIABLE} = ${value}` + const pattern = /^\s*\$MMKVLogLevel\s*=.*$/m + + if (pattern.test(contents)) { + return contents.replace(pattern, line) + } + + return `${line}\n${contents}` +} + +function withMMKV(config, props = {}) { + const logLevel = normalizeLogLevel(props.logLevel) + + if (logLevel == null) { + return config + } + + config = withGradleProperties(config, (config) => { + config.modResults = setGradleProperty( + config.modResults, + ANDROID_LOG_LEVEL_PROPERTY, + logLevel + ) + + return config + }) + + config = withPodfile(config, (config) => { + config.modResults.contents = setPodfileVariable( + config.modResults.contents, + logLevel + ) + + return config + }) + + return config +} + +module.exports = createRunOncePlugin(withMMKV, pkg.name, pkg.version)