|
2 | 2 |
|
3 | 3 | ## Installation/Usage: |
4 | 4 |
|
5 | | -To try this example, follow these 4 simple steps. |
| 5 | +To try this example, follow these 4 simple steps. |
6 | 6 |
|
7 | 7 | **Step 1**: Clone this entire repo |
8 | 8 | ```bash |
@@ -41,49 +41,71 @@ var watchify = require('watchify'); |
41 | 41 | var exorcist = require('exorcist'); |
42 | 42 | var browserify = require('browserify'); |
43 | 43 | var browserSync = require('browser-sync').create(); |
44 | | - |
45 | | -// Input file. |
46 | | -watchify.args.debug = true; |
47 | | -var bundler = watchify(browserify('./app/js/app.js', watchify.args)); |
48 | | - |
49 | | -// Babel transform |
50 | | -bundler.transform(babelify.configure({ |
51 | | - sourceMapRelative: 'app/js' |
52 | | -})); |
53 | | - |
54 | | -// On updates recompile |
55 | | -bundler.on('update', bundle); |
56 | | - |
57 | | -function bundle() { |
58 | | - |
59 | | - gutil.log('Compiling JS...'); |
60 | | - |
61 | | - return bundler.bundle() |
62 | | - .on('error', function (err) { |
63 | | - gutil.log(err.message); |
64 | | - browserSync.notify("Browserify Error!"); |
65 | | - this.emit("end"); |
66 | | - }) |
67 | | - .pipe(exorcist('app/js/dist/bundle.js.map')) |
68 | | - .pipe(source('bundle.js')) |
69 | | - .pipe(gulp.dest('./app/js/dist')) |
70 | | - .pipe(browserSync.stream({once: true})); |
71 | | -} |
| 44 | +var glob = require('glob'); |
| 45 | +var path = require('path'); |
| 46 | +var mkdirp = require('mkdirp'); |
| 47 | +var eventStream = require('event-stream'); |
72 | 48 |
|
73 | 49 | /** |
74 | | - * Gulp task alias |
| 50 | + * Creating multiple bundles with Watchify |
75 | 51 | */ |
76 | | -gulp.task('bundle', function () { |
77 | | - return bundle(); |
| 52 | +gulp.task('bundle', function (done) { |
| 53 | + glob('./app/js/{app.js,worker/worker.js}', {}, function (err, files) { |
| 54 | + if (err) { |
| 55 | + done(err); |
| 56 | + } |
| 57 | + |
| 58 | + var tasks = files.map(function (entry) { |
| 59 | + var relative = path.relative('./app/js', entry); |
| 60 | + |
| 61 | + // A workaround for exorcist issue, |
| 62 | + // where exorcist fails silently when the output dir does not exist |
| 63 | + // see https://github.com/thlorenz/exorcist/issues/18 |
| 64 | + // and https://github.com/thlorenz/exorcist/pull/19 |
| 65 | + var sourceMapPath = './app/js/dist/' + relative + '.map'; |
| 66 | + mkdirp.sync(path.dirname(sourceMapPath)); |
| 67 | + |
| 68 | + // Input file. |
| 69 | + watchify.args.debug = true; |
| 70 | + var bundler = watchify(browserify(entry, watchify.args)); |
| 71 | + |
| 72 | + // Babel transform |
| 73 | + bundler.transform(babelify.configure({ |
| 74 | + sourceMapRelative: 'app/js' |
| 75 | + })); |
| 76 | + |
| 77 | + var bundle = function () { |
| 78 | + gutil.log('Compiling ' + entry + '...'); |
| 79 | + |
| 80 | + return bundler.bundle() |
| 81 | + .on('error', function (err) { |
| 82 | + gutil.log(err.message); |
| 83 | + browserSync.notify('Browserify Error!'); |
| 84 | + this.emit('end'); |
| 85 | + }) |
| 86 | + .pipe(exorcist(sourceMapPath)) |
| 87 | + .pipe(source(relative)) |
| 88 | + .pipe(gulp.dest('./app/js/dist')) |
| 89 | + .pipe(browserSync.stream({once: true})); |
| 90 | + } |
| 91 | + |
| 92 | + // On updates recompile |
| 93 | + bundler.on('update', bundle); |
| 94 | + |
| 95 | + return bundle(); |
| 96 | + }); |
| 97 | + |
| 98 | + eventStream.merge(tasks).on('end', done); |
| 99 | + }); |
78 | 100 | }); |
79 | 101 |
|
80 | 102 | /** |
81 | 103 | * First bundle, then serve from the ./app directory |
82 | 104 | */ |
83 | 105 | gulp.task('default', ['bundle'], function () { |
84 | 106 | browserSync.init({ |
85 | | - server: "./app" |
| 107 | + server: './app' |
86 | 108 | }); |
87 | 109 | }); |
88 | | -``` |
89 | 110 |
|
| 111 | +``` |
0 commit comments