[PP-1] Build import maps for code component dependencies, attach their CSS

Created on 9 April 2025, about 1 month ago

Overview

Code component dependencies need to be added in an import map, and their CSS need to be attached. See the parent issue, 🌱 [Plan] First-party code component imports Active , and the issue to be solved before this one, ✨ Store and calculate dependencies in `JavaScriptComponent` Active .

Proposed resolution

Import maps

\Drupal\experience_builder\Plugin\ExperienceBuilder\ComponentSource\JsComponent already builds an import map for the common JS dependencies. Extend it by using the dependencies of the corresponding JavaScriptComponent entity.

Example import map:

<script type="importmap">  
  {  
    "imports": {  
      "@/components/button": "/sites/default/files/astro-island/8RXxcDE5c1RVLOC4Mvm-ZWvUTumW_bALDVy19e8J4EA.js",  
      "@/components/card": "/sites/default/files/astro-island/S_ROf_ktZmcl4uOJv12hB_yztlx_ZztgalZAedDRCqc.js",  
    }  
  }  
</script>
  • button and card in the above example are JavaScriptComponent config entity machine names. Prepend them with @/components/.
  • The paths are referencing JS files that are created for JavaScriptComponent config entities, containing the JS source code. Make sure these paths are pointing at the endpoint that serves the auto-saved version if that exists.

Attaching CSS

Attach CSS from the loaded dependencies using the generated libraries (named 'experience_builder/astro_island.' . $component→id() by experience_builder_library_info_build()).

User interface changes

None.

✨ Feature request
Status

Postponed

Version

0.0

Component

Page builder

Created by

πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL

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

Merge Requests

Comments & Activities

  • Issue created by @balintbrews
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA
  • πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    FYI: \Drupal\experience_builder\Render\ImportMapResponseAttachmentsProcessor added support for #attached[import_maps], so this should be relatively straightforward.

    (Introduced by πŸ“Œ Make JS files generated from code components import Preact modules with the correct paths Active , refined in πŸ› Component preview not loading for JS components Active .)

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Republishing after d.o unpublished πŸ™„

  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA
  • Merge request !915#3518191 build import maps and attach css β†’ (Merged) created by tedbow
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    Started but probably still rough. Need to self review more

  • Pipeline finished with Failed
    25 days ago
    Total: 1619s
    #476166
  • Pipeline finished with Success
    24 days ago
    Total: 2235s
    #476707
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    I think I need to update this issue based on πŸ› Regression after #3500386: import map scope mismatch when auto-saved code component's JS sends a 307 Active because I think introduced the same problem for dependencies. Shouldn't be too hard

  • Pipeline finished with Success
    24 days ago
    Total: 1801s
    #476785
  • Pipeline finished with Canceled
    24 days ago
    Total: 464s
    #476818
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    re #9 it is not only use the draft version of the libraries and JS files if there is an auto-save for the dependency. Hope @wim leers can confirm that since he worked on πŸ› Regression after #3500386: import map scope mismatch when auto-saved code component's JS sends a 307 Active

  • Pipeline finished with Failed
    24 days ago
    Total: 1804s
    #476825
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
  • Pipeline finished with Canceled
    20 days ago
    Total: 1426s
    #479370
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA
  • Pipeline finished with Running
    20 days ago
    #479416
  • Pipeline finished with Failed
    20 days ago
    Total: 385s
    #479407
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    test failing

  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    🀞🏻 tests will pass

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    πŸ“Œ ComponentSource robustness: add `ComponentSourceTestBase::testSettings()` Active just landed, and now requires this to be rerolled, sorry 😬

  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    Ok. ready for review again. There still isn't test coverage for the 3 new public methods added to JavaScriptComponent. They are test indirectly. so we could add the tests in a follow-up

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    In the words of @larowlan:

    Looking great, that refactor was the bomb @tedbow πŸ’₯

    Glad I asked for one more iteration round, this is now solid, and easy to evolve in the future πŸ‘

    Changing to , because 99% of the work here is in the JsComponent class and its test coverage.

  • Pipeline finished with Success
    20 days ago
    #480129
  • Pipeline finished with Skipped
    20 days ago
    #480150
  • Pipeline finished with Skipped
    20 days ago
    #480151
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
  • πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL

    We are missing one smaller piece here. Here is how an import map currently looks like:

    {
      "scopes": {
        "/sites/default/files/astro-island/z2TNPL8SBbhdCntH5L8-nUxBDIJKHG-QMqYOC8xazRY.js": {
          "preact": "/modules/experience_builder/ui/lib/astro-hydration/dist/preact.module.js",
          "preact/hooks": "/modules/experience_builder/ui/lib/astro-hydration/dist/hooks.module.js",
          "react/jsx-runtime": "/modules/experience_builder/ui/lib/astro-hydration/dist/jsxRuntime.module.js",
          "react": "/modules/experience_builder/ui/lib/astro-hydration/dist/compat.module.js",
          "react-dom": "/modules/experience_builder/ui/lib/astro-hydration/dist/compat.module.js",
          "react-dom/client": "/modules/experience_builder/ui/lib/astro-hydration/dist/compat.module.js",
          "clsx": "/modules/experience_builder/ui/lib/astro-hydration/dist/clsx.js",
          "class-variance-authority": "/modules/experience_builder/ui/lib/astro-hydration/dist/class-variance-authority.js",
          "tailwind-merge": "/modules/experience_builder/ui/lib/astro-hydration/dist/tailwind-merge.js",
          "@/lib/utils": "/modules/experience_builder/ui/lib/astro-hydration/dist/utils.js",
          "@/components/card": "/sites/default/files/astro-island/e7KF9ds1KTO6BgcK5ocVIBs0AwihvLBL6dq9fxvnwxI.js"
        }
      }
    }
    

    @/components/card is brought in as a dependency of another component. So far so good! πŸ‘πŸ»

    Now, because everything in the import map is scoped under the individual components, when the code from @/components/card is imported, the browser won't know how to map react, react/jsx-runtime etc. To solve that, we need to move all those to the global scope:

    {
      "imports": {
        "preact": "/modules/experience_builder/ui/lib/astro-hydration/dist/preact.module.js",
        "preact/hooks": "/modules/experience_builder/ui/lib/astro-hydration/dist/hooks.module.js",
        "react/jsx-runtime": "/modules/experience_builder/ui/lib/astro-hydration/dist/jsxRuntime.module.js",
        "react": "/modules/experience_builder/ui/lib/astro-hydration/dist/compat.module.js",
        "react-dom": "/modules/experience_builder/ui/lib/astro-hydration/dist/compat.module.js",
        "react-dom/client": "/modules/experience_builder/ui/lib/astro-hydration/dist/compat.module.js",
        "clsx": "/modules/experience_builder/ui/lib/astro-hydration/dist/clsx.js",
        "class-variance-authority": "/modules/experience_builder/ui/lib/astro-hydration/dist/class-variance-authority.js",
        "tailwind-merge": "/modules/experience_builder/ui/lib/astro-hydration/dist/tailwind-merge.js",
        "@/lib/utils": "/modules/experience_builder/ui/lib/astro-hydration/dist/utils.js",
      },
      "scopes": {
        "/sites/default/files/astro-island/z2TNPL8SBbhdCntH5L8-nUxBDIJKHG-QMqYOC8xazRY.js": {
          "@/components/card": "/sites/default/files/astro-island/e7KF9ds1KTO6BgcK5ocVIBs0AwihvLBL6dq9fxvnwxI.js"
        }
      }
    }
    
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Thanks for that detailed analysis! πŸ€©πŸ™

  • Merge request !934Resolve #3518191 "Scope fix" β†’ (Merged) created by tedbow
  • πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

    @balintbrews I have pushed a new MR that tries to nest the imports like you mentioned

    I am not getting this error

    Warning: Array to string conversion in
    Drupal\Core Render Htm|ResponseAttachmentsProcessor->process-Htm|HeadLink() (line 416 of core/lib/Drupal/Core/Render/Htm/Respon-seAttachmentsProcessor.php).

    $attached['html_head'][] = [$element, 'html_head_link:' . $rel . ':' . $href];

    because $href is my list of imports not sure why this happening yet

  • Pipeline finished with Canceled
    19 days ago
    Total: 1113s
    #480365
  • Pipeline finished with Canceled
    19 days ago
    Total: 903s
    #480387
  • Pipeline finished with Failed
    19 days ago
    Total: 1883s
    #480437
  • Pipeline finished with Failed
    19 days ago
    Total: 1683s
    #480520
  • Pipeline finished with Failed
    19 days ago
    Total: 1553s
    #480581
  • Pipeline finished with Failed
    19 days ago
    Total: 531s
    #480644
  • Pipeline finished with Failed
    19 days ago
    Total: 1762s
    #480663
  • Pipeline finished with Failed
    19 days ago
    Total: 856s
    #480707
  • Pipeline finished with Failed
    19 days ago
    Total: 1816s
    #480715
  • Pipeline finished with Failed
    19 days ago
    Total: 1986s
    #480738
  • Pipeline finished with Failed
    19 days ago
    Total: 1746s
    #480766
  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Addressed feedback and worked with @balintbrews to confirm it was working

  • Pipeline finished with Failed
    19 days ago
    Total: 2734s
    #480781
  • Pipeline finished with Skipped
    19 days ago
    #480794
  • Pipeline finished with Skipped
    19 days ago
    #480796
  • πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024