gulp-autoload

3.3.0 • Public • Published

gulp-autoload

A 5000 lines long Gulpfile.js is so 2015 👀

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
        ))) 
    ))
)

Readme

Keywords

none

Package Sidebar

Install

npm i gulp-autoload

Weekly Downloads

22

Version

3.3.0

License

BSD-3-Clause

Unpacked Size

12 kB

Total Files

8

Last publish

Collaborators

  • sn0w