Can I control the weight of a library loaded through a theme preprocess function? I have a base theme that loads several libraries, some through the libraries.yml file and some loaded conditionally in a preprocess function. I also have a base theme with its own library loaded through libraries.yml.

This is the order they are loading in:


Libraries loaded in base theme's libraries.yml 
Libraries loaded in sub theme's libraries.yml
Libraries loaded in base theme's preprocess function


This is a problem because I need to override some CSS from the libraries loaded in the base theme's preprocess function in my subtheme. I want it to load like this:


Libraries loaded in base theme's libraries.yml 
Libraries loaded in base theme's preprocess function
Libraries loaded in sub theme's libraries.yml


They are loading in that order regardless of the CSS weights defined in the libraries file (base, theme etc.).

Is there anything I can do about this? This is the relevant code from the base theme, used in hook_preprocess_html().

  if (\Drupal::moduleHandler()-&gt;moduleExists('module')) {
    $variables['#attached']['library'][] = 'basetheme/library';
  }


Will libraries loaded through a preprocess function always load later than the libraries loaded through libraries.yml? Are there any resources that explain this? And as an aside, where would libraries loaded through twig go?
Can you define your subtheme's library as extending or overriding your base theme's library. You'd have to do that in your subtheme's .info.yml. I think that should work. I think if you went this route you would just have a .css file in your subtheme that entends -- gets added whenever -- your basetheme's library is loaded. In this case you're override wouldn't be a "library" in your subtheme. Drupal should be smart enough to load your css file whenever the basetheme's library is loaded.