Vite Package Setup

Get started with a Vite Package, setup with TypeScript and Lit

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 use throughout this guide.

These are general recommendations for working with and building extensions for the Umbraco backoffice. You can use any framework or library of your choice.

Before You Begin

Make sure to read the Setup Your Development Environment article before continuing.

Create a Vite Package

Vite comes with a set of 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.

  1. Open your terminal and navigate to the folder where you want to create the new Vite package.

  2. Run the following command:

npm create vite@latest

This command starts a setup prompt.

For this tutorial, it is recommended to use the names given below. However, feel free to choose other names if preferred.

  1. When prompted:

    • Enter client as the Project Name.

    • Select Lit as the framework.

    • Select TypeScript as the variant.

    This creates a new folder called client with your project files.

Alternatively, to skip the prompts, use this command:

npm create vite@latest client -- --template lit-ts

For Windows environments the command should be slightly different::

npm create vite@latest client --- --template lit-ts

or you will still see the interactive prompts, especially when using PowerShell.

  1. Navigate into the new client folder and install the packages:

cd client
npm install
  1. Install the Umbraco Backoffice package:

npm install -D @umbraco-cms/backoffice
  1. To avoid installing additional dependencies such as TinyMCE or Monaco Editor,use the --legacy-peer-deps flag:

npm install --legacy-peer-deps -D @umbraco-cms/backoffice

This disables IntelliSense for external references but keeps the install lean.

  1. Open the tsconfig.json file.

  2. Add the array types inside compilerOptions, with the entry of @umbraco-cms/backoffice/extension-types:

{
    "compilerOptions": {
        ...
        "types": [
            "@umbraco-cms/backoffice/extension-types"
        ]
    }
}
  1. Create a new vite.config.ts file in the client folder:

vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
    build: {
        lib: {
            entry: "src/my-element.ts", // your web component source file
            formats: ["es"],
        },
        outDir: "../App_Plugins/client", // all compiled files will be placed here
        emptyOutDir: true,
        sourcemap: true,
        rollupOptions: {
            external: [/^@umbraco/], // ignore the Umbraco Backoffice package in the build
        },
    },
    base: "/App_Plugins/client/", // the base path of the app in the browser (used for assets)
});

The outDir parameter specifies where the compiled files are placed. In this example, they are stored in the App_Plugins/client folder. If you are working with a different structure, such as a Razor Class Library (RCL) project, update this path to wwwroot.

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 client.js 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.

  1. Build the ts file in the client folder:

npm run build

Watch for changes and build

To continuously work on the package and have each change built, add a watchscript in your package.json with vite build --watch.

The example below indicates where in the structure this change should be implemented:

package.json
{
  "name": "client",
  ...
  "scripts": {
    "watch": "vite build --watch"
    ...
  },
  ...

Run npm run watch in the terminal.

Umbraco Package declaration

Declare your package to Umbraco via a file called umbraco-package.json. This should be added in the public folder under the root of your package. Once built the umbraco-package.json file should be located at /App_Plugins/ or /App_Plugins/{YourPackageName} for Umbraco to detect it.

This example declares a Dashboard as part of your Package, using the Vite example element.

client/public/umbraco-package.json
{
    "$schema": "../../umbraco-package-schema.json",
    "name": "My Dashboard",
    "version": "0.1.0",
    "extensions": [
        {
            "type": "dashboard",
            "alias": "My.Dashboard.MyExtension",
            "name": "My Dashboard",
            "element": "/App_Plugins/client/client.js",
            "elementName": "my-element",
            "meta": {
                "label": "My Dashboard",
                "pathname": "my-dashboard"
            }
        }
    ]
}

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:

export default class MyElement extends LitElement {

Learn more about the abilities of the manifest file in the Umbraco Package Manifest article.

Testing your package

To test your package, run your site.

Before doing this, make sure to run npm run build to compile your TypeScript files and copy them to the App_Plugins/client folder.

The final result looks like this:

In the src/my-element.ts file, update the styles property to make any styling changes. You can change the background-color of the button to white so it is more visible:

button {
    background-color: white;
}

Summary

With this, you have set up your Package and created an Extension for the Backoffice.

This Dashboard appears in all sections and does not do much. To extend it to interact with the Umbraco Backoffice, follow the tutorial on Creating Your First Extension.

Last updated

Was this helpful?