Browse Source

add setFSModule (#755)

David Sehnal 2 years ago
parent
commit
01cb23f566
3 changed files with 13 additions and 6 deletions
  1. 1 0
      CHANGELOG.md
  2. 3 0
      src/examples/image-renderer/index.ts
  3. 9 6
      src/mol-util/data-source.ts

+ 1 - 0
CHANGELOG.md

@@ -16,6 +16,7 @@ Note that since we don't clearly distinguish between a public and private interf
 - Add exposure parameter to renderer
 - Only trigger marking when mouse is directly over canvas
 - Fix blurry occlusion in screenshots
+- [breaking] Add `setFSModule` to `mol-util/data-source` instead of trying to trick WebPack
 
 ## [v3.31.4] - 2023-02-24
 

+ 3 - 0
src/examples/image-renderer/index.ts

@@ -19,8 +19,11 @@ import { StructureRepresentation3D } from '../../mol-plugin-state/transforms/rep
 import { HeadlessPluginContext } from '../../mol-plugin/headless-plugin-context';
 import { DefaultPluginSpec } from '../../mol-plugin/spec';
 import { STYLIZED_POSTPROCESSING } from '../../mol-plugin/util/headless-screenshot';
+import { setFSModule } from '../../mol-util/data-source';
 
 
+setFSModule(fs);
+
 interface Args {
     pdbId: string,
     outDirectory: string

+ 9 - 6
src/mol-util/data-source.ts

@@ -300,14 +300,17 @@ 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.
+// NOTE: a workaround for using this in Node.js
 let _fs: (typeof import ('fs')) | undefined = undefined;
 function getFS() {
-    if (_fs) return _fs!;
-    const req = require; // To fool webpack
-    _fs = req('fs');
-    return _fs!;
+    if (!_fs) {
+        throw new Error('When running in Node.js and reading from files, call mol-util/data-source\'s setFSModule function first.');
+    }
+    return _fs;
+}
+
+export function setFSModule(fs: typeof import ('fs')) {
+    _fs = fs;
 }
 
 /** Alternative implementation of ajaxGetInternal (because xhr2 does not support file:// protocol) */