React Based XBlock Template

Hi team, I made a new template/sample XBlock that uses React to render both the Student and Author views: GitHub - open-craft/xblock-react: An example of a React-based XBlock (React student_view, React studio_view, react-intl)

It’s based on the XBlock cookiecutter (which, sadly, turned out to be broken). It works, integrates with Tutor, uses React 18 for both student_view and studio_view , is compatible with the new Studio unit MFE page, is fully type-safe (TypeScript), and uses react-intl for i18n. It does not support Paragon (as its SCSS is not available and I found that Paragon has a few bugs that make it unsuitable).

I plan to apply some of these changes to the cookiecutter template if they’re well received.

Cross-posted from Open edX forum and Slack.

Ticket: MNG-4494

6 Likes

@braden This is cool. I am super happy to see it uses rollup and not webpack. I was hoping it would be esbuild, but I get that rollup is better for bundling that esbuild. Now, I wish someone would resurrect that old frontend migration thread about webpack to something faster and modern. I digress.

I am curious to learn if you focused only on using React for this, or did you experiment with the idea of using “any library as dependency”, that you mentioned in the conference this year?

1 Like

This is quite interesting! I had a lot of issues with the cookiecutter as well, I think it’s a bit broken and probably can be modernised a lot.

I worked on something very similar recently for WGU. I decided to create the accordion XBlock using Vite, React and Paragon for both the studio and student UIs.

It’s still an unmerged PR due to the status of the WGU XBlocks project, but it should be fully working except for a bit of polish.

5 Likes

I second the implementation @kshitij has done a lot of great patterns there :slight_smile:

2 Likes

Wow, nice work @kshitij! I wish I’d known about that before I started making my version :sweat_smile:

One thing I did with mine is I tried to make it easy to transform back into a cookiecutter format, and I’m still thinking of doing that. It would be nice to do the same with some of the nice features you’ve added. (e.g. yours supports Paragon and has an external dev environment, and those are both a big plus)

I think the paragon support can be simplified a LOT in the future once the theme is loaded at runtime because, then, we can use a cached version of the paragon theme from a CDN and have the XBlock adapt to the theme easily. Currently, I wanted to keep the file size as low as possible so I only import the bare minimum of Paragon that I needed

1 Like

@braden @kshitij I also like the use of vite as build system, maybe it’s high time we evaluate the javascript build system that we are using, what do you folks thing about it?

Also maybe we can use some CC hours to improve the accordion xblock as well.

and I forgot about it but it’s worth a lightning talk at least :slight_smile:

@farhaan

Which “we” do you mean? If it’s OpenCraft, many of us are already using Vite by default and I encourage everyone to. (Listaflow uses Vite, Kshitij’s plugin uses Vite, etc.) I only used rollup in my sample XBlock to keep it a bit simpler, knowing that Vite just wraps rollup anyways.

But if it’s “we the Open edX community”, I have tried several times to suggest moving to Vite, and each time the decision has been made to stick with webpack. Currently the main reason for it is Vite’s lack of working support for “module federation”.

1 Like

Worth noting that there is a plugin for this, available here: GitHub - originjs/vite-plugin-federation: Module Federation for vite & rollup

…But it sounded like there were concerns about its comprehensiveness. I don’t know how hard it would be to close that gap, but I wonder if we could contribute something or if it would be too big/unwieldy a thing to improve while uninitiated. It would be great if we could move everything to Vite-- the DX is so much better.

1 Like

Sorry for the late reply but that makes sense to me :slight_smile: