Custom Dependencies
Learn how to add and use custom JavaScript libraries in your template expressions for data manipulation and transformation.
Introduction
While Compozy provides several built-in libraries (like Lodash and Day.js) for template expressions, you can extend this functionality by adding your own dependencies. These dependencies should focus on synchronous data manipulation and transformation operations.
Since Compozy uses Deno runtime, dependencies should be compatible with Deno’s module system. Additionally, template expressions are meant for data transformation, not for async operations.
Limitations
Keep in mind the following limitations when using custom dependencies:
- Dependencies must be compatible with Deno’s module system
- Only synchronous operations are supported in templates
- Async operations (HTTP, file I/O) are not allowed
- Some npm packages might not work in Deno context
Adding Dependencies
Custom dependencies are specified in your document’s frontmatter using the template-dependencies
field. All custom dependencies are automatically namespaced under the $
object to avoid naming conflicts:
All custom dependencies are accessible through the $
object in your templates. For example:
$.math
for Math.js functions$.decimal
for Decimal.js operations
This namespacing prevents conflicts with built-in variables and functions while keeping your custom dependencies organized.
Usage Examples
Best Practices
Version Management
Always specify exact versions in the package URL:
Deno Compatibility
- Use libraries that are compatible with Deno’s module system
- Avoid libraries that rely on Node.js specific features
- Check the library’s ESM compatibility
Performance
- Import only necessary packages
- Use lightweight alternatives when possible
- Consider bundle size impact
Error Handling
Implement proper error handling in your template expressions:
Dependency Documentation
When using custom dependencies, refer to their official documentation for available methods and usage:
Common Use Cases
Mathematical Operations
Use Math.js for complex calculations, matrix operations, and unit conversions
Precise Calculations
Handle financial calculations with Decimal.js for exact decimal arithmetic
Built-in Libraries
Compozy includes several popular libraries by default:
- Lodash (
_
) - Utility functions for arrays, objects, and more - Day.js (
dayjs
) - Modern date/time manipulation - Voca (
v
) - String manipulation and formatting - Numeral.js (
numeral
) - Number formatting
See Template Functions for details on built-in libraries.