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

klect

v0.1.5

Published

Simple asset manager

Downloads

5

Readme

What is "klect"?

Klect is a node module that will collect and enumerate groups of assets.

This is usually Javascript and Stylesheets, but it can be anything.

Install

$ npm install klect

Basic usage

var bundles = {
    "mobile.css": 'modules/**/*.mobile.{less,css}'
    "desktop.css": 'modules/**/*.{less,css}'
    "site.js": [
        'modules/angular/angular.js',
        'modules/**/*.js'
    ]
}

var assets = new klect().gather(bundles);

// List files
console.log(assets.files());

// List only "site.js" files
console.log(assets.bundles("site.js").files())

// List only "site.js" URL paths
console.log(assets.bundles("site.js").urls())

Note that assets are collected in a first come / first claim basis.

In the example above,

    "mobile.css": 'modules/**/*.mobile.{less,css}'
    "desktop.css": 'modules/**/*.{less,css}'

desktop.css will ignore any files already claimed by mobile.css.

This also applies within a single bundle:

    "site.js": [
        'modules/angular/angular.js',
        'modules/**/*.js'

Here, angular will always be loaded first, and the *.js glob will ignore angular so there will be no duplicates.

If there's a file you need included in every bundle every time, like a .less mixin, just preface the path with a "!":

    "mobile.css": [
        '!modules/styles/mixins.less'
        'modules/**/*.mobile.{less,css}'
    ]
    "desktop.css": [
        '!modules/styles/mixins.less'
        'modules/**/*.{less,css}'
    ]

You may have also guessed by now that path specifications can be literal

'foo/bar.js'

or glob

'foo/**/bar.*'

Klect is also good for server-side modules:

var files = new klect().gather(['modules/server/*']).files()
for (var i = 0; i < files.length; i++) {
    require(files[i]);
}

Advanced usage

Set path base and web url base:

var assets = new klect({
    cwd: "./",
    urlcwd: "/"
}).gather(bundles);

Get a specific bundle:

console.log(assets.bundles("site.js"));

Or a list of bundles by glob name:

console.log(assets.bundles("site.*"));

Integrations

Express / web views

First, create a helper function for your views:

var assets = new klect().gather(bundles);

app.locals.scripts = function(bundleName){
    // In production, just reference the bundle name, like "site.js"
    // otherwise, load each script individually.
    var scripts = environment === "production" ? [bundleName] : assets.bundles(bundleName).urls();

    var output = "";
    for (var i = 0; i < scripts.length; i++) {
        output += '<script src="'+ scripts[i] +'" type="text/javascript"></script>'
    }
    return output;
}

index.jade

html
    head
        !=scripts("head.js")
    body
        h1 Example page
        !=scripts("site.js")

Build systems

Gulp

This will build every script for production environments by bundle name.

var assets = new klect().gather(bundles);

gulp.task('scripts', function(){
    var scripts = assets.bundles('*.js');
    
    for (var i = 0; i < scripts.length; i++) {
        gulp
        .src(scripts[i].files)
        .pipe(concat(js.name))
        .pipe(uglify())
        .pipe(gulp.dest('./build/'));
    }
})

License

klect is UNLICENSED.