Get started with a Vite Package, setup with TypeScript and Lit
This page is a work in progress. It will be updated as the software evolves.
Umbraco recommends building extensions with a setup using TypeScript and a build tool such as Vite. Umbraco uses the library Lit for building web components which we will be using throughout this guide.
Vite comes with a set of really good presets to get you quickly up and running with libraries and languages. For example: Lit, Svelte, and Vanilla Web Components with both JavaScript and TypeScript.
Before following this guide, read the Setup Your Development Environment article.
Run the following command in the App_Plugins
which is found (or needs to be created) at the root of your project:
This command will help you set up your new package, asking you to pick a framework and a compiler.
To follow this tutorial, we recommend you enter my-dashboard
as the Project Name when prompted, although you can choose any other you like. Then choose Lit and TypeScript.
This creates a new folder, sets up our new project, and creates a package.json
file, which includes the necessary packages.
Instead of the 2 above steps, you can do the following:
This will create a Vite Package with Lit and Typescript in a folder called my-dashboard.
Navigate to the new project folder my-dashboard and install the packages using:
Install the Backoffice package. You can install the package using the following command:
Optionally you can use --legacy-peer-deps
in the installation command to avoid installing Umbraco´s sub-dependencies like TinyMCE and Monaco Editor:
npm install --legacy-peer-deps -D @umbraco-cms/backoffice@14.0.0-rc3
If this is used the Intellisense to those external references will not be available.
Create a new file called vite.config.ts
in my-dashboard
folder and insert the following code:
This alters the Vite default output into a library mode, where the output is a JavaScript file with the same name as the name
attribute in package.json
. The name is my-dashboard
if you followed this tutorial with no changes.
The source code that is compiled lives in the src
folder of your package folder and that is where you can see a my-element.ts
file. You can confirm that this file is the one specified as our entry on the Vite config file that we recently created.
The build:lib:entry
parameter can accept an array which will allow you to export multiple files during the build. You can read more about Vite's build options here.
Build Package
Build the ts
file in the my-dashboard
folder so we can use it in our package:
If you like to continuously work on the package and have each change built, you can add a watch
script in your package.json
with vite build --watch
. The example below indicates where in the structure this change should be implemented:
Then in the terminal, you can run npm run watch
.
Declare your package to Umbraco via a file called umbraco-package.json
. This should be added at the root of your package. In this guide, it is inside the my-dashboard
folder.
This example declares a Dashboard as part of your Package, using the Vite example element.
Umbraco needs the name of the element that will render as default when our dashboard loads.
This is specified in the manifest as the elementName
.
Another approach would be to define your default element in the TS code. To do this, in the src/my-element.ts
add default
to your MyElement
class in the file like so:
Learn more about the abilities of the manifest file in the Umbraco Package Manifest article.
To be able to test your package, you will need to run your site.
Before you do this, you need to include all the files in the src
folder and the umbraco-package.json
file in your project.
If you try to include these resources via Visual Studio (VS), then only the dist
folder needs to be included. Otherwise, VS will try to include a few lines on your .csproj
file to compile the TypeScript code that exists in your project folder. When you run your website, VS will try to compile these files and fail.
Result
The final result looks like this:
If the Vite logo is not found, the path to its location needs to be changed. Update the my-element.ts
file in the src
folder accordingly:
In the same file, you will need to change the background-color
of the button
to white so it is visible:
With this, you have set up your Package and created an Extension for the Backoffice.
This Dashboard will appear on all sections, so continue the following tutorial on Creating A Custom Dashboard.