Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebAsset dependencies and cross-dependencies #218

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/general-concepts/web-asset-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,61 @@ $wa->usePreset('bar');
$wa->registerAndUsePreset('bar','', [], [], ['core#script', 'bar#script']);
```

## Asset dependencies and cross dependencies

Managing dependencies is an important part of WebAsset API. This allows easily pre-define when and which asset should be loaded.
Instead of writing down all required asset in to the rendering layout it is easier to write only a main asset, and rest of dependencies will be picked up by WebAssetManager.

Dependency types:
- `dependencies` Contain list of assets of the same type on which current asset depend on. All items from this list will be attached to Document and taken into account while calculating sorting for assets of parent type.
- `crossDependencies` Contain associative list of assets of another type on which current asset depend on, grouped by type. All items from this list will be attached to Document, however they will not be taken into account while calculating sorting for assets of parent type. Also note, cross dependencies cannot contain an assets of the same as parent type, example asset of type `script` cannot have `script` in `crossDependencies`, it will be ignored.

When one of requested dependency are not available then exception will be thrown.

Example of mixed dependencies:

```json
{
"name": "foo",
"type": "style",
"uri": "foo.css"
},
{
"name": "bar",
"type": "style",
"uri": "bar.css"
},
{
"name": "bar",
"type": "script",
"uri": "bar.js"
"crossDependencies": {
"style": ["bar"]
}
},
{
"name": "foo",
"type": "script",
"uri": "foo.js",
"dependencies": ["bar"]
"crossDependencies": {
"style": ["foo"]
}
},
```

After enabling `foo` script with `$wa->useScript('foo')` WebAssetManager will enable all 4 assets as dependencies.

:::note Note

As was written before, the asset of type `presets` treat `dependencies` differently.
:::

:::note Note

Avoid the circular dependencies, WebAssetManager will ignore these assets when will detect a loop.
:::

## Advanced: Custom WebAssetItem class

The default class for all WebAsset items is `Joomla\CMS\WebAsset\WebAssetItem`.
Expand Down
6 changes: 6 additions & 0 deletions migrations/50-51/new-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ For more detail check [Joomla Dialog (popup) script](https://manual.joomla.org/d
PR: https://github.com/joomla/joomla-cms/pull/40150


#### Support of cross dependencies in WebAssetManager

It is now possible to define cross dependencies for assets.
Check [Asset dependencies and cross dependencies](/docs/general-concepts/web-asset-manager#Asset-dependencies-and-cross-dependencies) for more detail.

PR: https://github.com/joomla/joomla-cms/pull/42681