We are building an application where we allow our customers to build “apps” and install them into our application on runtime. We don’t want our customers to remember adding <require> statements in their views (this also wouldn’t make any sense, as they don’t have the source code of our application available), so we would like to make custom elements (built by us) available globally.
Right now we use FrameworkConfiguration.globalResources() on startup, but it is giving us a slow startup time, so we are exploring how to lazy load custom elements whenever a view is using it.
Currently we have experimented with a solution where we can parse the views from customers before loading them, and find any (unloaded) custom elements, and load them using something like this:
Parse a view and find any custom elements (not already loaded)
E.g. <custom1> will then be loaded like this:
await new FrameworkConfiguration.globalResource(customElements.custom1).apply()
This works, but if one of the custom elements contains any not already loaded custom elements, then we’re stuck, as we haven’t found a way to find custom elements in other custom elements.
Do you have any ideas on a better approach, preferably using some of Aurelias components (ViewCompiler, VIewResources, etc.)?
From what you described, the “lazy global resources” could be very much local to those elements. It feels so to me because you still need to teach those how to load anyway ? Or they are supposed to trigger the load with very simple configuration name like "load GlobalA if you load me, and I only know the name of the thing I want, its GlobalA" ?
Edit: also if you allow arbitrary modules to be loaded, then i guess you are not using webpack. Which i think made the task simpler ? You can tell the loader how to map a certain name to a certain url
The customElements-object is set by us, and our customers know which custom elements are available. Basically they just know, that they can use the tags in their views, they are not concerned about loading at all.
The custom elements can be used in apps (from customers) and/or in modules built by us, so they should be used both in compile-time modules and runtime-apps. The lazy loader should work, so whenever a view is using a custom element, it should be loaded. When other views are using the same custom element in the same session, they obviously don’t need to load the custom element as it has already been loaded.
We are using Webpack, combined with DLL, that our customers can use in their building process (using a tool). Thus they can only use the specific modules we have made available in the DLL, including the custom elements we specify in the customElements object.
I have solved the requirement of automatically load the necessary module once, without <require>, but it still required some configuration. I have some ideas how to auto trigger the loading, but will need some experiment first. Ill see what i can come up with and get back to you.
I’m sure it’s possible, I’m just worried that our current solution is too custom (and also still not solves the problem completely), i.e. that we should use some of Aurelias templating-modules instead.
Im still making it. Probably it will be released as plugin instead an official API. It will support lazy loading for standard resources types: elements, attributes, binding behavior and value converter. I got some progress but delayed a bit. Should be ready around this weekend
I like the patch, it’s simple and managable inside your application @boysensci . Also it requires only low level patching. If that fits your need probably I won’t be releasing the patch I intended for it, since it requires heavier patching.