Skip to content

Bundling JS modules #41746

@daniel-p-tech

Description

@daniel-p-tech

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

No response

Describe the solution you'd like

Hi,

I have library of about 30 reusable UI components, most of which use some level of JS interop. All JS files are co-located with .razor, .cs and .css files and the corresponding JS modules are imported when a component is first initialized using the following statement;

JSRuntime.InvokeAsync<IJSObjectReference>("import", $"./_content/{assemblyName}/{componentName}.js"

image

This works great, but I did notice when running my app on a mobile device and a very slow network that loading of a module may fail. This is a problem since failure to load JS code is a catastrophic event - some of the more complex components orchestrate communication between other simpler components etc. so I need to have a guarantee that when accessing a particular app page, all components on the page will be successfully initialized. Things like failure to load some data on the page using web api is being properly handled, but I don't see an elegant way how to handle a failure to load a JS module.

What I'd like to achieve is to somehow bundle all JS modules into a single file that will be loaded when the application first initializes. How can I do this without rewriting any of my existing code? I love how JS colocation provides clean separation of concerns. All combined, my JS files are only about 30K uncompressed.

Also, provided that there is a solution to bundle all JS files, I'd like to minimize/obfuscate them as well.

Maybe there is a completely different approach (error boundaries) that I need to look at?

Thank you @javiercn for your help!

Daniel

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions