Closed (outdated)
Project:
Components!
Version:
3.x-dev
Component:
Code
Priority:
Major
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
17 Apr 2016 at 08:09 UTC
Updated:
22 Nov 2025 at 19:07 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
johnalbinSee \Drupal\Core\Asset\LibraryDiscoveryCollector::getLibraryDefinitions() and \Drupal\Core\Asset\LibraryDiscoveryParser::buildByExtension().
Comment #3
cyb_tachyon commentedThis functionality has been built out in a small way for Progressively Decoupled blocks here: https://github.com/mrjmd/pdb/issues/5#issuecomment-218887118
We're thinking it would be a good idea to fully genericize that code and pull out that functionality into the components module and add a dependency for components to PDB.
Comment #4
wim leersWe should be able to port what #2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering) provides to this module. Then a component can easily define asset libraries
Comment #5
wim leersComment #6
wim leersThis is as close to a 1:1 port of the logic/patch in #2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering) as possible.
Differences:
hook_library_info_alter()instead of modifying\Drupal\Core\Asset\LibraryDiscoveryParserComponentDiscovery.phpclass, but doesn't actually use it for component discovery; only uses it for asset-library-for-a-component discoveryComment #7
wim leers#6 assumes that all components live at
/path/to/extension/components, i.e. in acomponentssubdirectory.But the
componentsmodule currently has the concept ofcomponent-libraries: any extension can include multiple component libraries. FromSTARTERKIT.info.yml:This would be fine to support, except that this is then used inconsistently:
STARTERKIT/templatescontains directories which contain Twig templates (plus metadata): single levelSTARTERKIT/componentscontains directories which contain directories which contain Twig templates (plus metadata): multiple levelsSo, it's not clear how this ought to behave. It's also not clear why "templates" are considered to be a component library.
Consequently, the patch in #6 does not work with STARTERKIT, because it contains multiple levels in
STARTERKIT/components, whereas #2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering) was designed to only allow a single level for simplicity.Comment #8
wim leersThis adds support for up to two levels of directories inside the
componentsdirectory. IOW: this allowscomponents/components/boxrather than onlycomponents/box. So this means you are allowed one extra level of directories for organization purposes.For example, the STARTERKIT theme uses
components/layouts/somethingcomponents/components/somethingcomponents/containers/somethingComment #9
wim leersThis remains true. IMO this is a design flaw in the current "components" module. Removing this would also mean we can remove the need for this:
Opened #2834748: Remove confusing 'component-libraries' key in THEME.info.yml to fix that.
Comment #10
wim leersThis then updates Zen's STARTERKIT theme to take advantage of this patch; it removes the need to define asset libraries in
STARTERKIT.libraries.yml.Comment #11
wim leersStill to do:
components/components/box/box.scssand then has its CSS file atcomponents/asset-builds/css/box.css). This violates the principle of self-containedness.\Drupal\components\Theme\ComponentDiscovery:\Drupal\components\Template\Loader\ComponentLibraryLoader(because it provides only a subset of what\Drupal\components\Theme\ComponentDiscoveryprovides)Comment #12
rainbowarrayThere are a lot of reasons why a front-end developer might want the production CSS in a separate folder from the source component. That's sometimes necessary in order to sync CSS with a style guide/pattern library for example. Requiring production CSS files to be in a certain location seems problematic to me. There may also be cases where CSS does live in the component folder and that works well. I'd just be reluctant to set a hard and fast rule with this.
Comment #13
mariohernandez commentedTotally agree with @mdrummond. Front-end is constantly changing and having the flexibility to specify a location for source CSS or JS is a plus.
Comment #14
briangervais commentedThe 2707849-8.patch patch allowed for libraries to be attached directly from a component template, which is awesome. Also agree with @mdrummond and @mariohernandez that it is preferable to have the flexibility to specify a destination outside of a component for a compiled CSS or JS file.
Comment #15
pixelwhip commentedIs the idea here that each individual component will have its own [component].libraries.yml file or would there be one [component-library].libraries.yml per component library namespace? Or am I misunderstanding the intent altogether?
Comment #16
cyb_tachyon commentedPersonally I like this approach - I'm tinkering with auto-ingesting components as blocks and using a block plugin to expose them to Presenter Mapping inside Drupal as well as Outside-In support. This would make these sorts of experiments easier IMHO.
@pixelwhip
I believe this is one of the solutions we are proposing here, similar to how https://www.drupal.org/project/pdb handles things.
This is closer to what we're currently doing - one or more component libraries per theme/module that exists in a Twig namespace as defined in that theme/module's
{{ extension }}.libraries.yml.Comment #17
jacineHowdy!
I'm confused about the
extendskey... I see in the patch, extends gets set to false for modules, and true for themes by default, and yet, in this Zen patch, it's false.What exactly does it facilitate? Is it whether or not a component template can be overridden? Does it have anything to do with Twig's extends tag?
Thanks :)
Comment #18
aleksipI have just released a module that enables component-specific
.libraries.ymland.layouts.ymlfiles: https://www.drupal.org/project/multiple_definition_filesComment #19
robloachWhoa! It's been more than 2 years. Would ya'll like me to commit this?
Comment #20
cyb_tachyon commentedI think there are two remaining items that Wim mentioned which need to be worked before committing:
Most of my efforts have been focused on PatternKit, which ingests component libraries into blocks and provides mappings for them. Similar to UI_Patterns, but more enterprise grade and no schema modifications.
Comment #21
johnalbinComment #22
johnalbinNew features will need to go in Components 3.x.
Comment #23
johnalbinDrupal core now has FrontMatter. That may come in useful for defining JS and CSS files for a Twig file. See #3203523: Investigate FrontMatter integration
Comment #24
youngelpaso commentedI'm with @mdrummond as well on this. By default, sure have the CSS/JS (and I think images) co-located next to *.twig but have the option to look elsewhere (i.e. wherever your SCSS/Less/PostCSS is built to). There's a good chance someone uses a pre-processor or a utility CSS file that's common to multiple components, but should only be referred to or 'imported' per component when necessary. Even more true for JavaScript dependencies.
Frontmatter might work.
However, since this module provides the ability to use custom namespaces, is there any plans for using those? It would be great if there was a general asset-loader Twig function/YAML definition, whatever the implementation that gells with the '@foobar/foobar.twig' format.
That might be another issue, though I think it's very closely related. In a sense this is all about asset management and Symfony etc extends Twig to better handle assets of any kind using namespaces, perhaps that might be inspirational?
TLDR: why not use a custom Twig function combined w/ namespaces like so?
Comment #25
mxtI'm wondering if it would be possible to simply copy / take as inspiration the ui_patterns code which already now allows you to declare the libraries for each component?
ui_patterns allows you to write the component's yml inside the component folder itself (which also contains all component's assets: css/js/imgs/twig...), and here's how you can declare the component libraries (just an example):
../my-component-folder/my-component-name.ui_patterns.yml
Comment #26
asherry commentedI'm not sure about the status of patch #8, but, I did my best to re-roll it for version 3.x using Drupal 9.
Comment #28
andypostComment #29
finnsky commentedHello! Added patch with usage of Frontmatter for defining layouts and libraries.
https://www.drupal.org/project/drupal/issues/3075427#comment-14259227
Comment #30
cobadger commentedRe-rolled the patch from comment 26 for Drupal 10 compatibility, addressing "app.root and site.path services have been converted to container parameters".
Comment #32
robloachNice work, all. Patch applies well. I like where this is going, but the new asset library integration is missing some tests. Would it be possible to add a small check to see if library assets are applied?
For those who prefer git over .patches:
Comment #33
johnalbinDrupal 10.3.0 was released on 21 Jun 2024 and includes SDC, which implements all of this better, IMO.
Thank you for all your hard work. I'm going to credit all of you and close the issue. Feel free to re-open if you have strong opinions that this should still go in.