1 | # gulp-autoload
|
2 |
|
3 | A 5000-lines long `Gulpfile.js` is so 2015 :eyes:
|
4 |
|
5 | [![NPM](https://nodei.co/npm/gulp-autoload.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/gulp-autoload/)
|
6 |
|
7 | ## Backstory
|
8 |
|
9 | gulp-autoload was created out of pure frustration while working on a project in my company.<br>
|
10 | The `Gulpfile.js` had grown to a real beast and the first 45 lines of the file were just `require` calls.
|
11 |
|
12 | I wanted something more modular and decided to revamp the structure a bit.<br>
|
13 | A few months later we faced that problem again and I decided to extract the code that loads the
|
14 | gulp tasks into this npm module.
|
15 |
|
16 | So uhm yeah.<br>
|
17 | Have fun, and if you like it leave a :star: :grimacing:
|
18 |
|
19 | ## API
|
20 |
|
21 | #### `require("gulp-autoload")([object])`
|
22 |
|
23 | Executes the autoloader.<br>
|
24 | Takes an optional config object.
|
25 |
|
26 | These are the available config options:
|
27 |
|
28 | ```txt
|
29 | {
|
30 | path: <string> [optional] [default="./gulp.d"] The path where your gulp scripts are saved
|
31 | moduleConfig: <object> [optional] [default={}] An object that is passed to your gulp scripts
|
32 | debug: <bool> [optional] [default=false] Enables debug logging
|
33 | }
|
34 | ```
|
35 |
|
36 | ## Usage
|
37 |
|
38 | - Create a `Gulpfile.js` that autoloads the other tasks and optionally defines some config options
|
39 |
|
40 | I tend to write all gulp-related files in coffeescript, but of course plain javascript will work too.
|
41 |
|
42 | ```js
|
43 | // This object will be passed to every autoloaded gulp task.
|
44 | // Use it to store paths, configurations or mode variables
|
45 | const moduleConfig = {
|
46 | js: {
|
47 | dst: "./dist/js",
|
48 | src: "./src/js"
|
49 | }
|
50 | };
|
51 |
|
52 | // Execute the autoloader
|
53 | require("gulp-autoload")({moduleConfig});
|
54 | ```
|
55 |
|
56 | - Create the folder that will contain your scripts
|
57 |
|
58 | ```
|
59 | mkdir gulp.d
|
60 | ```
|
61 |
|
62 | - Create a gulp module in that folder
|
63 |
|
64 | Gulp modules can be written in coffeescript (v1) or javascript.<br>
|
65 | The autoloader only recognizes files with `.js` or `.coffee` extension.
|
66 |
|
67 | Example `js.coffee` that uglifies javascript:
|
68 |
|
69 | ```coffee
|
70 | gulp = require("gulp")
|
71 | pump = require("pump")
|
72 |
|
73 | # A gulp module is an exported function that accepts a config object.
|
74 | # If you don't export a function the autoloader assumes that the require()-call
|
75 | # had some kind of side-effects and does nothing after loading it.
|
76 | module.exports = (config) ->
|
77 | gulp.task("js", (cb) ->
|
78 | pump([
|
79 | gulp.src(config.js.src)
|
80 | uglify()
|
81 | gulp.dest(config.js.dst)
|
82 | ], cb)
|
83 | return
|
84 | )
|
85 | ```
|
86 |
|
87 | - Run your tasks as usual (eg `gulp js`)
|
88 |
|
89 | - ...
|
90 |
|
91 | - Profit!
|