From 445977d99bcb6d77e96e139266d440923a1a28d1 Mon Sep 17 00:00:00 2001
From: dsehnal <david.sehnal@gmail.com>
Date: Sun, 5 Feb 2023 22:34:38 +0100
Subject: [PATCH] Fix data-source node workaround

---
 src/mol-util/data-source.ts | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/mol-util/data-source.ts b/src/mol-util/data-source.ts
index 753fbc36e..5b9b65a68 100644
--- a/src/mol-util/data-source.ts
+++ b/src/mol-util/data-source.ts
@@ -11,16 +11,10 @@
 import { utf8Read } from '../mol-io/common/utf8';
 import { RuntimeContext, Task } from '../mol-task';
 import { Asset, AssetManager } from './assets';
-import { LazyImports } from './lazy-imports';
 import { File_ as File, RUNNING_IN_NODEJS, XMLHttpRequest_ as XMLHttpRequest } from './nodejs-shims';
 import { ungzip, unzip } from './zip/zip';
 
 
-const lazyImports = LazyImports.create('fs') as {
-    'fs': typeof import ('fs'),
-};
-
-
 export enum DataCompressionMethod {
     None,
     Gzip,
@@ -306,12 +300,21 @@ function ajaxGetInternal<T extends DataType>(title: string | undefined, url: str
     });
 }
 
+// NOTE: lazy imports cannot be used here because WebPack complains since this
+// is part of the "browser" build.
+let _fs: (typeof import ('fs')) | undefined = undefined;
+function getFS() {
+    if (_fs) return _fs!;
+    _fs = require('fs');
+    return _fs!;
+}
+
 /** Alternative implementation of ajaxGetInternal (because xhr2 does not support file:// protocol) */
 function ajaxGetInternal_file_NodeJS<T extends DataType>(title: string | undefined, url: string, type: T, body?: string, headers?: [string, string][]): Task<DataResponse<T>> {
     if (!RUNNING_IN_NODEJS) throw new Error('This function should only be used when running in Node.js');
     if (!url.startsWith('file://')) throw new Error('This function is only for URLs with protocol file://');
     const filename = url.substring('file://'.length);
-    const data = lazyImports.fs.readFileSync(filename);
+    const data = getFS().readFileSync(filename);
     const file = new File([data], 'raw-data');
     return readFromFile(file, type);
 }
-- 
GitLab