FilesLast commitLast update
1 month ago
1 month ago
1 month ago
1 month ago
README

BundleAssetRootSample (RN 0.86 / Harmony)

This copy targets 0.86 and lives at docs/Samples/BundleAssetRootSample in this repo. Synced from the same-named sample in rn084new with adapted dependencies (React 19.2.3 / RN 0.86.3); see the corresponding repos for earlier versions.

Demonstrates declaring an AssetRoot per bundle under multi-bundle dynamic loading, avoiding the global assetsDest / setBundlePath pollution.

Directory

  • RNProject/: the three JS packages (base / bizA / bizB) and their bundling scripts
    • RNProject/build/: Metro helper scripts (moduleId.js, preferProjectNodeModules.js). Do not delete.
  • NativeProject/: HarmonyOS host (this sample is C-API only; see the notes at the end for the ArkTS architecture)

Dependencies (local packages under RNProject/pkg/)

Side Package Path
JS @react-native-oh/react-native-harmony RNProject/pkg/react-native-oh-react-native-harmony-0.86.1.tgz
JS CLI @react-native-oh/react-native-harmony-cli RNProject/pkg/react-native-oh-react-native-harmony-cli-0.86.1.tgz
Native @rnoh/react-native-openharmony RNProject/node_modules/@react-native-oh/react-native-harmony/react_native_openharmony.har (the HAR is embedded in the framework tgz and referenced via node_modules after npm install)

Packages under pkg/ are not committed; obtain them from the matching release package (this sample uses 0.86.1). The HAR ships inside the framework tgz, so the native dependency resolves through node_modules — no separate HAR copy is needed. Outside the tester/harmony project, hvigor cannot attach a source directory to build-profile (Path escapes / OhmUrl failures), so the sample does not depend on framework source directories directly.

Build

The sample root is docs/Samples/BundleAssetRootSample in this repo. All commands below assume the repo root as the working directory.

First full build

# ---------- 1) JS: install deps and bundle the three packages into Native rawfile ----------
cd docs/Samples/BundleAssetRootSample/RNProject
npm install              # postinstall patches the harmony Image export (if still needed)
npm run dev:all          # i.e. dev:base → dev:legacy-base-assets → dev:bizA → dev:bizB (no framework-package JS involved)
# Equivalent to: dev:base → syncLegacyBaseAssets → stageSharedLogo+bizA → stageSharedLogo+bizB

# Note: the native har dependency points into RNProject/node_modules (produced by step 1's npm
# install); complete step 1 before this step.
# ---------- 2) Native: install ohpm dependencies (linked to the react_native_openharmony.har embedded there) ----------
cd ../NativeProject
ohpm install

# ---------- 3) Build / install to device (open NativeProject in DevEco Studio) ----------
#   Build                  build only
#   Run 'entry'            build, install and run (device required)

Incremental (sample business JS changed only)

cd docs/Samples/BundleAssetRootSample/RNProject
npm run dev:all
cd ../NativeProject
# Run 'entry' in DevEco Studio to repackage and install

When framework code changed

Dependencies are local tgz files under pkg/; after framework changes, rebuild the release package, replace the files under pkg/, and reinstall.

Changed area Suggested steps
packages/react-native-harmony (JS) only ① replace the tgz files under RNProject/pkg/ ② npm install ③ npm run dev:all ④ Run in DevEco Studio
packages/tester/harmony/react_native_openharmony (ArkTS/C++) ① rebuild the HAR and repackage (the release tgz embeds the HAR) ② replace the tgz under pkg/ ③ npm install + ohpm install ④ Run in DevEco Studio
Both JS and Native changed combine the two

Replacing a tgz with an unchanged version number: if the new tgz keeps the same version (content-only update, same file name), npm install skips re-extraction because the version is already satisfied, leaving the old code in node_modules (symptom: nothing changes after swapping the package). In that case, first delete the node_modules directory and the package-lock.json file under RNProject, then run npm install again (forces re-extraction of the tgz) followed by npm run dev:all (re-bundle).

If the native .har is updated along with the package (after replacing the har or reinstalling node_modules), likewise delete the oh_modules and entry/oh_modules directories and the oh-package-lock.json5 file under NativeProject, then run ohpm install again, and Run in DevEco Studio.

Native framework change: rebuild HAR → repackage → replace tgz

# 1) Build the HAR: run hvigorw's assembleHar task under packages/tester/harmony
#    (hvigorw.bat on Windows; or open tester/harmony in DevEco Studio and run
#      assembleHar for the react_native_openharmony module)
#    Output: react_native_openharmony/build/default/outputs/default/react_native_openharmony.har
#    The release package embeds this HAR into the framework tgz (the sample references it via node_modules)

# 2) Reinstall the ohpm deps: first delete the oh_modules and entry/oh_modules
#    directories and the oh-package-lock.json5 file under NativeProject, then run:
cd docs/Samples/BundleAssetRootSample/NativeProject
ohpm install
# then Run 'entry' in DevEco Studio

dev:all stages a shared assets/logo.png for BizA/BizB (same-relative-path isolation); Base only uses src/base/assets/logo.png, so that resource never enters basicModuleIds where the biz packages' processModuleFilter would filter it out (otherwise the sandbox copy fails and the UI only shows "Bundle diagnostics pending").

Scenarios and buttons

Button Meaning
Legacy base=rawfile without root; biz=sandbox without root (partner topology / pre-merge baseline)
All file all three in the sandbox and all with root
Raw root base=rawfile with root; biz=sandbox with root (recommended / acceptance path)

Image cache: every scenario start calls setImageCacheEnabled(false) so that real resolution failures under Legacy are not masked by the memory cache.

Below: Base / BizA / BizB[] switch the Surface.

Button Meaning
Hot BizA Overwrite the sandbox BizA same-path logo with BizB rawfile bytes → unmount Surface → cacheBust remount; 1) expect BizB pixels (URI carries ?v=)
Miss BizA Delete the sandbox BizA logo.png → same remount; 1) expect blank/failure, must not show BizB's same-name image; the blob should fail
Restore A Restore the BizA logo from rawfile → remount

All three require BizA to have been tapped once (registering AssetRootBizA).

JS probe panel (AssetProbeApp)

Each package's screen includes:

  1. main logo + the resolved URIs of this package's placeholder/loading images (must land under this package's assetRoot)
  2. defaultSource (aligned with rnoh_test): the Image mounts only after tapping "Load"; this package's placeholder must show before the network image's onLoad. The resolved URI must land under this package's assetRoot
  3. loadingIndicatorSource (aligned with rnoh_test): this package's indicator image shows during the download window; the control shows none
  4. Blob / fetch: fetch → blob() on this package's logo URI
  5. Local HTML: fetch(htmlUri); the WebView adapter must use the same URI / resolveAssetUri

Suggested manual path

  1. Raw root → check Base images and URIs → BizA → BizB → Base again (no URI/image cross-package bleed)
  2. Compare Legacy vs Raw root (the former has no assetRoot and biz easily fails; the latter is the main acceptance path)
  3. Hot update + cache-bust: Raw root → BizA → Hot BizA → 1) the image becomes BizB's and the URI contains ?v=; then Restore A restores it
  4. Negative missing image: BizA → Miss BizA → 1) blank/failure and blob fails; switching to BizB[] still shows BizB's image (proof of no bleed); Restore A recovers

Same-relative-path isolation

Both BizA and BizB require('../../assets/logo.png') (the same Metro relative path assets/logo.png), but build/moduleId.js keeps an independent moduleId table per business package (bizAModuleIds / bizBModuleIds) and mixes the package name into the hash. Each package therefore ends up with its own registerAsset entries bound to its own __rnohAssetRoot, avoiding "the first-loaded package owns the resource and the later one draws the wrong image".

Item Note
ArkTS / C-API dual architecture This sample enables enableCAPIArchitecture: true only; the ArkTS architecture needs a separate control project
System WebView component The sample verifies HTML readability with fetch; no Web component is embedded, avoiding a hard ArkWeb dependency
Metro provider In dev mode Metro does not declare an assetRoot

API docs

See docs/zh-cn/API接口说明.md in this repo: AssetRoot, the assetRoot parameter of FileJSBundleProvider / ResourceJSBundleProvider, and RNInstance.resolveAssetUri.