Creating a Javascript or Sass Library

A javascript or sass library package is a package that can contain:

  1. Utility functions
  2. Variable Definitions
  3. Polyfills
  4. Javascript Behaviors
  5. Mixins
  6. Other Shared Code

Definition

A library package is a package that does not have its own entrypoint. It only exists as a dependency for other packages.

1. Create the package folder

See the creating a new package section.

Any new library package must be created in the packages/libraries directory of the default active drupal theme.

The folder name should follow the rules in naming a package.

2. Create a package.json file in the folder

Change to the directory you just created and run yarn init as described in the creating a new package section.

Pay careful attention to the rules for the name entry.

Ways to Reference your new Library

Lets say you created a component @my_subtheme/my-library.

As a Javascript Dependency

const library = require('@my_theme/my-library')

As a Sass Dependency

@forward '@my_theme/my-library';