|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import path from 'path'; |
| 4 | +import glob from 'glob'; |
| 5 | +import browserify from 'browserify'; |
| 6 | +import watchify from 'watchify'; |
| 7 | +import envify from 'envify'; |
| 8 | +import babelify from 'babelify'; |
| 9 | +import _ from 'lodash'; |
| 10 | +import vsource from 'vinyl-source-stream'; |
| 11 | +import buffer from 'vinyl-buffer'; |
| 12 | +import gulpif from 'gulp-if'; |
| 13 | + |
| 14 | +export default function(gulp, plugins, args, config, taskTarget, browserSync) { |
| 15 | + let dirs = config.directories; |
| 16 | + let entries = config.entries; |
| 17 | + |
| 18 | + let browserifyTask = (files) => { |
| 19 | + return files.map((entry) => { |
| 20 | + let dest = path.resolve(taskTarget); |
| 21 | + |
| 22 | + // Options |
| 23 | + let customOpts = { |
| 24 | + entries: [entry], |
| 25 | + debug: true, |
| 26 | + transform: [ |
| 27 | + babelify, // Enable ES6 features |
| 28 | + envify // Sets NODE_ENV for better optimization of npm packages |
| 29 | + ] |
| 30 | + }; |
| 31 | + |
| 32 | + let bundler = browserify(customOpts); |
| 33 | + |
| 34 | + if (!args.production) { |
| 35 | + // Setup Watchify for faster builds |
| 36 | + let opts = _.assign({}, watchify.args, customOpts); |
| 37 | + bundler = watchify(browserify(opts)); |
| 38 | + } |
| 39 | + |
| 40 | + let rebundle = function() { |
| 41 | + let startTime = new Date().getTime(); |
| 42 | + bundler.bundle() |
| 43 | + .on('error', function(err) { |
| 44 | + plugins.util.log( |
| 45 | + plugins.util.colors.red('Browserify compile error:'), |
| 46 | + '\n', |
| 47 | + err.stack, |
| 48 | + '\n' |
| 49 | + ); |
| 50 | + this.emit('end'); |
| 51 | + }) |
| 52 | + .on('error', plugins.notify.onError(config.defaultNotification)) |
| 53 | + .pipe(vsource(entry)) |
| 54 | + .pipe(buffer()) |
| 55 | + .pipe(plugins.sourcemaps.init({loadMaps: true})) |
| 56 | + .pipe(gulpif(args.production, plugins.uglify())) |
| 57 | + .on('error', plugins.notify.onError(config.defaultNotification)) |
| 58 | + .pipe(plugins.rename(function(filepath) { |
| 59 | + // Remove 'source' directory as well as prefixed folder underscores |
| 60 | + // Ex: 'src/_scripts' --> '/scripts' |
| 61 | + filepath.dirname = filepath.dirname.replace(dirs.source, '').replace('_', ''); |
| 62 | + })) |
| 63 | + .pipe(plugins.sourcemaps.write('./')) |
| 64 | + .pipe(gulp.dest(dest)) |
| 65 | + // Show which file was bundled and how long it took |
| 66 | + .on('end', function() { |
| 67 | + let time = (new Date().getTime() - startTime) / 1000; |
| 68 | + console.log( |
| 69 | + plugins.util.colors.cyan(entry) |
| 70 | + + ' was browserified: ' |
| 71 | + + plugins.util.colors.magenta(time + 's')); |
| 72 | + return browserSync.reload('*.js'); |
| 73 | + }); |
| 74 | + }; |
| 75 | + |
| 76 | + if (!args.production) { |
| 77 | + bundler.on('update', rebundle); // on any dep update, runs the bundler |
| 78 | + bundler.on('log', plugins.util.log); // output build logs to terminal |
| 79 | + } |
| 80 | + return rebundle(); |
| 81 | + }); |
| 82 | + }; |
| 83 | + |
| 84 | + // Browserify Task |
| 85 | + gulp.task('browserify', (done) => { |
| 86 | + return glob('./' + path.join(dirs.source, dirs.scripts, entries.js), function(err, files) { |
| 87 | + if (err) { |
| 88 | + done(err); |
| 89 | + } |
| 90 | + |
| 91 | + return browserifyTask(files); |
| 92 | + }); |
| 93 | + }); |
| 94 | +} |
0 commit comments