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 | // The path where your gulp scripts are saved
|
31 | path: <string> [optional] [default="./gulp.d"]
|
32 |
|
33 | // An object that is passed to your gulp scripts
|
34 | moduleConfig: <object> [optional] [default={}]
|
35 |
|
36 | // Enables (a lot of) debug logging
|
37 | debug: <bool> [optional] [default=false]
|
38 | }
|
39 | ```
|
40 |
|
41 | ## Usage
|
42 |
|
43 | - Create a `Gulpfile.js` that autoloads the other tasks and optionally defines some config options
|
44 |
|
45 | I tend to write all gulp-related files in coffeescript, but of course plain javascript will work too.
|
46 |
|
47 | ```js
|
48 | // This object will be passed to every autoloaded gulp task.
|
49 | // Use it to store paths, configurations or mode variables
|
50 | const moduleConfig = {
|
51 | js: {
|
52 | dst: "./dist/js",
|
53 | src: "./src/js"
|
54 | }
|
55 | };
|
56 |
|
57 | // Execute the autoloader
|
58 | require("gulp-autoload")({moduleConfig});
|
59 | ```
|
60 |
|
61 | - Create the folder that will contain your scripts
|
62 |
|
63 | ```
|
64 | mkdir gulp.d
|
65 | ```
|
66 |
|
67 | - Create a gulp module in that folder
|
68 |
|
69 | Gulp modules can be written in coffeescript (v1) or javascript.<br>
|
70 | The autoloader only recognizes files with `.js` or `.coffee` extension.
|
71 |
|
72 | Example `js.coffee` that uglifies javascript:
|
73 |
|
74 | ```coffee
|
75 | gulp = require("gulp")
|
76 | pump = require("pump")
|
77 |
|
78 | # A gulp module is an exported function that accepts a config object.
|
79 | # If you don't export a function the autoloader assumes that the require()-call
|
80 | # had some kind of side-effects and does nothing after loading it.
|
81 | module.exports = (config) ->
|
82 | gulp.task("js", (cb) ->
|
83 | pump([
|
84 | gulp.src(config.js.src)
|
85 | uglify()
|
86 | gulp.dest(config.js.dst)
|
87 | ], cb)
|
88 | return
|
89 | )
|
90 | ```
|
91 |
|
92 | - Run your tasks as usual (eg `gulp js`)
|
93 |
|
94 | - ...
|
95 |
|
96 | - Profit!
|