2

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)?

3

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) {
  if (PRODUCTION) {
    $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;
            break;
          case 'stage.anwalt-seiten.de':
            $ds = AsdentBaseService::DS_STAGE;
            break;
          default:
            $ds = AsdentBaseService::DS_DEVELOPMENT;
            break;
        }
      } 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.

  • if (getenv('MYENVFOOBAR') === 'production') ... – leymannx Nov 12 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 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 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 at 22:31
  • Use another theme for admin at the bottom of /admin/appearance – Rainer Feike Nov 20 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.