JavaScriptComponent CSS libraries should depend on AssetLibrary libraries

Created on 11 June 2025, 12 days ago

Overview

If I have a code component with CSS using a variable defined in global CSS, it isn't available. That's because the AssetLibrary CSS library is one o the last stylesheets added.

Proposed resolution

Make all code component libraries depend on all AssetLibrary CSS libraries

Idea within \Drupal\experience_builder\Hook\LibraryHooks::libraryInfoBuild

    foreach (AssetLibrary::loadMultiple() as $library_id => $library) {
      $library_name = "asset_library." . $library->id();
      if ($library->hasCss()) {
        $libraries[$library_name]['css']['theme'][$library->getCssPath()] = [];
        $asset_library_names[] = $library_name;
      }
....
    foreach (JavaScriptComponent::loadMultiple() as $component_id => $component) {
      $library_name = "astro_island." . $component_id;
      if ($component->hasCss()) {
        $libraries[$library_name]['css']['component'][$component->getCssPath()] = [];
        $libraries[$library_name]['dependencies'] = array_map(static fn($item) => 'experience_builder/' . $item, $asset_library_names);

User interface changes

๐Ÿ› Bug report
Status

Active

Version

0.0

Component

Theme builder

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States mglaman WI, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @mglaman
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    Giving it a try on this.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    Hi @mglaman,

    Could you please share the steps to reproduce this issue?

    Below are the steps I followedโ€”please let me know if they are correct or if Iโ€™m missing something. The CSS appears to be getting applied, but Iโ€™m not sure if Iโ€™m approaching it the right way. Iโ€™d appreciate your guidance on this.

    Thanks!

    1. I created a new component in the XB page - Test Component
    2. Added a Title prop of type text
    3. In the code editor - global css - I declared a variable
      :root {
        --brand-color: #0074D9;
      }
    4. In css section and used the variable as text color
      .test-box {
          color: var(--brand-color);
        }
    5. last, I used the .test-box is js code.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    I again tried but couldn't reproduce the issue. In the last comment I forgot to attach the screenshot so I have attached it now!

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ

    The problem/challenge here is that it's possible that both the code component (JavaScriptComponent config entity)'s and the AssetLibrary config entity (only a single one allowed for now, with the ID global) can have a draft (auto-saved) version.

    So dependencies don't quite end up working, because it depends on the context which should be loaded:

      private static function shouldLoadAssetFromAutoSave(AutoSaveData $autoSave, bool $isPreview) : bool {
        return $isPreview && !$autoSave->isEmpty();
      }
    

    โ€” \Drupal\experience_builder\Entity\JavaScriptComponent::shouldLoadAssetFromAutoSave()

    isPreview will be true when editing inside XB, not outside of it, and an auto-save entry can (dis)appear at any time. Similar thing for the global asset library:

    \Drupal\experience_builder\Hook\ComponentSourceHooks::pageAttachments
    

    โ€”

    \Drupal\experience_builder\Hook\ComponentSourceHooks::pageAttachments()    // Load the auto-save/draft version of the global asset library in the XB UI.
        $global_asset_library = AssetLibrary::load(AssetLibrary::GLOBAL_ID);
        assert($global_asset_library instanceof AssetLibrary);
    
        $auto_saved_global_asset_library = NULL;
        $auto_save = $this->autoSaveManager->getAutoSaveData($global_asset_library);
        if (!$auto_save->isEmpty()) {
          \assert($auto_save->data !== NULL);
          $auto_saved_global_asset_library = $global_asset_library->forAutoSavePreview($auto_save->data);
        }
        $page['#cache']['tags'][] = AutoSaveManager::CACHE_TAG;
        $page['#attached']['library'][] = $auto_saved_global_asset_library !== NULL ?
          'experience_builder/asset_library.' . AssetLibrary::GLOBAL_ID . '.draft' :
          'experience_builder/asset_library.' . AssetLibrary::GLOBAL_ID;
    
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ
  • Pipeline finished with Running
    5 days ago
    #525909
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India
  • Pipeline finished with Failed
    5 days ago
    Total: 615s
    #525969
  • Pipeline finished with Failed
    5 days ago
    Total: 559s
    #525982
  • Pipeline finished with Failed
    5 days ago
    #525984
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ

    However โ€ฆ the work I did on ๐Ÿ› Regression after #3500386: import map scope mismatch when auto-saved code component's JS sends a 307 Active gave me an idea:

    1. let ApiConfigAutoSaveControllers::getCss() and ApiConfigAutoSaveControllers::getJs() do the disambiguation: return auto-save data if it exists, return stored/live data if not
    2. always load the "draft" inside XB, even if no auto-save entry exists
    3. โ€ฆ which means I can create a "draft" asset library for the code component, which can always depend on the "draft" asset library for the global asset library
    4. This should work?

    Can you test this, @mglaman? Quick manual test suggests it works fine. ๐Ÿ˜Š

  • Pipeline finished with Failed
    5 days ago
    Total: 927s
    #526022
  • Pipeline finished with Failed
    5 days ago
    Total: 827s
    #526042
  • Pipeline finished with Success
    5 days ago
    #526104
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    Just a couple of minor nits about the use of the verbose match for booleans

  • Pipeline finished with Success
    4 days ago
    Total: 871s
    #526844
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ

    Only +1s from @larowlan, so going ahead and RTBC'ing :)

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ
  • Pipeline finished with Failed
    4 days ago
    Total: 1077s
    #526918
  • Pipeline finished with Skipped
    4 days ago
    #526945
  • Pipeline finished with Skipped
    4 days ago
    #526946
  • Pipeline finished with Skipped
    4 days ago
    #526947
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ
Production build 0.71.5 2024