Attempting to eliminate the use of import
statements and instead dynamically load Javascript files using a specific class.
This particular class is used for this purpose:
export function JSImport<T>(
importPromise: Promise<T>,
): Promise<T> {
// CommonJS's `module.exports` is wrapped as `default` in ESModule.
return importPromise.then((m: any) => (m.default || m) as T);
}
However, encountering an error when attempting to instantiate imported files with constructor parameters.
This is how it was previously done successfully:
declare let Tiff: any;
const tiff = new Tiff({buffer: xhr.response});
Here is the updated approach:
const importTiff = JSImport(
import(/* webpackChunkName: "Tiff" */ 'Tiff'),
);
try {
importTiff.then((Tiff: any) => {
const tiff = new Tiff({buffer: xhr.response});
....
}
The new code results in a TypeError: Tiff is not a constructor
When looking at the Tiff variable in the Chrome console, it appears as shown in the following image: https://i.sstatic.net/UpcyR.png
How can I utilize it as a constructor?