Is it possible to include CSS from a vendor directory? I need to include a CSS artifact from a Pattern Lab dependency.

Is it best to copy the artifact into the Drupal theme and just reference it that way?


It's probably best to copy it, yes. Drupal isn't set up to serve any assets from the vendor/ folder, and actively prevents it by default with an .htaccess file. Also, all assets are normally handled via "libraries", which are defined by a module and whose JS/CSS files must be in the module folder.

You could change the .htaccess and insert a direct style element with /vendor/.../xyz.css into the page, but at that point it's probably a lot cleaner and more secure to copy it.

If it changes often and you want to avoid manually copying/committing it every time, you might look at automating this with a build script (eg. with webpack).

  • Alright, I can easily just copy it with a shell script in the build tasks, just wondered if Drupal could read a directory like that from the theme. – Kevin Nov 27 '17 at 18:04

If you are wanting to add a CSS file from an external source you can use Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme.

As part of our development process we use Gulp to take all the files (regardless of folder location) and aggregate them into a single minified CSS file so having a vendor/ folder where you take the minified CSS files and include them is totally doable. If the CSS file you are placing locally in your subtheme is not minified, you can Gulp to minify that as well.

Read through the different CSS properties to see what best fits your needs.

  • Its not exactly external, its outside of the docroot in the vendor folder. Drupal prohibits looking there by design. I simply added a copy command to the end of my build script. – Kevin Nov 27 '17 at 20:27
  • @Kevin is there not an option to move it into your theme folder or sites/default/files folder? Opening up permissions adds overhead and have you thought about when security bugs are released to fix permissions in Drupal, how the .htaccess file would impact that? – usernameabc Nov 28 '17 at 20:18
  • No, because it is compiled externally. Long story. This is coming from a private pattern lab repo, not a Drupal or PHP package. The shell script copies that folder in which contains only css/js/svg assets from our own source. – Kevin Nov 28 '17 at 21:37

I just ran into this issue, and as I wasn't able to find a solution for it, I created the Vendor Stream Wrapper module, which creates a vendor:// stream wrapper that serves files from the vendor folder, even if it is outside the webroot. The module allows for using the vendor:// stream wrapper in *.libraries.yml files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.