diff --git a/example/gulp/multi/gulpfile.js b/example/gulp/multi/gulpfile.js new file mode 100644 index 0000000..24b4184 --- /dev/null +++ b/example/gulp/multi/gulpfile.js @@ -0,0 +1,65 @@ +'use strict' + +const reduce = require('../../..') +const gulp = require('gulp') +const path = require('path') +const del = require('del') + +gulp.task('clean', function () { + return del(path.join(__dirname, 'build')) +}) + +gulp.task('build', ['clean'], function () { + let basedir = path.join(__dirname, 'src') + let b = reduce.create({ + basedir: basedir, + paths: [path.join(basedir, 'web_modules')], + }) + + // As multiple bundles are created, + // it is important to deal with duplicates carefully. + // There are known issues caused in such cases. + // See https://github.com/substack/factor-bundle/issues/51 + // This plugin will just disable the default dedupe transform. + b.plugin('dedupify') + b.on('dedupify.deduped', function (o) { + console.warn('Duplicates of modules found!', o.file, o.dup) + }) + + return gulp.src('page/**/index.js', { cwd: basedir }) + .pipe(reduce.bundle(b, { + // This object is passed to `common-bundle`. + groups: 'page/**/index.js', + common: 'common.js', + })) + .pipe(gulp.dest('build')) +}) + +gulp.task('watch', ['clean'], function () { + let clean = require('clean-remains')([]) + let basedir = path.join(__dirname, 'src') + let b = reduce.create({ + basedir: basedir, + paths: [path.join(basedir, 'web_modules')], + cache: {}, + packageCache: {}, + }) + b.plugin('dedupify') + b.on('dedupify.deduped', function (o) { + console.warn('Duplicates of modules found!', o.file, o.dup) + }) + return gulp.src('page/**/index.js', { cwd: basedir }) + .pipe(reduce.watch(b, { + groups: 'page/**/index.js', + common: 'common.js', + }, { entryGlob: 'page/**/index.js' })) + .on('bundle', function (bundleStream) { + bundleStream + .pipe(gulp.dest('build')) + // This plugin will remove files created in the building history but no longer in the newest build. + .pipe(clean()) + .on('data', file => console.log('bundle:', file.relative)) + .on('end', () => console.log('-'.repeat(40))) + }) +}) + diff --git a/example/gulp/multiple-bundles/src/node_modules/exclamation/excl.js b/example/gulp/multi/src/node_modules/exclamation/excl.js similarity index 100% rename from example/gulp/multiple-bundles/src/node_modules/exclamation/excl.js rename to example/gulp/multi/src/node_modules/exclamation/excl.js diff --git a/example/gulp/multiple-bundles/src/node_modules/exclamation/package.json b/example/gulp/multi/src/node_modules/exclamation/package.json similarity index 100% rename from example/gulp/multiple-bundles/src/node_modules/exclamation/package.json rename to example/gulp/multi/src/node_modules/exclamation/package.json diff --git a/example/gulp/multiple-bundles/src/page/hello/index.js b/example/gulp/multi/src/page/hello/index.js similarity index 100% rename from example/gulp/multiple-bundles/src/page/hello/index.js rename to example/gulp/multi/src/page/hello/index.js diff --git a/example/gulp/multiple-bundles/src/page/hey/index.js b/example/gulp/multi/src/page/hi/index.js similarity index 100% rename from example/gulp/multiple-bundles/src/page/hey/index.js rename to example/gulp/multi/src/page/hi/index.js diff --git a/example/gulp/multiple-bundles/src/web_modules/lib/world.js b/example/gulp/multi/src/web_modules/lib/world.js similarity index 100% rename from example/gulp/multiple-bundles/src/web_modules/lib/world.js rename to example/gulp/multi/src/web_modules/lib/world.js diff --git a/example/gulp/multiple-bundles/gulpfile.js b/example/gulp/multiple-bundles/gulpfile.js deleted file mode 100644 index 04ff345..0000000 --- a/example/gulp/multiple-bundles/gulpfile.js +++ /dev/null @@ -1,59 +0,0 @@ -'use strict' - -const reduce = require('../../..') -const gulp = require('gulp') -const path = require('path') -const del = require('del') - -gulp.task('clean', function () { - return del(path.join(__dirname, 'build')) -}) - -gulp.task('build', ['clean'], function () { - let b = createBundler() - return gulp.src('page/**/index.js', { cwd: b._options.basedir }) - .pipe(reduce.bundle(b, { - groups: 'page/**/index.js', - common: 'common.js', - })) - .pipe(gulp.dest('build')) -}) - -gulp.task('watch', ['clean'], function (cb) { - let b = createBundler() - let clean = require('clean-remains')([]) - b.on('bundle-stream', function (bundleStream) { - bundleStream - .pipe(gulp.dest('build')) - .pipe(clean()) - }) - gulp.src('page/**/index.js', { cwd: b._options.basedir }) - .pipe(reduce.watch(b, { - groups: 'page/**/index.js', - common: 'common.js', - }, { entryGlob: 'page/**/index.js' })) -}) - -function createBundler() { - let basedir = path.join(__dirname, 'src') - let b = reduce.create({ - basedir: basedir, - paths: [path.join(basedir, 'web_modules')], - fileCache: {}, - cache: {}, - packageCache: {}, - }) - - b.plugin('dedupify') - b.on('dedupify.deduped', function (o) { - console.warn('Duplicates of modules found!', o.file, o.dup) - }) - - b.on('error', console.log.bind(console)) - b.on('common.map', function (map) { - console.log('bundles:', '[ ' + Object.keys(map).join(', ') + ' ]') - }) - - return b -} - diff --git a/example/gulp/single-bundle/gulpfile.js b/example/gulp/reduce/gulpfile.js similarity index 55% rename from example/gulp/single-bundle/gulpfile.js rename to example/gulp/reduce/gulpfile.js index 6371f44..f594972 100644 --- a/example/gulp/single-bundle/gulpfile.js +++ b/example/gulp/reduce/gulpfile.js @@ -10,37 +10,33 @@ gulp.task('clean', function () { }) gulp.task('build', ['clean'], function () { - let b = createBundler() - return gulp.src('page/**/index.js', { cwd: b._options.basedir }) + let basedir = path.join(__dirname, 'src') + let b = reduce.create({ + basedir: basedir, + paths: [path.join(basedir, 'web_modules')], + }) + return gulp.src('page/**/index.js', { cwd: basedir }) .pipe(reduce.bundle(b, 'bundle.js')) .pipe(gulp.dest('build')) }) -gulp.task('watch', ['clean'], function (cb) { - let b = createBundler() - b.plugin(require('clean-remains')([])) - b.on('bundle-stream', function (bundleStream) { - bundleStream.pipe(gulp.dest('build')) - }) - gulp.src('page/**/index.js', { cwd: b._options.basedir }) - .pipe(reduce.watch(b, 'bundle.js', { entryGlob: 'page/**/index.js' })) -}) - -function createBundler() { +gulp.task('watch', ['clean'], function () { + let clean = require('clean-remains')([]) let basedir = path.join(__dirname, 'src') let b = reduce.create({ basedir: basedir, paths: [path.join(basedir, 'web_modules')], - fileCache: {}, cache: {}, packageCache: {}, }) - - b.on('error', console.log.bind(console)) - b.on('common.map', function (map) { - console.log('bundles:', '[ ' + Object.keys(map).join(', ') + ' ]') - }) - - return b -} + return gulp.src('page/**/index.js', { cwd: b._options.basedir }) + .pipe(reduce.watch(b, 'bundle.js', { entryGlob: 'page/**/index.js' })) + .on('bundle', function (bundleStream) { + bundleStream + .pipe(gulp.dest('build')) + .pipe(clean()) + .on('data', file => console.log('bundle:', file.relative)) + .on('end', () => console.log('-'.repeat(40))) + }) +}) diff --git a/example/gulp/single-bundle/src/node_modules/exclamation/excl.js b/example/gulp/reduce/src/node_modules/exclamation/excl.js similarity index 100% rename from example/gulp/single-bundle/src/node_modules/exclamation/excl.js rename to example/gulp/reduce/src/node_modules/exclamation/excl.js diff --git a/example/gulp/single-bundle/src/node_modules/exclamation/package.json b/example/gulp/reduce/src/node_modules/exclamation/package.json similarity index 100% rename from example/gulp/single-bundle/src/node_modules/exclamation/package.json rename to example/gulp/reduce/src/node_modules/exclamation/package.json diff --git a/example/gulp/single-bundle/src/page/hello/index.js b/example/gulp/reduce/src/page/hello/index.js similarity index 100% rename from example/gulp/single-bundle/src/page/hello/index.js rename to example/gulp/reduce/src/page/hello/index.js diff --git a/example/gulp/multiple-bundles/src/page/hi/index.js b/example/gulp/reduce/src/page/hi/index.js similarity index 100% rename from example/gulp/multiple-bundles/src/page/hi/index.js rename to example/gulp/reduce/src/page/hi/index.js diff --git a/example/gulp/single-bundle/src/web_modules/lib/world.js b/example/gulp/reduce/src/web_modules/lib/world.js similarity index 100% rename from example/gulp/single-bundle/src/web_modules/lib/world.js rename to example/gulp/reduce/src/web_modules/lib/world.js diff --git a/example/gulp/single-bundle/src/page/hi/index.js b/example/gulp/single-bundle/src/page/hi/index.js deleted file mode 100644 index 1eeefb8..0000000 --- a/example/gulp/single-bundle/src/page/hi/index.js +++ /dev/null @@ -1,3 +0,0 @@ -var world = require('lib/world') -module.exports = 'hi, ' + world - diff --git a/example/node_modules/reduce-js b/example/node_modules/reduce-js new file mode 120000 index 0000000..5207c3e --- /dev/null +++ b/example/node_modules/reduce-js @@ -0,0 +1 @@ +../../../reduce-js \ No newline at end of file diff --git a/example/without-gulp/build.js b/example/without-gulp/build.js index ac71b17..6eb8a92 100644 --- a/example/without-gulp/build.js +++ b/example/without-gulp/build.js @@ -1,18 +1,18 @@ 'use strict' -const reduce = require('../..') +const reduce = require('reduce-js') const del = require('del') const basedir = __dirname + '/src' const build = __dirname + '/build' del(build).then(function () { let b = reduce.create({ basedir: basedir }) - b.on('error', err => console.log(err)) - b.on('common.map', map => console.log(map)) reduce.src('*.js', { cwd: basedir }) .pipe(reduce.bundle(b, 'bundle.js')) .pipe(reduce.dest(build)) + .on('data', file => console.log('bundle:', file.relative)) + .on('end', () => console.log('DONE')) }) diff --git a/example/without-gulp/default-watch.js b/example/without-gulp/default-watch.js index cdd8299..cc746dd 100644 --- a/example/without-gulp/default-watch.js +++ b/example/without-gulp/default-watch.js @@ -1,6 +1,6 @@ 'use strict' -const reduce = require('../..') +const reduce = require('reduce-js') const del = require('del') const basedir = __dirname + '/src' diff --git a/example/without-gulp/default.js b/example/without-gulp/default.js index f636278..4e8560b 100644 --- a/example/without-gulp/default.js +++ b/example/without-gulp/default.js @@ -1,6 +1,6 @@ 'use strict' -const reduce = require('../..') +const reduce = require('reduce-js') const del = require('del') const basedir = __dirname + '/src' diff --git a/example/without-gulp/stream-input.js b/example/without-gulp/stream-input.js index 8c11d65..54a627d 100644 --- a/example/without-gulp/stream-input.js +++ b/example/without-gulp/stream-input.js @@ -1,6 +1,6 @@ 'use strict' -const reduce = require('../..') +const reduce = require('reduce-js') const del = require('del') const fs = require('fs') const source = require('vinyl-source-stream') diff --git a/example/without-gulp/watch.js b/example/without-gulp/watch.js index abfda21..a513d43 100644 --- a/example/without-gulp/watch.js +++ b/example/without-gulp/watch.js @@ -1,7 +1,6 @@ 'use strict' -const reduce = require('../..') -const path = require('path') +const reduce = require('reduce-js') const del = require('del') const basedir = __dirname + '/src' @@ -13,14 +12,14 @@ del(build).then(function () { packageCache: {}, fileCache: {}, }) + b.on('error', err => console.log(err.stack)) - b.on('error', err => console.log(err)) - b.on('common.map', map => console.log(map)) - - b.on('bundle-stream', function (bundleStream) { - bundleStream.pipe(reduce.dest(build)) - }) reduce.src('*.js', { cwd: basedir }) .pipe(reduce.watch(b, 'bundle.js', { entryGlob: '*.js' })) + .on('bundle', function (bundleStream) { + bundleStream.pipe(reduce.dest(build)) + .on('data', file => console.log('bundle:', file.relative)) + .on('end', () => console.log('-'.repeat(40))) + }) }) diff --git a/index.js b/index.js index 040f854..e64181e 100644 --- a/index.js +++ b/index.js @@ -102,6 +102,7 @@ function watch(b, opts, wopts) { next() }, function (next) { + b.on('bundle-stream', s => this.emit('bundle', s)) b.once('close', next) b.start() }