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

gulp-autoload

v3.3.0

Published

A 5000 lines long `Gulpfile.js` is so 2015 :eyes:

Downloads

23

Readme

gulp-autoload

A 5000 lines long Gulpfile.js is so 2015 :eyes:

NPM

Backstory

gulp-autoload was created out of pure frustration while working on a project in my company. The Gulpfile.js had grown to a real beast and the first 45 lines of the file were just require calls. I wanted something more modular and decided to revamp the structure a bit.

API

require("gulp-autoload")([object])

Executes the autoloader. Takes an optional config object.

Note: The order in which files are processed is undefined. It might be lexicographical but on some operating systems / environments it could be not ordered at all. You should thus make no assumptions about the processing order and require() the submodules you depend on yourself.

These are the available config options:

{
    // The path where your gulp scripts are saved
    path:         <string> [optional] [default="./gulp.d"] 
    
    // An object that is passed to your gulp scripts
    moduleConfig: <object> [optional] [default={}]  
           
    // Enables (a lot of) debug logging
    debug:        <bool>   [optional] [default=false]      
}

Configuration

There are multiple ways to store and pass config values to your tasks. You can use and mix all of them at the same time.

Inline

Inline configs are simply passed to the gulp-autoload constructor.

let moduleConfig = {
    foo: "bar"
}

require("gulp-autoload")({moduleConfig})

File

File-based configs override inline configs. When "booting", gulp-autoload will search you configured path for a _config.js or _config.coffee file.

This file must export an object with arbitrary content. When both a _config.js and _config.coffee file are present, the behaviour is undefined.

module.exports = {
    foo: "bar"
}

Usage

  • Create a Gulpfile.js that requires and executes the autoloader
require("gulp-autoload")();
  • Create the folder that will contain your scripts
mkdir gulp.d
  • Create a _config.coffee or _config.js in gulp.d

This file is used to store all needed configuration of your gulp setup. This may be anything from paths/destinations, environments, to api keys or whatever else you want.

It is required that this file exports an oject. The content is up to you and will be passed (unprocessed) to all registered gulp modules.

module.exports =
    js:
        src: "www/src/js"
        dst: "www/dist/js"
  • Create a gulp module in gulp.d

Gulp modules can be written in coffeescript or javascript. The autoloader only recognizes files with .js or .coffee extension. If you need support for other languages see the "Special Cases" below.

Example js.coffee that uglifies javascript:

gulp = require("gulp")
pump = require("pump")

module.exports = config ->
  gulp.task "js", cb ->
    pump([
      gulp.src(config.js.src)
      uglify()
      gulp.dest(config.js.dst)
    ], cb)
  )
  • Run your tasks as usual (eg gulp js)

  • ...

  • Profit!

Special Cases

Note that there are two forms of valid gulp modules:

  • An exported function/closure that takes a config argument and will be executed by gulp-autoload
module.exports = function(config) {
    // do stuff when gulp-autoload is ready.
};
  • Side Effects

If your module does not export a function the autoloader assumes that the require() call had side effects and will do nothing with the returned values. This is included for convenience when, for example, chainloading other autoloaders or language-injection hooks like lispyscript/lib.

Example:

// compile-styles.js
// An example of how implicit side-effects can be used to chainload
// a different file and transpile it to javascript on the fly.

// Register a lisp-transpiler for example
require("lispyscript/lib/require")

// Chainload compile-styles.lisp
require("./compile-styles.lisp")();

// Notice the missing module.exports.
// By not exporting anything at all, or exporting null,
// We tell gulp-autoload to trust our side-effects.
;; compile-styles.lisp
;;
;; This module will be chainloaded by the code above
;;

(var gulp (require "gulp"))

(set module.exports 
    (function () (
        (gulp.task "compile-styles" (function(cb) (
            ;; do stuff with gulp
        ))) 
    ))
)