As you build out your UI extensions, you can leverage npm workspaces to share code across your app cards, settings page, app pages, and serverless functions. This allows you to consolidate common logic such as formatting dates and currency, and ensure that any fixes or updates are rolled out consistently to each UI extension.Documentation Index
Fetch the complete documentation index at: https://developers.hubspot.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Before getting started, install the latest version of the HubSpot CLI. In a terminal window, run the following command:8.0.0 or later.
- You can check which version of the CLI you have by running
hs --version. - If needed, you can run the command
npm install -g @hubspot/cli@latestto update to the latest version of the HubSpot CLI.
Project structure
The code for your card, setting page, and home page follows the same directory structure as outlined in the app configuration article. The main change you’ll need to make to set up code sharing between your extensions will be to add apackage.json file at the root of your project under src/app/. The naming convention and scope you choose for these shared packages is entirely customizable, allowing you to use a pattern you’re already comfortable with, such as @packages/types.
An example directory structure that demonstrates the root package.json location, along with the shared code for types, utils, and components is shown below:
Configure npm workspaces
After you create apackage.json file in the src/app/ directory of your project, open the package.json file and define the name and workspaces fields:
name: the name of your project (e.g.,my-hubspot-project).workspaces: an array that includes the UI extension directories where you plan to use your shared code, along with the directory where your shared code lives (e.g.,packages/*).
package.json file would be:
Create shared packages
You can use any naming convention when defining the package scope for your shared code (e.g.,@my-hubspot-project in the examples below).
The code blocks below provide examples for defining shared types, utils, and components that you can then import into your UI extensions.
Types
The examples below define a simpleMoney type under packages/types in your project.
- package.json
- index.tsx
Utilities
The examples below define a utility underpackages/utils that will format data using the Money type from above.
- package.json
- index.tsx
Components
The examples below demonstrate a shared component that lives underpackages/components.
- package.json
- index.tsx
Add dependencies to extensions
After you’ve defined your shared code underpackages/, add the path to your shared packages in each of your extension’s respective package.json files:
Use shared code in your UI extensions
The following code block shows how to use your shared code within one of your UI extensions:Install shared packages
To ensure your shared packages can be imported successfully in your UI extension, run the following command anywhere in your project:hs project dev or hs project upload.