From 9ef64ef30a1f84bc73fa333dedca4b7d6d981b93 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 00:25:39 +0100 Subject: [PATCH 1/9] fix: forward early JS errors to ErrorUtils --- .../repack/src/modules/ScriptManager/types.ts | 16 ++++++++++++++++ .../RepackTargetPlugin/implementation/init.ts | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/repack/src/modules/ScriptManager/types.ts b/packages/repack/src/modules/ScriptManager/types.ts index 6293355cb..273519721 100644 --- a/packages/repack/src/modules/ScriptManager/types.ts +++ b/packages/repack/src/modules/ScriptManager/types.ts @@ -1,4 +1,20 @@ +type ModuleExports = Record; + export interface WebpackContext { + i: ((options: { + id: number; + factory: ( + moduleObject: Record, + moduleExports: ModuleExports, + webpackRequire: WebpackContext + ) => void; + module: { + id: number; + loaded: boolean; + exports: Record; + }; + require: WebpackContext; + }) => void)[]; p: () => string; u: (id: string) => string; } diff --git a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts index 01014fa89..39fbdd01c 100644 --- a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts +++ b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts @@ -13,6 +13,18 @@ module.exports = function () { __webpack_require__.repack = $globalObject$.__repack__ = repackRuntime; + // intercept module factory calls to forward errors to global.ErrorUtils + __webpack_require__.i.push(function (options) { + var originalFactory = options.factory; + options.factory = function (moduleObject, moduleExports, webpackRequire) { + try { + originalFactory.call(this, moduleObject, moduleExports, webpackRequire); + } catch (e) { + $globalObject$.ErrorUtils.reportFatalError(e); + } + }; + }); + function loadScript( name: string, caller: string | undefined, From 4b7edc695ef7a1613f71ef59e927955a7227e9d0 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 12:44:19 +0100 Subject: [PATCH 2/9] feat: disable strict error handling from webpack/rspack --- .../src/plugins/RepackTargetPlugin/RepackTargetPlugin.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/repack/src/plugins/RepackTargetPlugin/RepackTargetPlugin.ts b/packages/repack/src/plugins/RepackTargetPlugin/RepackTargetPlugin.ts index 148ee395b..7758ec414 100644 --- a/packages/repack/src/plugins/RepackTargetPlugin/RepackTargetPlugin.ts +++ b/packages/repack/src/plugins/RepackTargetPlugin/RepackTargetPlugin.ts @@ -61,6 +61,11 @@ export class RepackTargetPlugin implements RspackPluginInstance { compiler.options.output.chunkFormat = 'array-push'; compiler.options.output.globalObject = globalObject; + // Disable built-in strict module error handling + // this is handled through an interceptor in the + // init module added to __webpack_require__.i array + compiler.options.output.strictModuleErrorHandling = false; + // Normalize global object. new compiler.webpack.BannerPlugin({ raw: true, From 6784cbb52e2b2a3cda78e6cb5ccbfce981b8c8f7 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 13:24:56 +0100 Subject: [PATCH 3/9] fix: typing for ModuleObject --- .../repack/src/modules/ScriptManager/types.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/repack/src/modules/ScriptManager/types.ts b/packages/repack/src/modules/ScriptManager/types.ts index 273519721..9933620eb 100644 --- a/packages/repack/src/modules/ScriptManager/types.ts +++ b/packages/repack/src/modules/ScriptManager/types.ts @@ -1,18 +1,21 @@ type ModuleExports = Record; +type ModuleObject = { + id: number; + loaded: boolean; + error?: any; + exports: ModuleExports; +}; + export interface WebpackContext { i: ((options: { id: number; factory: ( - moduleObject: Record, + moduleObject: ModuleObject, moduleExports: ModuleExports, webpackRequire: WebpackContext ) => void; - module: { - id: number; - loaded: boolean; - exports: Record; - }; + module: ModuleObject; require: WebpackContext; }) => void)[]; p: () => string; From 776bf4f7e8548b14cf6c061d753a8c12891f6dc3 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 13:53:29 +0100 Subject: [PATCH 4/9] chore: update setups for testers --- apps/tester-app/android/gradle.properties | 1 + apps/tester-app/ios/Podfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/tester-app/android/gradle.properties b/apps/tester-app/android/gradle.properties index 0c541f3a9..b729139a5 100644 --- a/apps/tester-app/android/gradle.properties +++ b/apps/tester-app/android/gradle.properties @@ -38,6 +38,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64 # Note that this is incompatible with web debugging. newArchEnabled=true bridgelessEnabled=true +hermesEnabled=true # Uncomment the line below to build React Native from source. #react.buildFromSource=true diff --git a/apps/tester-app/ios/Podfile.lock b/apps/tester-app/ios/Podfile.lock index 6aa37aae3..794d6a02f 100644 --- a/apps/tester-app/ios/Podfile.lock +++ b/apps/tester-app/ios/Podfile.lock @@ -1,6 +1,6 @@ PODS: - boost (1.84.0) - - callstack-repack (5.0.0-rc.2): + - callstack-repack (5.0.0-rc.3): - DoubleConversion - glog - hermes-engine @@ -1942,7 +1942,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost: 1dca942403ed9342f98334bf4c3621f011aa7946 - callstack-repack: 3106db24c24f7a76a380230ff7794d225cecb760 + callstack-repack: 5219eedfb8cb06b905edecffaecf71af4a4ecdd6 DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385 FBLazyVector: be7314029d6ec6b90f0f75ce1195b8130ed9ac4f fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be From 7f40c76a26d600d4326f2c33465ea812fdf3f43f Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 13:54:56 +0100 Subject: [PATCH 5/9] chore: add changeset --- .changeset/late-crabs-deliver.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/late-crabs-deliver.md diff --git a/.changeset/late-crabs-deliver.md b/.changeset/late-crabs-deliver.md new file mode 100644 index 000000000..b3daa3b58 --- /dev/null +++ b/.changeset/late-crabs-deliver.md @@ -0,0 +1,5 @@ +--- +"@callstack/repack": patch +--- + +Fix early JS errors not being displayed in LogBox From 966ffd944a4c5a1655f5c96bf2fece3fbb82bf25 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 13:55:49 +0100 Subject: [PATCH 6/9] fix: typing in tests --- .../src/modules/ScriptManager/__tests__/ScriptManager.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts b/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts index 49e9458cb..b20003189 100644 --- a/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts +++ b/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts @@ -20,6 +20,7 @@ jest.mock('../NativeScriptManager', () => ({ })); globalThis.__webpack_require__ = { + i: [], u: (id: string) => `${id}.chunk.bundle`, p: () => '', repack: { From 67908320048eb6f189d8d0adbc3148c92e27624a Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 15:44:27 +0100 Subject: [PATCH 7/9] chore: add comment about ErrorUtils --- .../src/plugins/RepackTargetPlugin/implementation/init.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts index 39fbdd01c..8d2533b42 100644 --- a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts +++ b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts @@ -20,6 +20,8 @@ module.exports = function () { try { originalFactory.call(this, moduleObject, moduleExports, webpackRequire); } catch (e) { + // exposed as global early on, part of `@react-native/js-polyfills` error-guard + // https://github.com/facebook/react-native/blob/4dac99cf6d308e804efc098b37f5c24c1eb611cf/packages/polyfills/error-guard.js#L121 $globalObject$.ErrorUtils.reportFatalError(e); } }; From 5032be78835b7c789f35c7820a6560512449b34a Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 15:47:36 +0100 Subject: [PATCH 8/9] fix: handle scenario were ErrorUtils are not initialized yet --- .../RepackTargetPlugin/implementation/init.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts index 8d2533b42..0c1deb826 100644 --- a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts +++ b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts @@ -20,9 +20,15 @@ module.exports = function () { try { originalFactory.call(this, moduleObject, moduleExports, webpackRequire); } catch (e) { - // exposed as global early on, part of `@react-native/js-polyfills` error-guard - // https://github.com/facebook/react-native/blob/4dac99cf6d308e804efc098b37f5c24c1eb611cf/packages/polyfills/error-guard.js#L121 - $globalObject$.ErrorUtils.reportFatalError(e); + if ($globalObject$.ErrorUtils) { + // exposed as global early on, part of `@react-native/js-polyfills` error-guard + // https://github.com/facebook/react-native/blob/4dac99cf6d308e804efc098b37f5c24c1eb611cf/packages/polyfills/error-guard.js#L121 + $globalObject$.ErrorUtils.reportFatalError(e); + } else { + // error happened before ErrorUtils was initialized + // at this point in runtime we can only rethrow the error + throw e; + } } }; }); From 93a466869bd9c9b62699de21231c1e16b5d2d6c5 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Wed, 18 Dec 2024 15:49:48 +0100 Subject: [PATCH 9/9] chore: add reference to guardedLoadModule in Metro --- .../src/plugins/RepackTargetPlugin/implementation/init.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts index 0c1deb826..dc93ab84f 100644 --- a/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts +++ b/packages/repack/src/plugins/RepackTargetPlugin/implementation/init.ts @@ -14,6 +14,8 @@ module.exports = function () { __webpack_require__.repack = $globalObject$.__repack__ = repackRuntime; // intercept module factory calls to forward errors to global.ErrorUtils + // aligned with `guardedLoadModule` behaviour in Metro + // https://github.com/facebook/metro/blob/a4cb0b0e483748ef9f1c760cb60c57e3a84c1afd/packages/metro-runtime/src/polyfills/require.js#L329 __webpack_require__.i.push(function (options) { var originalFactory = options.factory; options.factory = function (moduleObject, moduleExports, webpackRequire) {