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

wordpress-enqueue-chunks-webpack-plugin

v0.2.1

Published

A webpack plugin that manages registering and enqueing split chunks in a wordpress environment

Downloads

33

Readme

What

This is a simple webpack plugin that maps all of your entries and split chunks into a manifest. A PHP script is then generated with the manifest and a few other variables. That script can then be used in WordPress projects to easily register any required scripts and it's dependencies without having to keep track of webpack compilation changes.

Why

When utilizing Webpack's splitChunks optimization you won't always know what the final output will be. It can be a pain to keep track of any changes to the ouput and making sure all scripts and it's depedencies are registered in WordPress.

For example, passing the following entries into Webpack could result in the following output depending on how splitChunks is configured:

Entries
  • src/foo.js
  • src/bar.js
Output
  • foo.bundle.js
  • bar.bundle.js
  • vendors-foo.bundle.js
  • common-foo-bar.bundle.js
  • vendors-bar.bundle.js

Making any number of changes in your splitChunks config could result in a totally different compilation result from Webpack. This plugin will help you automate registering these scripts in WordPress without having to know what chunks are required for each script.

How

Require or autoload the generated PHP script in your project. Using the entries and output from above as an example, you can call registerScript(['foo']) and foo.bundle.js along with vendors-foo.bundle.js, common-foo-bar.bundle.js, and any other required chunks will automatically be registered with WordPress for you.

Install

npm i -D wordpress-enqueue-chunks-webpack-plugin

Usage

With Webpack

| Name | Type | Required | Description | |----------------|---------------------|----------|-----------------------------------------------------------------------------------------------| | AssetsDir | {string} | yes | Path to your built scripts | | phpScriptDir | {string} | yes | Path to where you want the generated PHP script to go | | namespace | {string} | no | Prefix added to the handle used when registering scripts with WordPress | | delimiter | {string} | no | Specify a delimiter to be used between the namespace and script name when creating a handle | | context | {plugin \| theme} | no | Defaults to plugin, tells the PHP script how to resolve the absolute path to the built assets |

const { WordPressEnqueueChunksPlugin } = require('wordpress-enqueue-chunks-webpack-plugin');

module.exports = {
  plugins: [
    new WordPressEnqueueChunksPlugin({
      assetsDir: 'path-to-your-built-scripts',
      phpScriptDir: 'where-the-php-script-should-go',
      context: 'theme',
      namespace: 'my-theme-js',
      delimiter: '-',
    }),
  ],
}

With WordPress

use function WordpressEnqueueChunksPlugin\registerScripts;

// register all scripts
registerScripts();

// register specific scripts
registerScripts(['foo', 'bar']);

// args passed to wp_register_script can be filtered for each script
add_filter('wpecp/register/foo', function($args) {
  array_push($args['deps'], 'media-editor', 'jquery');
  return $args;
});

wp_enqueue_script('my-theme-js-foo');