Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expo): add an Expo config plugin to simplify enabling Next Storage implementation #1145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-eggs-grin.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, I think this can be removed, as it'll be released with version specified in package.json

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-native-async-storage/async-storage-expo-plugin": minor
---

Add an Expo config plugin to simplify enabling Next Storage on Android
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"workspaces": [
"packages/api",
"packages/default-storage",
"packages/default-storage-expo-plugin",
"packages/eslint-config",
"packages/sqlite-storage",
"packages/website"
Expand Down
1 change: 1 addition & 0 deletions packages/default-storage-expo-plugin/app.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./build");
1 change: 1 addition & 0 deletions packages/default-storage-expo-plugin/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@react-native-async-storage/eslint-config");
53 changes: 53 additions & 0 deletions packages/default-storage-expo-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@react-native-async-storage/async-storage-expo-plugin",
"version": "1.0.0",
"description": "Asynchronous, persistent, key-value storage system for React Native.",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
"build/",
"src/",
"app.plugin.js"
],
"author": "Two Doors Dev <[email protected]>",
"contributors": [
"Hassan Khan <[email protected]> (https://github.com/hassankhan)",
"Bill Spooner <[email protected]> (https://github.com/sandyklark)"
],
"homepage": "https://github.com/react-native-async-storage/async-storage#readme",
"license": "MIT",
"keywords": [
"react-native",
"react native",
"async storage",
"asyncstorage",
"storage",
"expo",
"expo config plugin"
],
"repository": {
"type": "git",
"url": "https://github.com/react-native-async-storage/async-storage.git",
"directory": "packages/default-storage-expo-plugin"
},
"scripts": {
"prepack": "yarn build",
"prepare": "expo-module prepare",
"prepublishOnly": "expo-module prepublishOnly",
"build": "EXPO_NONINTERACTIVE=true expo-module build",
"clean": "expo-module clean",
"lint": "expo-module lint",
"test:ts": "yarn build --noEmit",
"test:lint": "yarn lint"
},
"devDependencies": {
"@expo/config-plugins": "^8.0.8",
"eslint": "^8.54.0",
"expo-module-scripts": "^3.5.2",
"prettier": "^2.8.8"
},
"peerDependencies": {
"expo": "^51"
},
"upstreamPackage": "@react-native-async-storage/async-storage"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ConfigPlugin} from "@expo/config-plugins";
import { withGradleProperties } from "@expo/config-plugins";

import type { AsyncStorageOptions } from "../types";

export const withNextStorage: ConfigPlugin<AsyncStorageOptions> = (
config,
props
) => {
const useNextStorage = props?.android?.nextStorage?.enabled ?? false;

if (!useNextStorage) {
return config;
}

return withGradleProperties(config, (config) => {
config.modResults.push({
key: "AsyncStorage_useNextStorage",
value: "true",
type: "property",
});

return config;
});
};
20 changes: 20 additions & 0 deletions packages/default-storage-expo-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ConfigPlugin } from "@expo/config-plugins";

import { withNextStorage } from "./android/withNextStorage";
import type { AsyncStorageOptions } from "./types";

const DEFAULT_OPTS: AsyncStorageOptions = {
android: {
nextStorage: {
enabled: false,
},
},
};

const withAsyncStorage: ConfigPlugin<AsyncStorageOptions> = (config, props) => {
const normalizedProps = { ...DEFAULT_OPTS, ...props };
config = withNextStorage(config, normalizedProps);
return config;
};

export default withAsyncStorage;
7 changes: 7 additions & 0 deletions packages/default-storage-expo-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type AsyncStorageOptions = {
android: {
nextStorage?: {
enabled: boolean;
};
};
};
8 changes: 8 additions & 0 deletions packages/default-storage-expo-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "expo-module-scripts/tsconfig.plugin",
"compilerOptions": {
"outDir": "build",
"rootDir": "src"
},
"include": ["./src"]
}
45 changes: 45 additions & 0 deletions packages/website/docs/advanced/Next.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,51 @@ If you want to use different KSP version, you can set a property in `gradle.prop
AsyncStorage_next_kspVersion=1.9.24-1.0.20
```

### Configuration with Expo

If you are using Expo, you can configure the feature using [config plugins](https://docs.expo.dev/guides/config-plugins/).

First, install the required plugins:

```bash
expo install expo-build-properties @react-native-async-storage/async-storage-expo-plugin
```

Next, in your Expo project's `app.json`, add the plugins:

```json
{
"expo": {
"plugins": [
[
"expo-build-properties",
{
"android": {
"kotlinVersion": "1.9.23"
}
}
],
[
"@react-native-async-storage/async-storage-expo-plugin",
{
"android": {
"nextStorage": {
"enabled": true
}
}
}
]
]
}
}
```

Finally, use the Expo prebuild command to re-generate the Android project:

```bash
expo prebuild --platform android --clean
```

### Notable changes

Alongside of a warning regarding `key`/`value`, errors are thrown when:
Expand Down
Loading