zouhir / preact-habitat

Zero configuration Preact widgets renderer in any host DOM
MIT License
518 stars 43 forks source link

dynamically imported module #55

Open nephix opened 2 years ago

nephix commented 2 years ago

Hey,

is it possible to use habitat() for dynamically imported ES modules like:

import habitat from "preact-habitat";
...
const module = await import("/js/module.js"); // module.js is a compiled preact component
const { render } = habitat(module);

my use case is that:

I tried:

const modules: Record<Modules, string> = {
  widget1: "/js/modules/1.js",
  widget2: "/js/modules/2.js",
  widget3: "/js/modules/3.js",
};

const module = await import(modules[feature]); // feature is "widget1" | "widget2" | "widget3"
const { render } = habitat(module.default); // each widget is a bundled preact functional component with default export
render({ selector: "..." })

But I keep getting errors a la:

TypeError: Cannot read properties of undefined (reading '__H')

When I import the module directly, it works ofc