Skip to content

Commit

Permalink
Compatible with non-pattern entries
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubin committed Dec 29, 2015
1 parent 7744a74 commit 5e7d7a5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ A sugar wrapper for [`browserify`].
* Use [`common-bundle`] to pack modules by default.
* Easy to work with [`gulp`].

## Examples

Check [examples](example/).
## Example
Check more [examples](example/).

```javascript
var gulp = require('gulp')
Expand Down Expand Up @@ -72,25 +71,29 @@ function src(r) {
Create a stream flowing [`vinyl`] file objects,
which represents bundles created.

#### patterns
**patterns**

Type: `String`, `Array`

Used by [`globby`] to locate entries.

#### bopts
**bopts**

Options to create the [`browserify`] instance.

Fields not explained in the following sections
are the same with [`browserify`].

#### basedir
**basedir**

Type: `String`

Default: `process.cwd()`

Used as the `cwd` field of the options passed to [`globby`].

#### bundleOptions
**bundleOptions**

Type: `Object`

Options passed to [`common-bundle`].
Expand Down Expand Up @@ -138,7 +141,7 @@ reduce.on('instance', function (b) {

`reduce.watch().on('instance', (b) => {})`

## Without Gulp
## Without [`gulp`]
If you are not using [`gulp`],
use `reduce.dest` instead of [`gulp.dest`] to write bundles into the disk.
Also, you can use `reduce.run` to run the task.
Expand Down
20 changes: 2 additions & 18 deletions example/single-bundle/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ gulp.task('clean', function () {
return del(path.join(__dirname, 'build'))
})

// Pack all JS modules into one bundle.
gulp.task('build', ['clean'], function () {
return src(reduce)
// `pipe` into gulp-plugins
.pipe(gulp.dest('build'))
})

// To keep `watch` unfinished, declare `cb` as the first argument of the task callback
gulp.task('watch', ['clean'], function (cb) {
return src(reduce.watch())
// `pipe` into lazy transforms, i.e. functions to create transforms
.pipe(gulp.dest, 'build')
})

Expand All @@ -29,22 +25,10 @@ function src(r) {
r.on('error', function (err) {
gutil.log(err.stack)
})
// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
return r.src('page/**/index.js', {
// Options passed to `common-bundle`
// Refer to `https://github.com/zoubin/common-bundle` for more information.
// Name of the output file.
// If omitted, `common.js` will be used.
return r.src({
entries: ['page/hello/index.js', 'page/hi/index.js'],
bundleOptions: 'bundle.js',

// And all options passed to `browserify`
// Refer to `https://github.com/substack/node-browserify#methods` for more information

basedir: path.join(__dirname, 'src'),

// Now, we can `require('lib/world')` anywhere under the `src` directory.
// Otherwise, we have to write relative paths like `require('../../web_modules/lib/world')`
paths: [path.join(__dirname, 'src', 'web_modules')],
})
}
Expand Down
23 changes: 15 additions & 8 deletions lib/browserify.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
var browserify = require('browserify')
var glob = require('globby')
var exclude = require('mixy').exclude

var bundler = require('./bundler')

module.exports = function (pattern, opts) {
opts = opts || {}
var bopts = exclude(['bundleOptions'], opts)
bopts.basedir = bopts.basedir || process.cwd()
module.exports = function (entries, opts) {
if (typeof entries === 'string' || Array.isArray(entries)) {
opts = opts || {}
entries = glob.sync(entries, {
cwd: opts.basedir || process.cwd(),
})
} else {
opts = entries || {}
entries = []
}
opts.cache = opts.cache || {}
opts.packageCache = opts.packageCache || {}

var entries = glob.sync(pattern, { cwd: bopts.basedir })
var b = browserify(entries, bopts)
var b = browserify(entries, opts)
var bundleOptions = opts.bundleOptions || 'bundle.js'

b.plugin(opts.bundler || bundler, opts.bundleOptions || 'bundle.js')
b.plugin(opts.bundler || bundler, bundleOptions)

return b
}
Expand Down
5 changes: 1 addition & 4 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ function Watch(opts) {
}

Watch.prototype.src = function(pattern, opts) {
opts = opts || {}
opts.cache = opts.cache || {}
opts.packageCache = opts.packageCache || {}
var b = browserify(pattern, opts)
watchify(b, this.options)
b.plugin(watchify, this.options)

var onerror = this.emit.bind(this, 'error')
var self = this
Expand Down
3 changes: 2 additions & 1 deletion test/single-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function clean() {
}

function bundle() {
return reduce.src('*.js', {
return reduce.src({
entries: ['green.js', 'red.js'],
basedir: fixtures('src', 'single-bundle'),
//bundleOptions: 'bundle.js',
})
Expand Down

0 comments on commit 5e7d7a5

Please sign in to comment.