Using Gulp I create two css files. One style.css and one style.min.css. Is it possible to load the style.css when developing (locally) and the style.min.css when in production (remote)?


You can specify two different libraries in mymodule.libraries.yml. Then in your mymodule.module file attach the one that matches your current deployment env:

function mymodule_page_attachments(array &$attachments) {
    $attachments['#attached']['library'][] = 'mymodule/lib_prod';
  } else {
    $attachments['#attached']['library'][] = 'mymodule/lib_dev';

I am using code like this to decide on which deployment level I am:

  public static function getDeploymentStage() : string {
    // lazy cache
    static $ds = null;

    if (null == $ds) {
      if (isset($_SERVER['SERVER_NAME'])) {
        switch ($_SERVER['SERVER_NAME']) {
          case 'anwalt-seiten.de':
          case 'www.anwalt-seiten.de':
            $ds = AsdentBaseService::DS_PROD;
          case 'stage.anwalt-seiten.de':
            $ds = AsdentBaseService::DS_STAGE;
            $ds = AsdentBaseService::DS_DEVELOPMENT;
      } else {
        $ds = AsdentBaseService::DS_DEVELOPMENT;

    return $ds;

Note that any drush command will run in dev-mode here. You can also use $settings from settings.php or any environment variable instead.

| improve this answer | |
  • if (getenv('MYENVFOOBAR') === 'production') ... – leymannx Nov 12 '19 at 16:17
  • Thanks it works. I used the base URL to check if its in running local or remote. Does this have any negative effect on the performance? Do the files get loaded later because of the hook or the way I'm figuring out it is local or remote? – Joost Nov 15 '19 at 14:58
  • Don't think there's any impact on performance (use caching!) - and also there's no change in processing order by calling that hook. – Rainer Feike Nov 16 '19 at 9:52
  • One more question, I noticed when using this. The attached css also overwrites styling on my admin pages. Is there a way to have this only work on none admin pages? – Joost Nov 18 '19 at 22:31
  • Use another theme for admin at the bottom of /admin/appearance – Rainer Feike Nov 20 '19 at 12:01

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.