Code Monkey home page Code Monkey logo

gulp-watchify-factor-bundle's Introduction

gulp-watchify-factor-bundle

version status coverage dependencies devDependencies

A sugar wrapper for browserify, watchify and factor-bundle to work with gulp.

The main ideas are borrowed from reduce-js.

Usage

gulpfile.js:

var reduce = require('gulp-watchify-factor-bundle')
var gulp = require('gulp')
var path = require('path')
var buffer = require('vinyl-buffer')
var uglify = require('gulp-uglify')
var del = require('del')

gulp.task('clean', function () {
  return del('build')
})

gulp.task('build', ['clean'], function () {
  var basedir = path.join(__dirname, 'src')

  // Create a browserify instance
  // same with `browserify(opts)`
  var b = reduce.create({ basedir: basedir })

  // find entries
  // same with gulp.src()
  return reduce.src('page/**/index.js', { cwd: basedir })
    // apply `factor-bundle`
    // and call b.bundle() which produces a vinyl stream now
    .pipe(reduce.bundle(b, { common: 'common.js' }))

    // apply gulp plugins to process the vinyl stream
    .pipe(buffer())
    .pipe(uglify())

    // same with gulp.dest
    .pipe(reduce.dest('build'))
})

gulp.task('watch', ['clean'], function () {
  var basedir = path.join(__dirname, 'src')

  // Create a browserify instance
  // same with `browserify(opts)`
  var b = reduce.create({ basedir: basedir })

  b.on('log', console.log.bind(console))

  // find entries
  // same with gulp.src()
  return reduce.src('page/**/index.js', { cwd: basedir })
    // apply `factor-bundle` and `watchify`
    .pipe(reduce.watch(b, { common: 'common.js' }))
    // whenever `b.bundle()` is called,
    // event 'bundle' is fired
    .on('bundle', function (vinylStream) {
      // vinylStream = b.bundle()
      vinylStream
        // apply gulp plugins to process the vinyl stream
        .pipe(buffer())
        .pipe(uglify())
        // same with gulp.dest
        .pipe(reduce.dest('build'))
    })
})

The source directory:

example/src/
├── node_modules
│   └── color-map
│       └── index.js
└── page
    ├── blue
    │   └── index.js
    └── red
        └── index.js

The build directory:

⌘ tree example/build/
example/build/
├── common.js
└── page
    ├── blue
    │   └── index.js
    └── red
        └── index.js

Exports

create()

Same with the browserify constructor.

bundle(b, bundleOptions)

A gulp plugin to use browserify with factor-bundle, and produces a vinyl stream.

b

The browserify instance.

bundleOptions

Options for factor-bundle.

bundleOptions.common specifies the path to the common bundle. All other options are exactly the same with those consumed by factor-bundle.

NOTE

bundleOptions.outputs must be an array of file paths. However, if not specified, a new bundle is created for each entry, with the same path with the entry.

watch(b, bundleOptions, watchifyOptions)

A gulp plugin to use browserify with factor-bundle and watchify.

b

The browserify instance.

bundleOptions

Options for factor-bundle.

watchOptions

Options for watchify.

NOTE This method creates a transform to process the entry stream, and emit a bundle event whenever b.bundle() called.

reduce.watch().on('bundle', vinylStream => {})

gulp-watchify-factor-bundle's People

Contributors

colorfulstan avatar zoubin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.