### assetUrl Function Usage Examples Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc These examples demonstrate practical usage of the `assetUrl` function, showing how to generate a URL for a CSS file by providing its path, presented in both JavaScript and TypeScript syntax. ```javascript const url = assetLib.assetUrl({ path: 'styles/main.css' }); ``` ```typescript const url = assetUrl({ path: 'styles/main.css' }); ``` -------------------------------- ### Install Enonic lib-asset Types Source: https://github.com/enonic/lib-asset/blob/master/types/README.md Command to install the `@enonic-types/lib-asset` package as a development dependency using npm. This package provides TypeScript type definitions for the Enonic asset library. ```bash npm i --save-dev @enonic-types/lib-asset ``` -------------------------------- ### Install TypeScript Type Definitions for lib-asset Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This command installs the `@enonic-types/lib-asset` package as a development dependency using npm, providing TypeScript type definitions for the `lib-asset` library to enable type-safe development. ```groovy npm install --save-dev @enonic-types/lib-asset ``` -------------------------------- ### lib-asset Configuration File Example Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This JSON snippet provides a sample configuration file for the `lib-asset` library, illustrating the default values for properties such as the asset root directory, cache control headers, static compression, and cache busting behavior. ```json { "root": "/assets", "cacheControl": "public, max-age=31536000, immutable", "staticCompress": true, "cacheBust": true } ``` -------------------------------- ### Import assetUrl from lib-asset Types Source: https://github.com/enonic/lib-asset/blob/master/types/README.md Example of importing the `assetUrl` function from the `/lib/enonic/asset` module in a TypeScript file. After proper setup, this import will benefit from the provided type definitions, enabling type checking and autocompletion. ```typescript import {assetUrl} from '/lib/enonic/asset'; ``` -------------------------------- ### Import and Use assetUrl Function Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc These examples demonstrate how to import and utilize the `assetUrl` function from the `lib-asset` library to generate URLs for static assets, showcasing both CommonJS (JavaScript) and ES Module (TypeScript) import syntaxes. ```javascript var assetLib = require('/lib/enonic/asset'); var url = assetLib.assetUrl({path: 'path/to/asset.ext'}); ``` ```typescript import {assetUrl} from '/lib/enonic/asset'; const url = assetUrl({path: 'path/to/asset.ext'}); ``` -------------------------------- ### Add Enonic lib-asset Dependency to Gradle Source: https://github.com/enonic/lib-asset/blob/master/README.md Instructions on how to include the Enonic lib-asset library as a dependency in your Enonic XP project's `build.gradle` file. Remember to replace `` with the specific library version you intend to use, for example, `1.0.0`. ```Groovy dependencies { include 'com.enonic.lib:lib-asset:' } repositories { maven { url 'http://repo.enonic.com/public' } } ``` -------------------------------- ### Import Enonic lib-asset in XP Controllers Source: https://github.com/enonic/lib-asset/blob/master/README.md Examples demonstrating how to import the Enonic lib-asset library into an XP controller using both standard JavaScript `require` syntax and TypeScript `import` statements. This allows access to the library's functionalities within your application logic. ```JavaScript const libAsset = require('/lib/enonic/asset'); ``` ```TypeScript import { assetUrl } from '/lib/enonic/asset'; ``` -------------------------------- ### Configure TypeScript Path Alias for lib-asset Source: https://github.com/enonic/lib-asset/blob/master/types/README.md Configuration snippet for `tsconfig.json` to add a path alias. This maps the `/lib/enonic/asset` import path to the installed type definitions located in `node_modules`, allowing TypeScript to correctly resolve the module. ```json { "compilerOptions": { "paths": { "/lib/enonic/asset": ["node_modules/@enonic-types/lib-asset"] } } } ``` -------------------------------- ### assetUrl Function API Reference Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This section provides a detailed API reference for the `assetUrl` function, outlining its input parameters, their types, attributes (e.g., optional, default values), and a description of each. It also specifies the function's return type. ```APIDOC assetUrl function generates a URL pointing to a static file. Parameters: params: object - Input parameters Properties of params: path: string - Path to the asset type: string (optional, default: server) - URL type. Either server (server-relative URL) or absolute params: object (optional) - Custom parameters to append to the url Returns: string: The generated URL. ``` -------------------------------- ### Add lib-asset Dependency to build.gradle Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This snippet demonstrates how to include the `lib-asset` library as a dependency in your Enonic application's `build.gradle` file, specifying the required library version for compilation and runtime. ```groovy dependencies { include "com.enonic.lib:lib-asset:${libVersion}" } ``` -------------------------------- ### Migrate Thymeleaf Template for Asset URL Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This snippet illustrates the necessary changes in a Thymeleaf template to consume the `assetUrlBase` provided by the controller. It replaces the deprecated `portal.assetUrl` function with a direct concatenation of the base URL and the asset path, ensuring correct asset linking. ```Thymeleaf ``` ```Thymeleaf ``` -------------------------------- ### Configure Tsup to Externalize lib-asset Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This TypeScript snippet illustrates how to mark the `lib-asset` library as an external dependency within your `tsup.config.ts` file, ensuring it is not bundled by Tsup and is instead resolved at runtime. ```typescript external: [ ... '/lib/enonic/asset', ] ``` -------------------------------- ### Migrate Thymeleaf Controller for Asset URL Source: https://github.com/enonic/lib-asset/blob/master/docs/index.adoc This snippet demonstrates how to update a TypeScript controller to correctly pass asset URLs to Thymeleaf templates using `lib-asset`. It shows the transition from directly rendering the view to injecting `assetUrlBase` into the model, enabling the template to construct asset paths. ```TypeScript import {render} from '/lib/thymeleaf'; const VIEW = resolve('./thymeleaf-template.html'); export function get() { const model = {}; return { body: render(VIEW, model) }; } ``` ```TypeScript import {render} from '/lib/thymeleaf'; import {assetUrl} from '/lib/enonic/asset'; const VIEW = resolve('./thymeleaf-template.html'); export function get() { const model = { assetUrlBase: assetUrl({path: ''}) }; return { body: render(VIEW, model) }; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.