I have worked for several companies, both as a full-time employee and as a contractor. One thing has always remained true: There are multiple languages and frameworks being used across their frontend applications. (Well, okay, there was that small startup that only had one, but this still applies.) Often several teams have people with different skill sets, and they might decide that it’s easier to go with React. Another team might go with Vue because they feel it’s the next great framework.
Mitosis uses a static subset of JSX, inspired by Solid. This means we can parse it to a simple JSON structure, then easily build serializers that target various frameworks and implementations.
As of April 2022, Builder.io's Mitosis supports
You can try this yourself in Builder.io's Mitosis Fiddle pictured below.
How does this compare to other compile time frameworks?
Two very popular compile-time frameworks are Svelte and SolidJS. Mitosis is very similar to these in terms of it being a compile time framework which makes it very fast. Where it differs is that it lets you produce multiple frameworks, allowing for the most flexibility.
Similar to SolidJS, Mitosis uses a version of JSX that compiles the components to JSON. Plugins then compile the components to different targets, enabling you to create tooling in two directions:
Mitosis is supported by Builder.io, because Mitosis also supports no-code tools. It does this by following the above serialization to Builder.io's JSON format. This makes Mitosis a very stable product because we will need to continue updating and improving the code base so that Builder.io remains successful.
How to install the CLI
First install the CLI by opening your terminal and executing:
The mitosis CLI has simple commands to follow but if you ever forget, you can run
mitosis --help. Which will show you the usage and examples.
It is important to remember that Mitosis is just the compiler here. We'll still need to create the JSX files that represent our component, which Mitosis will then output.
How to create a Mitosis Project
First create a new directory and change into it:
Now create an npm project so that we can use the
I would also highly recommend initializing this directory with git (this is optional):
If you're using git make sure to create a
.gitignore file with the following:
Install the Builder.io Mitosis package into the local project:
Add JSX Mitosis File
The file name should end with
lite.jsx this allows the VSCode extension to be used.
Open your project in VSCode:
Your project should look like this in VSCode:
Add JSX to the
If you've ever written in React, this should feel very familiar to you.
Before you output our
component.lite.jsx to any available language, take a close look at how state is used. The useState hook will be the same as how you use it in React. Below you can see a few of the frameworks and how they use this definition to appropriately assign the state variable for each input.
As you can see the state variable
name used within the JSX, is correctly compiled in the other frameworks below.
Now that you have the Mitosis CLI setup, your
component.lite.jsx file created, and a better understanding of how state works, it's time to generate your own framework files.
I like to keep my output files in a separate folder. To create that folder execute:
To start compiling open a terminal in the same directory as your component.lite.jsx file (in
VSCode Ctrl + `) and run the below command:
--to= flag sets the framework that you compile your Mitosis component output to, followed by the location of your Mitosis input file
> represents where the output will be placed, and finally the output file location including any directory
After running the above example you will get the below React code.
Mitosis is the easiest way to future-proof your current development. If you're starting a new greenfield project, it will be even more important to future-proof, as well as far simpler to start writing from scratch rather than converting your current components.
Mitosis allows multiple teams to work on a single unified language no matter what framework they're using. This reduces the amount of duplicate components and lowers technical debt.
Have more questions? See the Mitosis Docs.
Full example can be found at https://github.com/codercatdev/mitosis-example