diff --git a/src/mol-util/data-source.ts b/src/mol-util/data-source.ts index 753fbc36e11a9d7b1cc23f514c0f65c3347e2941..5b9b65a68793cf04d49e800e36bc82a6fa5b308c 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); }