diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a9286f9effe1e23582d8065cef37a405c29e27b..b065baa5719b5f4bdbb6a54453885ecf857dbec9 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/src/examples/image-renderer/index.ts b/src/examples/image-renderer/index.ts index 4acaa96cd27581bec00a8e88d855f71b83b0e236..a4f6961115f7176edac416948bf8e2c924a986f0 100644 --- a/src/examples/image-renderer/index.ts +++ b/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 diff --git a/src/mol-util/data-source.ts b/src/mol-util/data-source.ts index 3d66dbf7466cc0ce305b10ce518949ca5377a34a..759e5b644b401fc314f4b5ba65043efa39522b21 100644 --- a/src/mol-util/data-source.ts +++ b/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) */