From aad7300e4bc7c185e066f8ef44ac5db92d535bdf Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 29 Jul 2024 15:41:44 +0530 Subject: [PATCH] Take 2 --- web/docs/dependencies.md | 7 +++++-- web/packages/new/photos/services/ml/worker.ts | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/web/docs/dependencies.md b/web/docs/dependencies.md index e78b2c9907..c087e92811 100644 --- a/web/docs/dependencies.md +++ b/web/docs/dependencies.md @@ -192,8 +192,11 @@ For more details, see [translations.md](translations.md). ## Media - [ExifReader](https://github.com/mattiasw/ExifReader) is used for Exif - parsing. [piexifjs](https://github.com/hMatoba/piexifjs) is used for writing - back Exif (only supports JPEG). + parsing. We also need its optional peer dependency + [@xmldom/xmldom](https://github.com/xmldom/xmldom) since the browser's + DOMParser is not available in web workers. + [piexifjs](https://github.com/hMatoba/piexifjs) is used for writing back + Exif (only supports JPEG). - [jszip](https://github.com/Stuk/jszip) is used for reading zip files in the web code (Live photos are zip files under the hood). Note that the desktop diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts index e50781fe20..86f7dc9f86 100644 --- a/web/packages/new/photos/services/ml/worker.ts +++ b/web/packages/new/photos/services/ml/worker.ts @@ -96,20 +96,23 @@ export class MLWorker { // user's token. It'll be used to download files to index if needed. await downloadManager.init(await ensureAuthToken()); - // Take an explicit dependency on the DOMParser provided by xmldom so - // that webpack does not tree shake it. - // // Normally, DOMParser is available to web code, so our Exif library // (ExifReader) has an optional dependency on the the non-browser // alternative DOMParser provided by @xmldom/xmldom. // // But window.DOMParser is not available to web workers. // - // So we want to use the @xmldom/xmldom version. For this, we need to - // also explicitly refer to it so that webpack does not tree shake it, - // since inside ExifReader it is weakly referenced using - // `__non_webpack_require__('@xmldom/xmldom')`. - DOMParser; + // So we need to get ExifReader to use the @xmldom/xmldom version. + // ExifReader references it using the following code: + // + // __non_webpack_require__('@xmldom/xmldom') + // + // So we need to explicitly reference it to ensure that it does not get + // tree shaken by webpack. But ensuring it is part of the bundle does + // not seem to work (for reasons I don't yet understand), so we also + // need to monkey patch it (This also ensures that it is not tree + // shaken). + globalThis.DOMParser = DOMParser; } /**