|
| 1 | +/* |
| 2 | + * This is the heart of Gulp. Here we define and compost tasks together |
| 3 | + * |
| 4 | + * Gulp runs on plugins! |
| 5 | + * If you want to automate anything, run an `npm search` |
| 6 | + * for `gulp-whatever` and chances are you'll find it. |
| 7 | + */ |
| 8 | + |
| 9 | +var gulp = require('gulp'), |
| 10 | + sync = require('run-sequence'), |
| 11 | + serve = require('browser-sync'), |
| 12 | +/* START SOLUTION */ |
| 13 | + webpack = require('gulp-webpack'); |
| 14 | +/* END SOLUTION */ |
| 15 | + |
| 16 | +/* |
| 17 | + * A convenience object to hold different paths we care about. |
| 18 | + * |
| 19 | + * This 'glob' notation is a nice wildcard notation for paths that is common in gulp tasks. |
| 20 | + * This glob says: watch for any .js, .css, or .html file at any level in our 'client' directory |
| 21 | + */ |
| 22 | +var paths = { |
| 23 | + app: ['client/**/*.{js,css,html}'] |
| 24 | +}; |
| 25 | + |
| 26 | +/* START SOLUTION */ |
| 27 | +/* |
| 28 | + * Gulp is built ontop of node's powerful 'stream' concepts. |
| 29 | + * You can think of a stream as faucet of water that flows through different 'pipes' |
| 30 | + * We open the gulp faucet by passing a glob of files into gulp.src. |
| 31 | + * Then we can 'pipe' these files through all sorts of different transformations, |
| 32 | + * each one returning its own stream that can continue to be piped into more transformations. |
| 33 | + * |
| 34 | + * Here, we're grabbing the entry point to our (making the `entry` property in webpack.config.js unnecessary), |
| 35 | + * piping the entry into webpack plugin, bundling our app, and piping that into the client folder designated by gulp.dest |
| 36 | + * |
| 37 | + * NOTE: gulp.dest defines our webpack's output folder. Webpack will get mad if it finds an output.path property |
| 38 | + * in webpack.config.js. We still need `filename` though. |
| 39 | +*/ |
| 40 | +gulp.task('build', function() { |
| 41 | + return gulp.src('./client/app.js') |
| 42 | + .pipe(webpack(require('./webpack.config.js'))) |
| 43 | + .pipe(gulp.dest('client')); |
| 44 | +}); |
| 45 | +/* END SOLUTION */ |
| 46 | + |
| 47 | +/* |
| 48 | + * Browsersync manages a server that plays nicely with gulp (see serve.reload below) |
| 49 | + * NOTE: server.baseDir should be a directory that contains `index.html` |
| 50 | + */ |
| 51 | +gulp.task('serve', function() { |
| 52 | + serve({ |
| 53 | + port: 3000, |
| 54 | + open: false, |
| 55 | + server: { |
| 56 | + baseDir: 'client' |
| 57 | + } |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +/* |
| 62 | + * Gulp also has the power to watch your files for any changes, and run a command whenever one changes. |
| 63 | + * It doesn't even need a plugin. Let's do it! |
| 64 | + * |
| 65 | + * This task says: If any files in our app glob change, re-build the whole thing, and then reload the server |
| 66 | + */ |
| 67 | +gulp.task('watch', function() { |
| 68 | + gulp.watch(paths.app, ['build', serve.reload]); |
| 69 | +}); |
| 70 | + |
| 71 | +/* |
| 72 | + * A gulp task can consist of many other gulp tasks. |
| 73 | + * By default, tasks passed in using array notation (like `[serve.reload]` above) run in series, |
| 74 | + * not guaranteeing any order of completion, and causing a race condition. |
| 75 | + * |
| 76 | + * Sometimes this is not wanted. For example, if we want to fully transpile all our ES6 code into ES5 |
| 77 | + * before it is served, we need to guarantee the entire build step is completed before the serve task. |
| 78 | + * |
| 79 | + * `run-sequence` is a nice plugin that lets us run gulp tasks in series instead of parallel. |
| 80 | + * |
| 81 | + * Now whenever we run `gulp`, it will run the `build` command in its entirety, |
| 82 | + * the `serve` command in its entirety, then `watch` in its entirety, |
| 83 | + * and then invoke the `done` callback to let gulp know all asyncronous tasks have been completed. |
| 84 | + */ |
| 85 | +gulp.task('default', function(done) { |
| 86 | + sync(/*START SOLUTION*/'build'/*END SOLUTION*/, 'serve', 'watch', done); |
| 87 | +}); |
0 commit comments