Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubin committed Dec 16, 2015
1 parent 1e14f57 commit 54304e6
Show file tree
Hide file tree
Showing 34 changed files with 386 additions and 107 deletions.
84 changes: 53 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,70 @@ which can be transformed by [gulp](https://www.npmjs.com/package/gulp) plugins.

## Examples

See the files in the `example` directory.
Check [examples](example/).

```javascript
var gulp = require('gulp');
var reduce = require('reduce-js');
var path = require('path');
var gutil = require('gulp-util');
var uglify = require('gulp-uglify');
var lazypipe = require('lazypipe');
var del = require('del');
var gulp = require('gulp')
var reduce = require('reduce-js')
var path = require('path')
var uglify = require('gulp-uglify')
var del = require('del')

var basedir = path.join(__dirname, 'src');
var onerror = function (err) {
console.log(err.message)
}

var factorOpts = {
outputs: ['a.js', 'b.js'],
common: 'common.js',
};
var bundleOpts = {
// Options passed to `factor-vinylify`
// Refer to `https://github.com/zoubin/factor-vinylify#options` for more information.
factor: {
// One bundle for each entry detected from `.src()`.
needFactor: true,

var onerror = function (err) {
gutil.log(err.message);
};
// If omitted, no common bundle will be created.
common: 'common.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')],
}

gulp.task('clean', function () {
return del(path.join(__dirname, 'build'));
});
return del(path.join(__dirname, 'build'))
})

gulp.task('multiple', ['clean'], function () {
return reduce.src('*.js', { basedir: basedir, factor: factorOpts })
.on('log', gutil.log.bind(gutil))
.on('error', onerror)
// Pack all JS modules into multiple bundles.
gulp.task('build', ['clean'], function () {
reduce.on('log', console.log.bind(console))
reduce.on('error', onerror)

// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
return reduce.src('page/**/index.js', bundleOpts)
// `pipe` into gulp-plugins
.pipe(uglify())
.pipe(gulp.dest('build'));
});
.pipe(gulp.dest('build'))
})

gulp.task('watch-multiple', ['clean'], function (cb) {
reduce.watch()
.on('log', gutil.log.bind(gutil))
.on('error', onerror)
.src('*.js', { basedir: basedir, factor: factorOpts })
// To keep `watch` unfinished, declare `cb` as the first argument of the task callback
gulp.task('watch', ['clean'], function (cb) {
var watcher = reduce.watch()
watcher.on('log', console.log.bind(console))
watcher.on('error', onerror)

// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
watcher.src('page/**/index.js', bundleOpts)
// `pipe` into lazy transforms, i.e. functions to create transforms
.pipe(uglify)
.pipe(gulp.dest, 'build');
});
.pipe(gulp.dest, 'build')
})

```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var gulp = require('gulp')
var reduce = require('../..')
var path = require('path')
var uglify = require('gulp-uglify')
var del = require('del')

var onerror = function (err) {
console.log(err.message)
}

var initialScripts = [
path.join(__dirname, 'src/web_modules/lib/init.js'),
]

var bundleOpts = {
// Options passed to `factor-vinylify`
// Refer to `https://github.com/zoubin/factor-vinylify#options` for more information.
factor: {
// One bundle for each entry detected from `.src()`.
needFactor: true,

// If omitted, no common bundle will be created.
common: 'common.js',

// This is a little complicated.
// We need to make `factor-vinylify` more convenient to handle such cases.
threshold: function (row, groups) {
// Force `modules` specified in `initialScripts` go to the common bundle
if (initialScripts.indexOf(row.file) >= 0) {
return true
}

// Apply commonify
// Refer to `https://github.com/zoubin/factor-vinylify#commonify` for more information
if (typeof row.common === 'boolean') {
return row.common
}

return groups.length > 1 || groups.length === 0
},
},

// 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')],
}

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

// Pack all JS modules into multiple bundles.
gulp.task('build', ['clean'], function () {
reduce.on('log', console.log.bind(console))
reduce.on('error', onerror)

// `b` is the browserify instance.
reduce.on('instance', function (b) {
b.add('./web_modules/lib/init.js')
})

// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
return reduce.src('page/**/index.js', bundleOpts)
// `pipe` into gulp-plugins
.pipe(uglify())
.pipe(gulp.dest('build'))
})

// To keep `watch` unfinished, declare `cb` as the first argument of the task callback
gulp.task('watch', ['clean'], function (cb) {
var watcher = reduce.watch()
watcher.on('log', console.log.bind(console))
watcher.on('error', onerror)

// `b` is the browserify instance.
watcher.on('instance', function (b) {
b.add('./web_modules/lib/init.js')
})

// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
watcher.src('page/**/index.js', bundleOpts)
// `pipe` into lazy transforms, i.e. functions to create transforms
.pipe(uglify)
.pipe(gulp.dest, 'build')
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var world = require('lib/world')
module.exports = 'hello, ' + world

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var world = require('lib/world')
module.exports = 'hi, ' + world

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = 'Welcome to ' + require('./website') + require('exclamation')

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'http://www.example.com'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'world' + require('exclamation')
66 changes: 66 additions & 0 deletions example/customize-the-common-bundle/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var gulp = require('gulp')
var reduce = require('../..')
var path = require('path')
var uglify = require('gulp-uglify')
var del = require('del')

var onerror = function (err) {
console.log(err.message)
}

var bundleOpts = {
// Options passed to `factor-vinylify`
// Refer to `https://github.com/zoubin/factor-vinylify#options` for more information.
factor: {
// One bundle for each entry detected from `.src()`.
needFactor: true,

// If omitted, no common bundle will be created.
common: 'common.js',

// Passed to multimatch to filter modules should go to the common bundle.
// Refer to `https://github.com/sindresorhus/multimatch` for more information
threshold: '**/lib/*.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')],
}

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

// Pack all JS modules into multiple bundles.
gulp.task('build', ['clean'], function () {
reduce.on('log', console.log.bind(console))
reduce.on('error', onerror)

// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
return reduce.src('page/**/index.js', bundleOpts)
// `pipe` into gulp-plugins
.pipe(uglify())
.pipe(gulp.dest('build'))
})

// To keep `watch` unfinished, declare `cb` as the first argument of the task callback
gulp.task('watch', ['clean'], function (cb) {
var watcher = reduce.watch()
watcher.on('log', console.log.bind(console))
watcher.on('error', onerror)

// The first argument is passed to globby.
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information
watcher.src('page/**/index.js', bundleOpts)
// `pipe` into lazy transforms, i.e. functions to create transforms
.pipe(uglify)
.pipe(gulp.dest, 'build')
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/customize-the-common-bundle/src/page/hello/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var world = require('lib/world')
module.exports = 'hello, ' + world

3 changes: 3 additions & 0 deletions example/customize-the-common-bundle/src/page/hi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var there = require('./there')
module.exports = 'hi, ' + there

1 change: 1 addition & 0 deletions example/customize-the-common-bundle/src/page/hi/there.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'there' + require('exclamation')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'world' + require('exclamation')
74 changes: 0 additions & 74 deletions example/gulpfile.js

This file was deleted.

Loading

0 comments on commit 54304e6

Please sign in to comment.