@@ -46,11 +46,18 @@ namespace
4646 winrt::hstring const kUseWebDebugger = L" useWebDebugger" ;
4747#endif // USE_WEB_DEBUGGER
4848
49- std::optional<winrt::hstring> GetBundleName (std::optional<winrt::hstring> const &bundleRoot)
49+ std::filesystem::path GetBundleRootPath ()
50+ {
51+ WCHAR modulePath[MAX_PATH];
52+ GetModuleFileNameW (NULL , modulePath, MAX_PATH);
53+ return std::filesystem::path{modulePath}.replace_filename (L" Bundle" ) / L" " ;
54+ }
55+
56+ std::optional<winrt::hstring> GetBundleName (std::filesystem::path bundlePath,
57+ std::optional<winrt::hstring> const &bundleRoot)
5058 {
5159 constexpr std::wstring_view const bundleExtension = L" .bundle" ;
5260
53- std::filesystem::path bundlePath{L" Bundle\\ " };
5461 if (bundleRoot.has_value ()) {
5562 std::wstring_view root = bundleRoot.value ();
5663 for (auto &&ext : {L" .windows" , L" .native" , L" " }) {
@@ -104,74 +111,31 @@ std::vector<std::wstring_view> const ReactTestApp::JSBundleNames = {
104111 L" main" ,
105112};
106113
107- ReactInstance::ReactInstance ()
108- {
109- reactNativeHost_.PackageProviders ().Append (winrt::make<ReactPackageProvider>());
110- winrt::Microsoft::ReactNative::RegisterAutolinkedNativeModulePackages (
111- reactNativeHost_.PackageProviders ());
112-
113- reactNativeHost_.InstanceSettings ().InstanceLoaded (
114- [this ](winrt::IInspectable const & /* sender*/ , winrt::InstanceLoadedEventArgs const &args) {
115- context_ = args.Context ();
116-
117- #if __has_include("AppRegistry.h") && __has_include(<JSI/JsiApiContext.h>)
118- if (!onComponentsRegistered_) {
119- return ;
120- }
121-
122- winrt::Microsoft::ReactNative::ExecuteJsi (context_, [this ](Runtime &runtime) noexcept {
123- try {
124- onComponentsRegistered_ (ReactTestApp::GetAppKeys (runtime));
125- } catch ([[maybe_unused]] std::exception const &e) {
126- #if defined(_DEBUG) && !defined(DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION)
127- if (IsDebuggerPresent ()) {
128- __debugbreak ();
129- }
130- #endif // defined(_DEBUG) && !defined(DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION)
131- }
132- });
133- #endif // __has_include("AppRegistry.h") && __has_include(<JSI/JsiApiContext.h>)
134- });
135- }
136-
137- #if __has_include(<winrt/Microsoft.UI.Composition.h>)
138- ReactInstance::ReactInstance (HWND hwnd,
139- winrt::Microsoft::UI::Composition::Compositor const &compositor)
140- : ReactInstance()
141- {
142- winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId (
143- reactNativeHost_.InstanceSettings ().Properties (), reinterpret_cast <uint64_t >(hwnd));
144-
145- // By using the MicrosoftCompositionContextHelper here, React Native Windows
146- // will use Lifted Visuals for its tree.
147- winrt::Microsoft::ReactNative::Composition::CompositionUIService::SetCompositor (
148- reactNativeHost_.InstanceSettings (), compositor);
149- }
150- #endif // __has_include(<winrt/Microsoft.UI.Composition.h>)
151-
152- bool ReactInstance::LoadJSBundleFrom (JSBundleSource source)
114+ bool ReactInstance::LoadJSBundleFrom (JSBundleSource source, bool reloadHost)
153115{
154116 source_ = source;
155117
156118 auto instanceSettings = reactNativeHost_.InstanceSettings ();
157119 switch (source) {
158120 case JSBundleSource::DevServer:
159- instanceSettings.JavaScriptBundleFile (L" index" );
121+ instanceSettings.DebugBundlePath (L" index" );
160122 break ;
161123 case JSBundleSource::Embedded:
162- auto const &bundleName = GetBundleName (bundleRoot_);
124+ auto const bundleRootPath = GetBundleRootPath ();
125+ instanceSettings.BundleRootPath (L" file://" + bundleRootPath.wstring ());
126+ auto const &bundleName = GetBundleName (bundleRootPath, bundleRoot_);
163127 if (!bundleName.has_value ()) {
164128 return false ;
165129 }
166130 instanceSettings.JavaScriptBundleFile (bundleName.value ());
167131 break ;
168132 }
169133
170- Reload ();
134+ Reload (reloadHost );
171135 return true ;
172136}
173137
174- void ReactInstance::Reload ()
138+ void ReactInstance::Reload (bool reloadHost )
175139{
176140 auto instanceSettings = reactNativeHost_.InstanceSettings ();
177141
@@ -196,7 +160,9 @@ void ReactInstance::Reload()
196160 instanceSettings.SourceBundleHost (host);
197161 instanceSettings.SourceBundlePort (static_cast <uint16_t >(port));
198162
199- reactNativeHost_.ReloadInstance ();
163+ if (reloadHost) {
164+ reactNativeHost_.ReloadInstance ();
165+ }
200166}
201167
202168bool ReactInstance::BreakOnFirstLine () const
@@ -297,6 +263,35 @@ void ReactInstance::UseWebDebugger(bool useWebDebugger)
297263#endif // USE_WEB_DEBUGGER
298264}
299265
266+ void ReactInstance::InitializeHost (winrt::Microsoft::ReactNative::ReactNativeHost host)
267+ {
268+ host.PackageProviders ().Append (winrt::make<ReactPackageProvider>());
269+ winrt::Microsoft::ReactNative::RegisterAutolinkedNativeModulePackages (host.PackageProviders ());
270+
271+ host.InstanceSettings ().InstanceLoaded (
272+ [this ](winrt::IInspectable const & /* sender*/ , winrt::InstanceLoadedEventArgs const &args) {
273+ context_ = args.Context ();
274+
275+ #if __has_include("AppRegistry.h") && __has_include(<JSI/JsiApiContext.h>)
276+ if (!onComponentsRegistered_) {
277+ return ;
278+ }
279+
280+ winrt::Microsoft::ReactNative::ExecuteJsi (context_, [this ](Runtime &runtime) noexcept {
281+ try {
282+ onComponentsRegistered_ (ReactTestApp::GetAppKeys (runtime));
283+ } catch ([[maybe_unused]] std::exception const &e) {
284+ #if defined(_DEBUG) && !defined(DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION)
285+ if (IsDebuggerPresent ()) {
286+ __debugbreak ();
287+ }
288+ #endif // defined(_DEBUG) && !defined(DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION)
289+ }
290+ });
291+ #endif // __has_include("AppRegistry.h") && __has_include(<JSI/JsiApiContext.h>)
292+ });
293+ }
294+
300295winrt::IAsyncOperation<bool > ReactTestApp::IsDevServerRunning ()
301296{
302297 winrt::Uri uri (L" http://localhost:8081/status" );
0 commit comments