From 671f385e09c377df537c21b9c5d4ed3085040457 Mon Sep 17 00:00:00 2001 From: zoubin Date: Wed, 16 Sep 2015 18:10:48 +0800 Subject: [PATCH] tests done --- README.md | 198 ++++++++++++++---- example/gulpfile.js | 5 +- example/no-gulp.js | 30 +++ example/src/c/index.js | 2 +- example/watch-no-gulp.js | 27 +++ index.js | 8 +- lib/browserify.js | 7 +- lib/bundle.js | 10 +- lib/dedupify.js | 84 ++++---- lib/factor.js | 4 +- package.json | 4 +- test/common-entry.js | 53 +++++ test/dedupe-entry.js | 63 ++++++ test/dedupe.js | 63 ++++++ test/expected/common-entry/common.js | 1 + test/expected/common-entry/green.js | 1 + test/expected/common-entry/red.js | 1 + test/expected/dedupe-entry/common.js | 1 + test/expected/dedupe-entry/green.js | 1 + test/expected/dedupe-entry/red.js | 1 + test/expected/dedupe/blue-green.js | 1 + test/expected/dedupe/blue-red.js | 1 + test/expected/dedupe/common.js | 1 + .../expected/multi-bundles-no-common/green.js | 1 + test/expected/multi-bundles-no-common/red.js | 1 + test/expected/multi-bundles/common.js | 1 + test/expected/multi-bundles/green.js | 1 + test/expected/multi-bundles/red.js | 1 + test/expected/require-entry/blue-red.js | 1 + test/expected/require-entry/blue.js | 1 + test/expected/require-entry/common.js | 1 + test/multi-bundles-no-common.js | 52 +++++ test/multi-bundles.js | 53 +++++ test/require-entry.js | 63 ++++++ test/single-bundle.js | 16 +- test/src/common-entry/blue.js | 4 + test/src/common-entry/green.js | 4 + test/src/common-entry/red.js | 4 + test/src/dedupe-entry/green.js | 4 + test/src/dedupe-entry/red.js | 4 + test/src/dedupe/blue-green.js | 5 + test/src/dedupe/blue-red.js | 5 + test/src/dedupe/blue/blue.js | 4 + test/src/dedupe/blue/index.js | 4 + test/src/multi-bundles-no-common/green.js | 4 + test/src/multi-bundles-no-common/red.js | 4 + test/src/multi-bundles/green.js | 4 + test/src/multi-bundles/red.js | 4 + test/src/require-entry/blue-red.js | 5 + test/src/require-entry/blue.js | 4 + 50 files changed, 714 insertions(+), 108 deletions(-) create mode 100644 example/no-gulp.js create mode 100644 example/watch-no-gulp.js create mode 100644 test/common-entry.js create mode 100644 test/dedupe-entry.js create mode 100644 test/dedupe.js create mode 100644 test/expected/common-entry/common.js create mode 100644 test/expected/common-entry/green.js create mode 100644 test/expected/common-entry/red.js create mode 100644 test/expected/dedupe-entry/common.js create mode 100644 test/expected/dedupe-entry/green.js create mode 100644 test/expected/dedupe-entry/red.js create mode 100644 test/expected/dedupe/blue-green.js create mode 100644 test/expected/dedupe/blue-red.js create mode 100644 test/expected/dedupe/common.js create mode 100644 test/expected/multi-bundles-no-common/green.js create mode 100644 test/expected/multi-bundles-no-common/red.js create mode 100644 test/expected/multi-bundles/common.js create mode 100644 test/expected/multi-bundles/green.js create mode 100644 test/expected/multi-bundles/red.js create mode 100644 test/expected/require-entry/blue-red.js create mode 100644 test/expected/require-entry/blue.js create mode 100644 test/expected/require-entry/common.js create mode 100644 test/multi-bundles-no-common.js create mode 100644 test/multi-bundles.js create mode 100644 test/require-entry.js create mode 100644 test/src/common-entry/blue.js create mode 100644 test/src/common-entry/green.js create mode 100644 test/src/common-entry/red.js create mode 100644 test/src/dedupe-entry/green.js create mode 100644 test/src/dedupe-entry/red.js create mode 100644 test/src/dedupe/blue-green.js create mode 100644 test/src/dedupe/blue-red.js create mode 100644 test/src/dedupe/blue/blue.js create mode 100644 test/src/dedupe/blue/index.js create mode 100644 test/src/multi-bundles-no-common/green.js create mode 100644 test/src/multi-bundles-no-common/red.js create mode 100644 test/src/multi-bundles/green.js create mode 100644 test/src/multi-bundles/red.js create mode 100644 test/src/require-entry/blue-red.js create mode 100644 test/src/require-entry/blue.js diff --git a/README.md b/README.md index 22d6b07..acae832 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ which has a lot of plugins to transform such streams. It also handles some subtle problems arisen when using factor-bundle. -## Table of contents +# Table of contents - [Examples](#examples) - [Single bundle](#single-bundle) -## Examples +# Examples -### Single bundle +## Single bundle ```javascript gulp.task('default', ['clean'], function () { @@ -29,7 +29,7 @@ gulp.task('default', ['clean'], function () { }); ``` -### Watch single bundle +## Watch single bundle ```javascript gulp.task('watch', ['clean'], function (cb) { @@ -43,11 +43,12 @@ gulp.task('watch', ['clean'], function (cb) { }); ``` -### Multiple bundles +## Multiple bundles ```javascript var factorOpts = { outputs: ['a.js', 'b.js'], + common: 'common.js', }; gulp.task('factor', ['clean'], function () { return reduce.src('*.js', { basedir: basedir, factor: factorOpts }) @@ -59,11 +60,12 @@ gulp.task('factor', ['clean'], function () { }); ``` -### Watch multiple bundles +## Watch multiple bundles ```javascript var factorOpts = { outputs: ['a.js', 'b.js'], + common: 'common.js', }; gulp.task('factor.watch', ['clean'], function (cb) { reduce.watch() @@ -76,85 +78,84 @@ gulp.task('factor.watch', ['clean'], function (cb) { }); ``` -## API +# API -### reduce.src(patterns, bopts) +## reduce.src(patterns, bopts) Creates a vinyl file stream, which flows all the file objects, and can be transformed by gulp plugins. -#### patterns +### patterns Type: `String`, `Array` Used by [xglob](https://github.com/zoubin/xglob) to find entries. -#### bopts +### bopts Options to create the browserify instance. -New fields or fields with extra meanings are explained below. +Fields not explained in the following sections are the same with those in [browserify](https://github.com/substack/node-browserify#browserifyfiles--opts) -##### basedir +#### basedir Also used as the `cwd` field of the options passed to xglob. -##### factor +#### factor Type: `Object` -Options passed to [post-factor-bundle](https://github.com/zoubin/post-factor-bundle). +Options passed to [factor-bundle](https://github.com/substack/factor-bundle/). -(Perhaps move to `post-factor-bundle`?) +Fields not explained in the following sections are the same with those in [factor-bundle](https://github.com/substack/factor-bundle/#var-fr--factorfiles-opts) -New fields or fields with extra meanings are explained below. - -`common` +##### common Type: `String` -File name of the common bundle - -`commonFilter` - -Type: `String`, `Array`, `Function` - -If `String` or `Array`, -specified files will be packed into the common bundle. - -If `Function`, -it receives the tested filename, -and if it returns true, -that file will be packed into the common bundle. +File name of the common bundle. -### w = reduce.watch(watchifyOpts) +## w = reduce.watch(watchifyOpts) Creates a watch instance. `watchifyOpts` will be passed to `watchify`. -#### w.src(pattern, opts) +### w.src(pattern, opts) The same with `reduce.src`. -#### w.pipe(fn, arg1, arg2,...) +### w.pipe(fn, arg1, arg2,...) Like [lazypipe](https://github.com/OverZealous/lazypipe), just pass the stream constructor and its arguments to `.pipe`, and they will be called sequently with these arguments to create the pipeline. -## factor-bundle + +## reduce.dest + +The same with [gulp.dest](https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpdestpath-options) + +## reduce.lazypipe + +The same with [lazypipe](https://github.com/OverZealous/lazypipe) + +## reduce.run + +The same with [callback-sequence#run](https://github.com/zoubin/callback-sequence#sequenceruncallbacks-done) + +# factor-bundle Options for controlling the behaviour of packing (`factorOpts`) are specified as the `factor` field of the options object (`bopts`) passed to `.src`. -### Entries +## Entries There are two types of entires: browserify entries and factor entries. -#### Browserify entries +### Browserify entries Handled by browserify to generate all the text streams. @@ -163,7 +164,7 @@ which can be patterns used by [xglob](https://github.com/zoubin/xglob). Entry modules will be executed immediately when their containing outputs are loaded by the browser. -#### Factor entries +### Factor entries Handled by factor-bundle to group the text streams generarted by browserify, and create one bundle for each group. @@ -181,7 +182,7 @@ If you want to pack some browserify entries into the common bundle, such as modules for initiating all the site pages, you can specify `factorOpts.entries` and excluded them from it. -#### Outputs +## Outputs Each factor entry groups several modules together and create a text stream, which will be transformed into a vinyl stream. @@ -189,24 +190,31 @@ which will be transformed into a vinyl stream. You can specify the relative path (to the destination `gulp.dest()`) for each vinyl stream, through `factorOpts.outputs`. -#### The common vinyl stream +### The common vinyl stream The common vinyl stream is constructed from the text stream generated from the pipeline of browserify. You can specify its destination path through `factorOpts.common`. -If `factorOpts.common` is not defined but there are other vinyl streams generated by factor-bundle, +If `factorOpts.common` is not defined, +and there are no other vinyl streams generated by factor-bundle, `factorOpts.common` will be defaulted to `common.js`. Otherwise, no common streams will be outputed. -#### Factor vinyl streams +#### Entries in common + +Factor entries are always excluded from the common bundle. + +Browserify entries that are not specified as factor entries will be included in the common bundle. + +### Factor vinyl streams Factor vinyl streams are created by factor-bundle. You can specify their destination paths through `factorOpts.outputs`, which should pair with `factorOpts.entries`. -### Dedupe +# Dedupe Modules could have the exactly same contents, and thus some of them will be deduped. @@ -220,7 +228,7 @@ So, all non-entry deduped modules will be packed into the common bundle. Entries will never be deduped. -### Hash IDs +## Hash IDs Browserify uses numeric ids by default. @@ -238,7 +246,107 @@ and discarded by factor-bundle. Right now, nothing has been done to make things better. -## Watchify +# Watchify + +`reduce.src` generates a vinyl stream, +which could be transformed by gulp-plugins. + +However, `reduce.watch().src` generates a [lazypipe](https://github.com/OverZealous/lazypipe) instance, +and will bundle in the next tick. + +In normal mode, +you just `pipe` streams. + +```javascript +var factorOpts = { + outputs: ['a.js', 'b.js'], + common: 'common.js', +}; +gulp.task('factor', ['clean'], function () { + return reduce.src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(buffer()) + .pipe(uglify()) + .pipe(gulp.dest('build')); +}); +``` + +In watch mode, +you should `pipe` stream constructors. + +```javascript +var factorOpts = { + outputs: ['a.js', 'b.js'], + common: 'common.js', +}; +gulp.task('factor.watch', ['clean'], function (cb) { + reduce.watch() + .src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(buffer) + .pipe(uglify) + .pipe(gulp.dest, 'build'); +}); +``` + +You can use `lazypipe` to make things clear: + +```javascript +var reduce = require('reduce-js'); +var lazypipe = reduce.lazypipe() + .pipe(buffer) + .pipe(uglify) + .pipe(gulp.dest, 'build'); + +var factorOpts = { + outputs: ['a.js', 'b.js'], + common: 'common.js', +}; +gulp.task('factor', ['clean'], function () { + return reduce.src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(lazypipe()); +}); +gulp.task('factor.watch', ['clean'], function (cb) { + reduce.watch() + .src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(lazypipe); +}); +``` + +# No gulp + +Actually, `gulp` is not necessary. +`reduce.dest` can be always used in place of `gulp.dest`. +Use `reduce.run` to run the task. + +```javascript +var reduce = require('reduce-js'); +var path = require('path'); + +var basedir = path.join(__dirname, 'src'); +var factorOpts = { + outputs: ['a.js', 'b.js'], + common: 'common.js', +}; + +var del = require('del'); +reduce.run( + [clean, bundle], + function () { + console.log('DONE'); + } +); +function clean() { + return del(path.join(__dirname, 'build')); +} + +function bundle() { + return reduce + .on('log', console.log.bind(console)) + .on('error', console.log.bind(console)) + .src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(reduce.dest('build')); +} + +``` diff --git a/example/gulpfile.js b/example/gulpfile.js index b1cdccc..6b04677 100644 --- a/example/gulpfile.js +++ b/example/gulpfile.js @@ -16,6 +16,7 @@ var postTransforms = lazypipe() var factorOpts = { outputs: ['a.js', 'b.js'], + common: 'common.js', }; var onerror = function (err) { @@ -46,7 +47,7 @@ gulp.task('watch', ['clean'], function (cb) { }); gulp.task('lazypipe', ['clean'], function () { - return reduce.src('*.js', { basedir: basedir }) + return reduce.src('*.js', { basedir: basedir, factor: factorOpts }) .on('log', gutil.log.bind(gutil)) .on('error', onerror) .pipe(postTransforms()); @@ -56,7 +57,7 @@ gulp.task('lazypipe.watch', ['clean'], function (cb) { reduce.watch() .on('log', gutil.log.bind(gutil)) .on('error', onerror) - .src('*.js', { basedir: basedir }) + .src('*.js', { basedir: basedir, factor: factorOpts }) .pipe(postTransforms); }); diff --git a/example/no-gulp.js b/example/no-gulp.js new file mode 100644 index 0000000..dd494cb --- /dev/null +++ b/example/no-gulp.js @@ -0,0 +1,30 @@ +var reduce = require('..'); +var path = require('path'); + +var basedir = path.join(__dirname, 'src'); +var factorOpts = { + outputs: ['a.js', 'b.js'], + common: 'common.js', +}; + +var del = require('del'); + +reduce.run( + [clean, bundle], + function () { + console.log('DONE'); + } +); + +function clean() { + return del(path.join(__dirname, 'build')); +} + +function bundle() { + return reduce + .on('log', console.log.bind(console)) + .on('error', console.log.bind(console)) + .src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(reduce.dest('build')); +} + diff --git a/example/src/c/index.js b/example/src/c/index.js index e427572..beed402 100644 --- a/example/src/c/index.js +++ b/example/src/c/index.js @@ -1,2 +1,2 @@ -module.exports = 'cc'; +module.exports = 'c'; diff --git a/example/watch-no-gulp.js b/example/watch-no-gulp.js new file mode 100644 index 0000000..f9526b7 --- /dev/null +++ b/example/watch-no-gulp.js @@ -0,0 +1,27 @@ +var reduce = require('..'); +var path = require('path'); + +var basedir = path.join(__dirname, 'src'); +var factorOpts = { + outputs: ['a.js', 'b.js'], + common: 'common.js', +}; + +var del = require('del'); + +reduce.run( + [clean, bundle] +); + +function clean() { + return del(path.join(__dirname, 'build')); +} + +function bundle() { + reduce.watch() + .on('log', console.log.bind(console)) + .on('error', console.log.bind(console)) + .src('*.js', { basedir: basedir, factor: factorOpts }) + .pipe(reduce.dest, 'build'); +} + diff --git a/index.js b/index.js index d9d86c8..fb4639f 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ - -module.exports = require('./lib/reduce')(); -module.exports.watch = require('./lib/watch'); +exports = module.exports = require('./lib/reduce')(); +exports.watch = require('./lib/watch'); +exports.lazypipe = require('lazypipe'); +exports.dest = require('vinyl-fs').dest; +exports.run = require('callback-sequence').run; diff --git a/lib/browserify.js b/lib/browserify.js index 9c3d6e7..85514c6 100644 --- a/lib/browserify.js +++ b/lib/browserify.js @@ -18,9 +18,12 @@ module.exports = function (pattern, opts) { var entries = glob.sync(pattern, { cwd: bopts.basedir }); var b = browserify(entries, bopts); - b.plugin(dedupify); - var factorOpts = opts.factor || {}; + + b.plugin(dedupify, { + noCommon: !factorOpts.common && factorOpts.outputs, + }); + if (factorOpts.outputs) { mix(factorOpts, { basedir: bopts.basedir, diff --git a/lib/bundle.js b/lib/bundle.js index 5bcef5e..bd464e3 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,6 +1,6 @@ var source = require('vinyl-source-stream'); var merge = require('merge-stream'); -var Transform = require('stream').Transform; +var thr = require('through2'); module.exports = function (b, opts) { var stream = merge(); @@ -24,7 +24,7 @@ module.exports = function (b, opts) { } if (opts.outputs) { - var wait = createTransform(); + var wait = thr.obj(); stream.add(wait); b.once('factor.outputs', function (streams) { streams.forEach(function (s) { @@ -38,9 +38,3 @@ module.exports = function (b, opts) { return stream; }; -function createTransform() { - var ts = Transform({ objectMode: true }); - ts._transform = function () {}; - return ts; -} - diff --git a/lib/dedupify.js b/lib/dedupify.js index 72c4985..0973565 100644 --- a/lib/dedupify.js +++ b/lib/dedupify.js @@ -1,46 +1,50 @@ var thr = require('through2'); -module.exports = function (b) { - b.on('reset', dedupify.bind(null, b)); - dedupify(b); -}; +module.exports = function (b, opts) { + b.on('reset', dedupify); + dedupify(); + + function dedupify() { + var undef; + b.pipeline.get('dedupe').unshift(thr.obj(function (row, enc, next) { + /** + * If `entry` is deduped against another module, + * that module should always be packed together with `entry`, + * which will cause problems with `threshold` + * + */ + if (row.entry && row.dedupe) { + row.dedupe = undef; + row.dedupeIndex = undef; + } -function dedupify(b) { - var undef; - b.pipeline.get('dedupe').unshift(thr.obj(function (row, enc, next) { - /** - * If `entry` is deduped against another module, - * that module should always be packed together with `entry`, - * which will cause problems with `threshold` - * - */ - if (row.entry && row.dedupe) { - row.dedupe = undef; - row.dedupeIndex = undef; - } + /** + * If `entry` is factored into `common.js`, + * this module will be executed on each page, + * which probably is not desirable + * + */ + if (row.entry) { + row.common = false; + } - /** - * If `entry` is factored into `common.js`, - * this module will be executed on each page, - * which probably is not desirable - * - */ - if (row.entry) { - row.common = false; - } + /** + * If not `entry`, but `dedupe`, + * it must be bundled with the module deduped against + * So, we pack all of them into common + * Since the deduped `row` depends on its dedupe, + * if `row` is packed into common, + * its dedupe will also be there. + */ + if (!row.entry && row.dedupe) { + row.common = true; + } - /** - * If not `entry`, but `dedupe`, - * it must be bundled with the module deduped against - * So, we pack all of them into common - * Since the deduped `row` depends on its dedupe, - * if `row` is packed into common, - * its dedupe will also be there. - */ - if (!row.entry && row.dedupe) { - row.common = true; - } + if (opts && opts.noCommon) { + row.common = false; + } - next(null, row); - })); -} + next(null, row); + })); + } +}; diff --git a/lib/factor.js b/lib/factor.js index 64a2f76..06acce0 100644 --- a/lib/factor.js +++ b/lib/factor.js @@ -26,8 +26,8 @@ module.exports = function (b, opts) { }, {}); entries = typeof opts.entries === 'function' ? bentries.filter(opts.entries) - : pick(opts.entries.map(abs), entryMap); - commonFilter = unpick(entries, entryMap); + : Object.keys(pick(opts.entries.map(abs), entryMap)); + commonFilter = Object.keys(unpick(entries, entryMap)); } else { entries = bentries; commonFilter = []; diff --git a/package.json b/package.json index df09d25..8280ce1 100644 --- a/package.json +++ b/package.json @@ -24,23 +24,25 @@ "homepage": "https://github.com/zoubin/reduce-js#readme", "dependencies": { "browserify": "^11.1.0", + "callback-sequence": "^1.1.0", "factor-bundle": "^2.5.0", "lazypipe": "^1.0.1", "merge-stream": "^1.0.0", "through2": "^1.1.1", "util-mix": "^3.0.1", + "vinyl-fs": "^1.0.0", "vinyl-source-stream": "^1.1.0", "watchify": "^3.4.0", "xglob": "^1.0.0" }, "devDependencies": { - "callback-sequence": "^1.1.0", "del": "^2.0.2", "eslint": "^1.4.1", "gulp": "^3.9.0", "gulp-uglify": "^1.4.1", "gulp-util": "^3.0.6", "tap": "^1.4.1", + "util-equal": "^1.0.0", "vinyl-buffer": "^1.0.0" } } diff --git a/test/common-entry.js b/test/common-entry.js new file mode 100644 index 0000000..7c313e5 --- /dev/null +++ b/test/common-entry.js @@ -0,0 +1,53 @@ +var reduce = require('..'); +var test = require('tap').test; +var runSequence = require('callback-sequence').run; +var path = require('path'); +var del = require('del'); +var gutil = require('gulp-util'); +var buffer = require('vinyl-buffer'); +var uglify = require('gulp-uglify'); +var gulp = require('gulp'); +var equal = require('util-equal'); + +var fixtures = path.resolve.bind(path, __dirname); +var log = gutil.log.bind(gutil); +var dest = fixtures.bind(null, 'build', 'common-entry'); +var expect = fixtures.bind(null, 'expected', 'common-entry'); + +test('common-entry, browserify entries which are not factor entries should go to common', function(t) { + t.plan(1); + runSequence( + [clean, bundle], + function () { + equal( + [dest('common.js'), dest('green.js'), dest('red.js')], + [expect('common.js'), expect('green.js'), expect('red.js')], + function (res) { + t.ok(res); + } + ); + } + ); +}); + +function clean() { + return del(dest()); +} + +function bundle() { + var opts = { + basedir: fixtures('src', 'common-entry'), + factor: { + entries: ['green.js', 'red.js'], + outputs: ['green.js', 'red.js'], + common: 'common.js', + }, + }; + return reduce.src('*.js', opts) + .on('log', log) + .on('error', log) + .pipe(buffer()) + .pipe(uglify()) + .pipe(gulp.dest(dest())); +} + diff --git a/test/dedupe-entry.js b/test/dedupe-entry.js new file mode 100644 index 0000000..345f23d --- /dev/null +++ b/test/dedupe-entry.js @@ -0,0 +1,63 @@ +var reduce = require('..'); +var test = require('tap').test; +var runSequence = require('callback-sequence').run; +var path = require('path'); +var del = require('del'); +var gutil = require('gulp-util'); +var buffer = require('vinyl-buffer'); +var uglify = require('gulp-uglify'); +var gulp = require('gulp'); +var equal = require('util-equal'); + +var fixtures = path.resolve.bind(path, __dirname); +var log = gutil.log.bind(gutil); +function dest(file) { + if (file) { + return fixtures('build', 'dedupe-entry', file); + } + return fixtures('build', 'dedupe-entry'); +} +function expect(file) { + if (file) { + return fixtures('expected', 'dedupe-entry', file); + } + return fixtures('expected', 'dedupe-entry'); +} + +test('dedupe-entry, entry should never be deduped', function(t) { + t.plan(1); + runSequence( + [clean, bundle], + function () { + equal( + ['common.js', 'green.js', 'red.js'].map(dest), + ['common.js', 'green.js', 'red.js'].map(expect), + function (res) { + t.ok(res); + } + ); + } + ); +}); + +function clean() { + return del(dest()); +} + +function bundle() { + var opts = { + basedir: fixtures('src', 'dedupe-entry'), + factor: { + entries: ['green.js', 'red.js'], + outputs: ['green.js', 'red.js'], + common: 'common.js', + }, + }; + return reduce.src('*.js', opts) + .on('log', log) + .on('error', log) + .pipe(buffer()) + .pipe(uglify()) + .pipe(gulp.dest(dest())); +} + diff --git a/test/dedupe.js b/test/dedupe.js new file mode 100644 index 0000000..d0d5f46 --- /dev/null +++ b/test/dedupe.js @@ -0,0 +1,63 @@ +var reduce = require('..'); +var test = require('tap').test; +var runSequence = require('callback-sequence').run; +var path = require('path'); +var del = require('del'); +var gutil = require('gulp-util'); +var buffer = require('vinyl-buffer'); +var uglify = require('gulp-uglify'); +var gulp = require('gulp'); +var equal = require('util-equal'); + +var fixtures = path.resolve.bind(path, __dirname); +var log = gutil.log.bind(gutil); +function dest(file) { + if (file) { + return fixtures('build', 'dedupe', file); + } + return fixtures('build', 'dedupe'); +} +function expect(file) { + if (file) { + return fixtures('expected', 'dedupe', file); + } + return fixtures('expected', 'dedupe'); +} + +test('dedupe, dedupes should go to common', function(t) { + t.plan(1); + runSequence( + [clean, bundle], + function () { + equal( + ['common.js', 'blue-green.js', 'blue-red.js'].map(dest), + ['common.js', 'blue-green.js', 'blue-red.js'].map(expect), + function (res) { + t.ok(res); + } + ); + } + ); +}); + +function clean() { + return del(dest()); +} + +function bundle() { + var opts = { + basedir: fixtures('src', 'dedupe'), + factor: { + entries: ['blue-green.js', 'blue-red.js'], + outputs: ['blue-green.js', 'blue-red.js'], + common: 'common.js', + }, + }; + return reduce.src('*.js', opts) + .on('log', log) + .on('error', log) + .pipe(buffer()) + .pipe(uglify()) + .pipe(gulp.dest(dest())); +} + diff --git a/test/expected/common-entry/common.js b/test/expected/common-entry/common.js new file mode 100644 index 0000000..e1008f6 --- /dev/null +++ b/test/expected/common-entry/common.js @@ -0,0 +1 @@ +require=function r(e,f,n){function o(u,i){if(!f[u]){if(!e[u]){var c="function"==typeof require&&require;if(!i&&c)return c(u,!0);if(t)return t(u,!0);var a=new Error("Cannot find module '"+u+"'");throw a.code="MODULE_NOT_FOUND",a}var l=f[u]={exports:{}};e[u][0].call(l.exports,function(r){var f=e[u][1][r];return o(f?f:r)},l,l.exports,r,e,f,n)}return f[u].exports}for(var t="function"==typeof require&&require,u=0;u