Skip to content

Commit 18ef207

Browse files
author
theGrep01
committed
feat: re-works flow
1 parent 44dd5af commit 18ef207

File tree

1 file changed

+186
-25
lines changed

1 file changed

+186
-25
lines changed

components/zephyr/repack/start-from-scratch.mdx

Lines changed: 186 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -550,44 +550,205 @@ Follow these steps to configure and run the HostApp in release mode with Zephyr:
550550
551551
Configure the Zephyr environment:
552552
553-
1. Configure the Zephyr environment:
554-
555-
- In the Zephyr Cloud dashboard, create or select the target environment (for example, production or staging).
553+
1. Change your configs to divide debug builds and release builds:
554+
555+
- In the HostApp and MiniApp, change the `rspack.config.mjs` file to add a `ZC` variable:
556+
557+
```js title="HostApp/rspack.config.mjs"{9,17,72}
558+
import path from 'node:path';
559+
import {fileURLToPath} from 'node:url';
560+
import * as Repack from '@callstack/repack';
561+
import {withZephyr} from 'zephyr-repack-plugin';
562+
const __filename = fileURLToPath(import.meta.url);
563+
const __dirname = path.dirname(__filename);
564+
565+
566+
const USE_ZEPHYR = Boolean(process.env.ZC);
567+
/**
568+
* Rspack configuration enhanced with Re.Pack defaults for React Native.
569+
*
570+
* Learn about Rspack configuration: https://rspack.dev/config/
571+
* Learn about Re.Pack configuration: https://re-pack.dev/docs/guides/configuration
572+
*/
573+
574+
const config = env => {
575+
const { platform, mode } = env
576+
return {
577+
context: __dirname,
578+
entry: './index.js',
579+
resolve: {
580+
// 1. Understand the file path of ios and android file extensions
581+
// 2. Configure the output to be as close to Metro as possible
582+
...Repack.getResolveOptions(),
583+
},
584+
output: {
585+
// Unsure - for module federation HMR and runtime?
586+
uniqueName: 'react-native-host-app',
587+
},
588+
module: {
589+
rules: [
590+
...Repack.getJsTransformRules(),
591+
...Repack.getAssetTransformRules(),
592+
],
593+
},
594+
plugins: [
595+
new Repack.RepackPlugin({
596+
platform,
597+
}),
598+
new Repack.plugins.ModuleFederationPluginV2({
599+
name: 'HostApp',
600+
filename: 'HostApp.container.js.bundle',
601+
dts: false,
602+
remotes: {
603+
MiniApp: `MiniApp@http://localhost:9001/${platform}/MiniApp.container.js.bundle`,
604+
},
605+
shared: {
606+
react: {
607+
singleton: true,
608+
version: '19.0.0',
609+
eager: true,
610+
},
611+
'react-native': {
612+
singleton: true,
613+
version: '0.78.0',
614+
eager: true,
615+
}
616+
},
617+
}),
618+
// Supports for new architecture - Hermes can also use JS, it's not a requirement, it will still work the same but it's for performance optimization
619+
new Repack.plugins.HermesBytecodePlugin({
620+
enabled: mode === 'production',
621+
test: /\.(js)?bundle$/,
622+
exclude: /index.bundle$/,
623+
}),
624+
],
625+
}
626+
};
556627

557-
2. Retrieve the remote URLs:
628+
export default USE_ZEPHYR ? withZephyr()(config) : config;
629+
```
558630
559-
- After the environment is configured, open its details page in the Zephyr dashboard for each platform. Zephyr will display the hosted URLs for the HostApp and each miniapp.
631+
```js title="MiniApp/rspack.config.mjs"{9,19,68}
632+
import path from 'node:path';
633+
import {fileURLToPath} from 'node:url';
634+
import * as Repack from '@callstack/repack';
635+
import {withZephyr} from 'zephyr-repack-plugin';
636+
637+
const __filename = fileURLToPath(import.meta.url);
638+
const __dirname = path.dirname(__filename);
639+
640+
const USE_ZEPHYR = Boolean(process.env.ZC);
641+
const STANDALONE = Boolean(process.env.STANDALONE);
642+
643+
/**
644+
* Rspack configuration enhanced with Re.Pack defaults for React Native.
645+
*
646+
* Learn about Rspack configuration: https://rspack.dev/config/
647+
* Learn about Re.Pack configuration: https://re-pack.dev/docs/guides/configuration
648+
*/
649+
650+
const config = env => {
651+
const {platform, mode} = env;
652+
return {
653+
mode,
654+
context: __dirname,
655+
entry: './index.js',
656+
resolve: {
657+
...Repack.getResolveOptions(),
658+
},
659+
output: {
660+
uniqueName: 'react-native-mini-app',
661+
},
662+
module: {
663+
rules: [
664+
...Repack.getJsTransformRules(),
665+
...Repack.getAssetTransformRules({inline: true}),
666+
],
667+
},
668+
plugins: [
669+
new Repack.RepackPlugin(),
670+
new Repack.plugins.ModuleFederationPluginV2({
671+
name: 'MiniApp',
672+
filename: 'MiniApp.container.js.bundle',
673+
dts: false,
674+
exposes: {
675+
'./App': './App.tsx',
676+
},
677+
shared: {
678+
react: {
679+
singleton: true,
680+
version: '19.0.0',
681+
eager: STANDALONE,
682+
},
683+
'react-native': {
684+
singleton: true,
685+
version: '0.78.0',
686+
eager: STANDALONE,
687+
},
688+
},
689+
}),
690+
new Repack.plugins.HermesBytecodePlugin({
691+
enabled: mode === 'production',
692+
test: /\.(js)?bundle$/,
693+
exclude: /index.bundle$/,
694+
}),
695+
],
696+
};
697+
};
698+
699+
export default USE_ZEPHYR ? withZephyr()(config) : config;
700+
701+
```
702+
703+
- `ZC` is used to indicate that the bundles are for Zephyr Cloud.
704+
705+
2. Add bundle scripts to the HostApp and MiniApp:
706+
707+
- In the HostApp and MiniApp, add the following scripts to the package.json files:
708+
709+
```json title="HostApp/package.json"
710+
"scripts": {
711+
"bundle": "pnpm run bundle:ios && pnpm run bundle:android",
712+
"bundle:ios": "react-native bundle --platform ios --dev false --entry-file index.js",
713+
"bundle:android": "react-native bundle --platform android --dev false --entry-file index.js"
714+
}
715+
```
560716
561-
- Copy the provided HostApp URL. It usually follows this pattern:
562-
`https://{platform}-{appname}-{yourusername}-ze.zephyrcloud.app/`
717+
```json title="MiniApp/package.json"
718+
"scripts": {
719+
"bundle": "pnpm run bundle:ios && pnpm run bundle:android",
720+
"bundle:ios": "react-native bundle --platform ios --dev false --entry-file index.js",
721+
"bundle:android": "react-native bundle --platform android --dev false --entry-file index.js"
722+
}
723+
```
563724
564-
3. Update the HostApp configuration:
725+
2. Bundle HostApp and MiniApps, and upload to Zephyr Cloud:
565726
566-
- In your HostApp project directory, open the HostApp/rspack.config.mjs file. Locate the remotes object, which defines how to load each miniapp remotely.
727+
- Bundle the MiniApp:
567728
568-
- Replace each remote’s localhost URL with the Zephyr Cloud URL. For example:
729+
```bash
730+
ZC=1 pnpm --filter MiniApp bundle
731+
```
732+
733+
- Bundle the HostApp:
569734
570-
```js title="HostApp/rspack.config.mjs"
571-
const remotes = {
572-
MiniApp: `MiniApp@https://android-miniapp-yourusername-ze.zephyrcloud.app/MiniApp.container.js.bundle`,
573-
};
735+
```bash
736+
ZC=1 pnpm --filter HostApp bundle
574737
```
575738
576-
- In the example above, replace MiniApp with your actual miniapp name (used both as the object key and inside the URL path), and replace yourusername with your Zephyr username/organization. The ${platform} placeholder will be replaced by Rspack with android or ios at build time. Ensure the URL has a trailing slash before ${platform} as shown.
739+
- Command run with `ZC=1` will upload the bundles to Zephyr Cloud.
577740
578-
- If you have multiple miniapps, add each one to the remotes object in the same way, using its own name and Zephyr URL. For example:
579741
580-
```js title="HostApp/rspack.config.mjs"
581-
const remotes = {
582-
MiniApp1: `MiniApp1@https://ios-miniapp1-yourusername-ze.zephyrcloud.app/MiniApp1.container.js.bundle`,
583-
MiniApp2: `MiniApp2@https://ios-miniapp2-yourusername-ze.zephyrcloud.app/MiniApp2.container.js.bundle`,
584-
// add more miniapps as needed
585-
};
586-
```
742+
3. Build and run the HostApp in release mode:
743+
744+
- For Android, you can use the following command from the HostApp android directory:
587745
588-
4. Build and run the HostApp in release mode:
746+
```bash
747+
ZC=1 ./gradlew assembleRelease
748+
```
589749
590-
- With the configuration updated, build and launch the HostApp using your normal release process.
750+
{/* TODO: set ZC for iOS */}
751+
{/* - For iOS, you can use the following command: */}
591752
592753
- The HostApp will now load each miniapp bundle from the specified Zephyr Cloud URLs instead of localhost. Verify that the app launches correctly and that each miniapp is fetched successfully from the remote URL.
593754

0 commit comments

Comments
 (0)