npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

drupal-libraries-webpack-plugin

v3.0.0

Published

Generates a Drupal asset library file for a webpack build.

Downloads

837

Readme

Build Status Coverage Status npm version Greenkeeper badge

This plugin generates a Drupal asset library file based on a webpack build.

It requires webpack 5 to work. For webpack 4 use the 1.x version of this plugin.

npm install --save-dev drupal-libraries-webpack-plugin

webpack.config.js

const DrupalLibrariesPlugin = require('drupal-libraries-webpack-plugin');

module.exports = {
  plugins: [
    new DrupalLibrariesPlugin()
  ],
};

By default, when you compile webpack, the drupal library file will be generated to 'webpack.libraries.yml'.

The plugin will automatically figure out what libraries need to depend on other libraries based on the final webpack chunks.

You can explicitly add a Drupal library dependency to module by using a special require statement:

require('@drupal(core/jquery)')

Drupal libraries must be added as external dependencies in your webpack configuration:

externals: {
  '@drupal(drupal/core)': 'Drupal'
}

Configuration

path

Type: String|Function

Default: 'webpack.libraries.yml'

Specifies a custom public path for the target webpack file relative to the process working directory.

Minimal example

webpack.config.js

module.exports = {
  plugins: [
  	new DrupalLibrariesPlugin({
  	  path: 'my-theme-name.libraries.yml',
  	})
  ],
};

Advanced Example

Split a library into multiple library files.

module.exports = {
  plugins: [
  	new DrupalLibrariesPlugin({

  	  path: (library, metadata) => {
  	    const lib1 = new DrupalLibraryFile('a.libraries.yml'),
  	    	lib2 = new DrupalLibraryFile('webpack.libraries.yml')

  	    Object.keys(library).forEach(name => {
  	      if (name == 'a') {
  	        lib1.add(name, library, metdata)
  	      }
  	      else {
  	        lib2.add(name, library, metadata)
  	      }
  	    })

  	    return [lib1, lib2]
  	  }
  	})
  ],
};

nameGenerator

Type: Function

Default: nameGenerator

Generates a library name for a chunk.

Minimal example

webpack.config.js

module.exports = {
  plugins: [
  	new DrupalLibrariesPlugin({
  	  nameGenerator: chunk => chunk.hash
  	})
  ],
};

requirePattern

Type: RegExp

Default: /^@drupal\(([^\)]+)\)$/

The pattern to use for detecting drupal library dependencies.

module.exports = {
  plugins: [
  	new DrupalLibrariesPlugin({
  	  // Only pick up require('jquery') or require('Drupal') statements.
  	  requirePattern: /^(jquery|Drupal)$/
  	})
  ],
};

prepareFile

Type: Function

Default: DrupalLibrariesPlugin.defaults.prepareFile

Prepares a library file that is about to be written.

webpack.config.js

module.exports = {
  plugins: [
  	new DrupalLibrariesPlugin({
  	  prepareFile: (file, compiler, compilation) => {
  	    // Add an extra entry to the file when outputting
  	    file.content['external'] = {
  	      remote: 'https://external-library.js',
  	    }
  	    return DrupalLibrariesPlugin.defaults.prepareFile(file, compiler, compilation)
  	  },
  	})
  ],
};

libraryEntryGenerator

Type: DrupalLibraryEntryGenerator

Default: DrupalLibraryEntryGenerator

Generates a flat Drupal library entry as a javascript object from a DrupalLibraryMetadata object.

webpack.config.js

class StaticVersionLibraryGenerator extends DrupalLibraryEntryGenerator {
  constructor(version) {
    this.version = version
  }

  async versionGenerator(metadata) {
    return this.version
  }
}

module.exports = {
  plugins: [
  	new DrupalLibrariesPlugin({
  	  libraryEntryGenerator: new StaticVersionLibraryGenerator('2.0'),
  	})
  ],
};