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

@kunoichi/grab-deps

v1.3.1

Published

Grab dependencies from js/css files.

Downloads

539

Readme

grab-deps

WordPress library to extract dependencies information from js/css files.

TEST

This library dump wp-dependencies.json which includes dependencies and path information about js/css.

  • You don't have to specify dependencies from php files.
  • You can automate the registration & enqueue of assets.

Example

Suppose that you have assets/js/app.js in your theme folder.

/*!
 * My Plugin main JS
 * 
 * @handle my-plugin-app
 * @version 2.1.0
 * @footer false
 * @deps jquery, jquery-masonry, wp-i18n
 */
console.log( 'This script runs jQuery Masonry.' );

And you can get setting file wp-dependencies.json like this.

[
  {
    "handle": "my-plugin",
    "version": "2.1.0",
    "path": "assets/js/app.js",
    "hash": "5e84fd5b5817a6397aeef4240afeb97a",
    "deps": [ "jquery", "jquery-masonry", "wp-i18n" ],
    "ext": "js",
    "footer": true,
    "media": "all"
  }
]

Now you can bulk register assets through php.

add_action( 'init', function() {
    // Load setting as array.
    $settings = json_decode( file_get_contents( __DIR__ . '/wp-dependencies.json' ), true );
    // Register each setting.
    foreach ( $settings as $setting ) {
        $handle  = $setting['handle'];
        $version = $setting['hash']; // You can also specify @version
        $url     = get_template_directory_uri() . '/' . $setting['path'];
        if ( 'js' === $setting['ext'] ) {
            // Register JavaScript.
            wp_register_script( $handle, $url, $deps, $version, $setting['footer'] );
        } else {
            // This is CSS.
            wp_register_style( $handle, $url, $deps, $version, $setting['media'] ); 
        }
    }
} );

Now you can enqueue any of your scripts/styles with wp_enqueue_script( 'my-app-js' ) or wp_enqueue_style( 'my-blocks-alert-css' ).

Supported Header Info

| Name | Default | type | Target | |----------|----------------------------------|---------|--------| | @version | 0.0.0 | String | both | | @handle | Base file name without extension | String | both | | @deps | Empty | Array | both | | @footer | True | Boolean | js | | @media | all | String | css |

Installation

npm install @kunoichi/grab-deps

Usage

Suppose that the directory structure of your theme/plugin is like below:

assets
- js
  - main.js
- css
  - style.css

And run this in your npm scripts or gulpfile.js.

// Import function.
const { dumpSetting } = require('@kunoichi/grab-deps');
// Dump JSON
dumpSetting( 'assets' );

For automatic dumping, watch assets directory.

// gulpfile.js
const gulp = require( 'gulp' );
const { dumpSetting } = require('@kunoichi/grab-deps');

// Dump task.
gulp.task( 'dump', function( done ) {
	dumpSetting( 'assets' );
	done();
} );

// Watch assets directory.
gulp.task( 'watch', function () {
	// Watch assets change and dump.
	gulp.watch( [ 'assets/**/*.css', 'assets/**/*.js' ], gulp.task( 'dump' ) );
} );

Now you can get updated dump information whatever changes you made for assets.

JSON Example

[
  {
    "path": "assets/js/app.js",
    "deps": [ "jquery", "wp-api-fetch" ],
  }
]

License text

Nowadays, some compilers like webpack extract license comments. If original is like below:

/*!
 * Main app file.js
 * 
 * @version 2.0.0
 */
console.log( 'Start rendering!' );

file.js will compiled like below:

console.log( 'Start rendering!' );

And in same directory, file.js.LICENSE.txt will be exported.

/*!
 * Main app file.js
 * 
 * @version 2.0.0
 */

In such case, @kunoichi/grab-deps will support .LISENCE.txt format by default. 3rd arghment suffix of dumpSetting supports other format.

// If your JS license will be in `app.js.txt`,
// You can set suffix.
// `app.js` will be `app.js.txt`
dumpSetting( 'assets', './wp-dependencies.json', '.txt' );
// If your licenses will be other format, specify function.
dumpSetting( 'assets', './wp-dependencies.json', function( path ) {
  // Convert path to your license.txt
  return licensePath;
} );