Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamically imported module #55

Open
nephix opened this issue Jul 21, 2022 · 0 comments
Open

dynamically imported module #55

nephix opened this issue Jul 21, 2022 · 0 comments

Comments

@nephix
Copy link

nephix commented Jul 21, 2022

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 want to init multiple widgets on a site
  • A server response decides which widgets to init
  • I only want to request/load the widgets that are supposed to be mounted

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant