Overview
Code component dependencies need to be added in an import map, and their CSS need to be attached. See the parent issue, #3518182: [Plan] First-party code component imports, and the issue to be solved before this one, #3518185: Store imported JS components in `JavaScriptComponent` (and reflect in config dependencies).
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>buttonandcardin the above example areJavaScriptComponentconfig entity machine names. Prepend them with@/components/.- The paths are referencing JS files that are created for
JavaScriptComponentconfig 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.

| Comment | File | Size | Author |
|---|---|---|---|
| #24 | Screenshot 2025-04-23 at 10.56.49 AM.png | 92.5 KB | tedbow |
Issue fork experience_builder-3518191
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
balintbrewsComment #4
wim leersFYI:
\Drupal\experience_builder\Render\ImportMapResponseAttachmentsProcessoradded support for#attached[import_maps], so this should be relatively straightforward.(Introduced by #3507567: Make JS files generated from code components import Preact modules with the correct paths, refined in #3508975: Preview-on-hover in Component Library not loading for code components.)
Comment #5
wim leersRepublishing after d.o unpublished 🙄
Comment #6
tedbow#3518185: Store imported JS components in `JavaScriptComponent` (and reflect in config dependencies) is in!
Comment #8
tedbowStarted but probably still rough. Need to self review more
Comment #9
tedbowI think I need to update this issue based on #3508922: Regression after #3500386: import map scope mismatch when previewed code component's JS is a 307 due to it not having an auto-save/draft because I think introduced the same problem for dependencies. Shouldn't be too hard
Comment #10
tedbowre #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 #3508922: Regression after #3500386: import map scope mismatch when previewed code component's JS is a 307 due to it not having an auto-save/draft
Comment #12
wim leersComment #13
tedbowComment #14
tedbowtest failing
Comment #15
tedbow🤞🏻 tests will pass. passedComment #16
wim leers#3518838: ComponentSource robustness: add `ComponentSourceTestBase::testSettings()` just landed, and now requires this to be rerolled, sorry 😬
Comment #17
tedbowOk. 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-upComment #18
wim leersIn the words of @larowlan:
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
JsComponentclass and its test coverage.Comment #20
wim leersComment #21
balintbrewsWe are missing one smaller piece here. Here is how an import map currently looks like:
@/components/cardis 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/cardis imported, the browser won't know how to mapreact,react/jsx-runtimeetc. imported from that component. To solve that, we need to move all those to the global scope:Comment #22
wim leersThanks for that detailed analysis! 🤩🙏
Comment #24
tedbow@balintbrews I have pushed a new MR that tries to nest the imports like you mentioned
I am not getting this error
$attached['html_head'][] = [$element, 'html_head_link:' . $rel . ':' . $href];because
$hrefis my list ofimportsnot sure why this happening yetComment #26
larowlanAddressed feedback and worked with @balintbrews to confirm it was working
Comment #28
balintbrewsComment #30
effulgentsia commented