lakedrops/theme-d8-sass

Composer Plugin for Drupal 8 to create a new theme with SASS compiler based on Gulp

v1.6.3 2021-01-21 10:34 UTC

README

A Drupal 8 theme template that works with SASS, Bower, Gulp and any Drupal 8 base theme you want to use. This template also has a self-update feature built in and you should never have to copy/paste anything when setting up a custom theme for a Drupal project in the future.

Usage

Basic installation

By default, this template uses Bartik as the base theme and you can start using it in an existing Drupal project by calling:

composer require lakedrops/theme-d8-sass

This will add this template as a dependency to your composer.json file and create a custom theme named mytheme with all the settings requiered to get started immediately.

Advanced installation

Define the theme name

To define a specific theme name you should simply add the following section to your '.lakedrops.yml':

theme-d8-sass:
  project_name: NAME_YOUR_THEME

It is best practice to add this configuration into the composer.json before you install the theme itself, otherwise you'll end up with two custom themes, one with the default name and a second one with your own theme name.

Define and configure the base theme

To use a different base theme than Bartik you can add the following configuration to your composer.json:

theme-d8-sass:
  base_theme:
    name: bootstrap
    package: drupal/bootstrap
    starterkit: starterkits/sass
    sasspath: scss
    import:
      - overrides
KeyDefaultDescription
namebartikThe Drupal project name of the base theme.
packageThe composer package name with namespace and package name so that Composer is able to find the package in the current installation for additional resources like starterkit. This parameter is required if you want to use any of the following settings and you have to make sure that this package is included as a requirement for your project.
starterkitIf the base theme comes with a starterkit you can specify the relative path within that project where to find it.
sasspathThe created custom theme is always based on a subdirectory called sass for all the sass or scss code. If the starterkit of the base theme uses a different directory name, then specify that here so that we can copy the files from the correct place into our sass directory.
import[]If the starterkit comes with basic files that you should import, then you can provide a list of names that will then be imported automatically.

Note: if you're using bootstrap as your base theme, you will also have to add the bower assets for the Bootstrap library which is explained below. The same may apply for other base themes, you should always find their requirements in their own documentation. Feel free to notify us about configurations on other base themes and we'll be adding that into the documentation here as well.

Adding bower assets

Either a base theme or your own theme development style may require some libraries that are provided by Bower. To add one or more Bower assets to your theme, simply add a section like this to your composer.json:

theme-d8-sass:
  bower_assets:
    bootstrap-sass:
      name: bootstrap
      version: "~3.3.7"
      fonts:
        src: assets/fonts/bootstrap
      sass:
        src: assets/stylesheets
        import:
          - bootstrap
      js:
        src: assets/javascripts

In the above example bootstrap-sass in the package name from the bower package repository and it contains a dictionary of values with these options:

KeyDescription
nameThe name how to identify this asset in your own theme.
versionThis value is optional and defaults to the latest if not provided. Note: if you're using global bower assets (see below), this value here is ignored.
fontsA dictionary containing the source directory where to find the fonts of that package. Those fonts will be copied over into your custom theme.
sassA dictionary containing the source directory where to find the sass file of that package and a list of sass files that will be imported by the sass compiler when required. Those sass file will not be copied to your custom theme, because they will only be required for compilation but not to deliver those files to the browser of your website visitors.
jsA dictionary containing the source directory where to find the javascript files of that package. Those javascript file will be copied over into your custom theme and included into the library that adds them to the sites when required.

A bower.json file will be created dynamically when composer is executed and bower will be executed automatically to install and update all required assets locally in your theme.

Add bower assets globally

The previous chapter describes how to add bower assets to your custom theme and without doing anything else, they will be added locally to your theme by using bower to install and update them.

If you either want to avoid that overhead of using Bower or if any of these assets will be used by multiple projects, then you could install them globally in the vendor directory of your Drupal project. This will be done by composer if you add the following settings to your composer.json in addition to the settings in the previous chapter:

{
  "repositories": [
    {
      "type": "composer",
      "url": "https://asset-packagist.org"
    }
  ],
  "require": {
    "bower-asset/bootstrap-sass": "~3.3.7"
  }
}

Your custom theme will then just use the assets from the vendor directory rather than keeping them in a bower_components subdirectory of the theme. But you still need the bower_assets settings in the extra section as described above.

Other Bower Assets

A short list of bower assets that might be interesting to add them to your theme:

Flat-UI
{
  "require": {
    "bower-asset/flat-ui-sass": "~2.1.3"
  }
}
theme-d8-sass:
  bower_assets:
    flat-ui-sass:
      name: flat-ui
      fonts:
        src: vendor/assets/fonts.flat-ui
      sass:
        src: vendor/assets/stylesheets
        import:
          - flat-ui
      js:
        src: vendor/assets/javascripts
FontAwesome
{
  "require": {
    "bower-asset/fontawesome": "~4.7.0"
  }
}
theme-d8-sass:
  bower_assets:
    fontawesome:
      name: fontawesome
      fonts:
        src: fonts
      sass:
        src: scss
        import:
          - font-awesome
Animate
{
  "require": {
    "bower-asset/animate-sass": "~0.6.0"
  }
}
theme-d8-sass:
  bower_assets:
    animate-sass:
      name: animate
      sass:
        src: ''
        import:
          - animate
        modules:
          - bounceIn

For the animate package you have an additional list called modules which contains a list of animation modules that you want to enable for your custom theme.

Add optional scripts

The custom theme will be updated when ever you either run composer install or composer update on your project. However, sometime you may only want to update the theme without running composer on the full project and you can achieve that by adding some custom scripts to your composer.json file:

{
  "scripts": {
    "drupal-theme-install": "LakeDrops\\Drupal8Theme\\SASS\\Plugin::init",
    "drupal-theme-update": "LakeDrops\\Drupal8Theme\\SASS\\Plugin::update",
    "drupal-theme-reset": "LakeDrops\\Drupal8Theme\\SASS\\Plugin::reset",
    "drupal-theme-overwrite": "LakeDrops\\Drupal8Theme\\SASS\\Plugin::overwrite"
  }
}

You don't need them all, just add those that make sense to your development workflow. To run any of those scripts simply call composer drupal-theme-update which will then run that script.

The available scripts in this package are:

NamePurpose
initCreate or update the theme with all its files and settings. Do not overwrite any custom files and finally run npm and/or bower if required.
updateSame as init just that npm and bower will be called in update mode rather than install mode.
resetSame as init with a prior reset which will remove fonts and javascript files from previously installed bower assets in the theme.
overwriteSame as init but it will also overwrite custom files.

Updating

Simply call composer update in the root of your project and the latest version of this plugin will be updated and the changed files will then be updated in your custom theme. No worries, the files that you edited in your theme and that are not core to this plugin, won't be overwritten, so you keep your own work easily.

At the end of the update, gulp default will be executed which will update your fonts, javascript files and compile your Sass files into css. This is useful for deployment of Drupal sites which will make sure that the theme assets are always up to date.

The sass compiler also knows a development and a production mode. It uses development mode by default and production mode when composer is run with the --no-dev command line argument, which is usually the case in production environments.

Using Gulp

Go to your theme's directory and call gulp once to run all of the available pre-processing. This is the equivalent of calling gulp default.

If Gulp is not installed globally then you need to call node_modules/.bin/gulp in all examples of this chapter.

Gulp by default will run the tasks fonts, js and css which are described in detail below.

Gulp tasks

Fonts

gulp fonts will copy all fonts from all configured bower assets into the fonts subdirectory of your custom theme.

Javascript

gulp js will copy all javascript files from all configured bower assets into the js subdirectory of your custom theme.

CSS

gulp css will compile your sass files into css once. When adding the argument --env production then the output will be optimized for production environments, otherwise the output will be optimized for development.

gulp watch wiwll start a process that monitors your sass directory and automatically calls the css compilation when ever one or more files have changed.

Examples

This template is embedded in our D8 Project Template and looking into the composer.json there shows you all the options in action.