├── .eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── README.md ├── changelog.md ├── example ├── .gitignore ├── node_modules │ └── reduce-js ├── package.json ├── reduce │ ├── build.js │ └── src │ │ ├── a.js │ │ ├── b.js │ │ └── c │ │ └── index.js └── work-with-gulp │ ├── gulpfile.js │ └── src │ ├── node_modules │ ├── exclamation │ │ ├── excl.js │ │ └── package.json │ └── world │ │ └── index.js │ └── page │ ├── hello │ └── index.js │ └── hi │ └── index.js ├── index.js ├── package.json └── test ├── expected ├── multi-bundles │ ├── common.js │ ├── green.js │ └── red.js ├── single-bundle │ └── bundle.js ├── stream │ └── bundle.js └── watch │ ├── a.js │ ├── b.js │ └── common.js ├── multi-bundles.js ├── single-bundle.js ├── src ├── multi-bundles │ ├── green.js │ └── red.js ├── node_modules │ └── colors │ │ └── index.js ├── single-bundle │ ├── green.js │ └── red.js └── watch │ ├── a.js │ ├── b.js │ └── c │ └── index.js └── watch.js /.eslintrc: -------------------------------------------------------------------------------- 1 | --- 2 | ecmaFeatures: 3 | modules: true 4 | 5 | env: 6 | browser: true 7 | node: true 8 | es6: true 9 | 10 | rules: 11 | comma-dangle: [2, "always-multiline"] 12 | no-dupe-args: 2 13 | no-dupe-keys: 2 14 | no-duplicate-case: 2 15 | no-empty-character-class: 2 16 | no-ex-assign: 2 17 | no-extra-boolean-cast: 2 18 | no-extra-parens: 2 19 | no-extra-semi: 2 20 | no-func-assign: 2 21 | no-inner-declarations: 2 22 | no-invalid-regexp: 2 23 | no-irregular-whitespace: 2 24 | no-negated-in-lhs: 2 25 | no-obj-calls: 2 26 | no-regex-spaces: 2 27 | no-sparse-arrays: 2 28 | no-unreachable: 2 29 | use-isnan: 2 30 | valid-typeof: 2 31 | no-unexpected-multiline: 2 32 | no-constant-condition: 2 33 | no-control-regex: 2 34 | no-debugger: 2 35 | # code style 36 | consistent-return: 0 37 | curly: [2, "multi-line"] 38 | default-case: 2 39 | dot-notation: 2 40 | dot-location: [2, "property"] 41 | eqeqeq: [2, "allow-null"] 42 | no-else-return: 2 43 | no-lone-blocks: 2 44 | no-loop-func: 2 45 | no-multi-spaces: 2 46 | no-multi-str: 2 47 | no-proto: 2 48 | no-redeclare: 2 49 | no-return-assign: 2 50 | no-sequences: 2 51 | no-throw-literal: 2 52 | no-unused-expressions: 2 53 | no-void: 2 54 | no-warning-comments: [2, { "terms": ["todo", "fixme", "xxx"], "location": "start" }] 55 | no-with: 2 56 | radix: 2 57 | no-delete-var: 2 58 | no-shadow-restricted-names: 2 59 | no-shadow: 2 60 | no-undef: 2 61 | no-unused-vars: 2 62 | brace-style: [2, "1tbs", { "allowSingleLine": true }] 63 | comma-spacing: 2 64 | comma-style: 2 65 | indent: [2, 2] 66 | key-spacing: 2 67 | max-nested-callbacks: [2, 5] 68 | no-lonely-if: 2 69 | no-mixed-spaces-and-tabs: 2 70 | no-nested-ternary: 2 71 | no-spaced-func: 2 72 | no-trailing-spaces: 2 73 | one-var: [2, "never"] 74 | operator-linebreak: 2 75 | quote-props: [2, "as-needed"] 76 | quotes: [2, "single", "avoid-escape"] 77 | semi: [2, "never"] 78 | keyword-spacing: 2 79 | space-before-blocks: 2 80 | space-infix-ops: 2 81 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | .DS_Store 3 | npm-debug.log 4 | build/ 5 | coverage/ 6 | .nyc_output/ 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | changelog.md 3 | coverage/ 4 | example/ 5 | test/ 6 | .* 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "stable" 4 | - "5" 5 | - "4" 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # reduce-js 2 | [![version](https://img.shields.io/npm/v/reduce-js.svg)](https://www.npmjs.org/package/reduce-js) 3 | [![status](https://travis-ci.org/reducejs/reduce-js.svg?branch=master)](https://travis-ci.org/reducejs/reduce-js) 4 | [![coverage](https://img.shields.io/coveralls/reducejs/reduce-js.svg)](https://coveralls.io/github/reducejs/reduce-js) 5 | [![dependencies](https://david-dm.org/reducejs/reduce-js.svg)](https://david-dm.org/reducejs/reduce-js) 6 | [![devDependencies](https://david-dm.org/reducejs/reduce-js/dev-status.svg)](https://david-dm.org/reducejs/reduce-js#info=devDependencies) 7 | ![node](https://img.shields.io/node/v/reduce-js.svg) 8 | 9 | Augment [`browserify`] with the following features: 10 | 11 | * Accept patterns to add entries. 12 | * Use [`watchify2`] to watch files, which is able to detect new entries. 13 | * Use [`common-bundle`] to pack modules by default, 14 | which make `b.bundle()` output a stream manipulatable by [`gulp`] plugins. 15 | 16 | ## Example 17 | Suppose we want to create one bundle for each js file in `/path/to/src`, 18 | and an additional common bundle to hold modules shared among them. 19 | 20 | ```js 21 | const reduce = require('reduce-js') 22 | const path = require('path') 23 | const del = require('del') 24 | const uglify = require('gulp-uglify') 25 | 26 | bundle(createBundler()) 27 | 28 | function createBundler(watch) { 29 | var b = reduce.create( 30 | /* glob for entries */ 31 | '*.js', 32 | 33 | /* options for browserify */ 34 | { 35 | basedir: path.join(__dirname, 'src'), 36 | cache: {}, 37 | packageCache: {}, 38 | }, 39 | 40 | /* options for common-bundle */ 41 | // single bundle 42 | // 'bundle.js', 43 | // multiple bundles 44 | { 45 | groups: '*.js', 46 | common: 'common.js', 47 | }, 48 | 49 | /* options for watchify2 */ 50 | watch && { entryGlob: '*.js' } 51 | ) 52 | return b 53 | } 54 | 55 | function bundle(b) { 56 | var build = path.join(__dirname, 'build') 57 | del.sync(build) 58 | return b.bundle().pipe(uglify()).pipe(b.dest(build)) 59 | } 60 | 61 | 62 | ``` 63 | 64 | To watch file changes: 65 | ```js 66 | var b = createBundler(true) 67 | b.on('update', function update() { 68 | bundle(b) 69 | return update 70 | }()) 71 | 72 | ``` 73 | 74 | To work with gulp: 75 | 76 | ```js 77 | var gulp = require('gulp') 78 | gulp.task('build', function () { 79 | return bundle(createBundler()) 80 | }) 81 | 82 | gulp.task('watch', function (cb) { 83 | var b = createBundler(true) 84 | b.on('update', function update() { 85 | bundle(b) 86 | return update 87 | }()) 88 | b.on('close', cb) 89 | }) 90 | 91 | ``` 92 | 93 | 94 | ## API 95 | 96 | ```javascript 97 | var reduce = require('reduce-js') 98 | var b = reduce.create(entries, browserifyOptions, bundleOptions, watchifyOptions) 99 | 100 | ``` 101 | 102 | ### reduce.create(entries, browserifyOptions, bundleOptions, watchifyOptions) 103 | Return a [`browserify`] instance. 104 | 105 | * `entries`: patterns to locate input files. Check [`globby`] for more details. 106 | * `browserifyOptions`: options for [`browserify`]. 107 | * `bundleOptions`: options for [`common-bundle`]. 108 | * `watchifyOptions`: options for [`watchify2`]. If truthy, file changes are watched. 109 | 110 | ### b.bundle() 111 | Return a [`vinyl`] stream, 112 | which can be processed by gulp plugins. 113 | 114 | ```js 115 | b.bundle().pipe(require('gulp-uglify')()).pipe(b.dest('build')) 116 | 117 | ``` 118 | ### b.dest(outFolder, options) 119 | The same with [`gulp.dest`]. 120 | 121 | ## Related 122 | 123 | * [`browserify`] 124 | * [`reduce-css`] 125 | 126 | 127 | [`reduce-css`]: https://github.com/reducejs/reduce-css 128 | [`browserify`]: https://www.npmjs.com/package/browserify 129 | [`common-bundle`]: https://www.npmjs.com/package/common-bundle 130 | [`gulp`]: https://www.npmjs.com/package/gulp 131 | [`watchify`]: https://github.com/substack/watchify 132 | [`watchify2`]: https://github.com/reducejs/watchify2 133 | [`gulp.dest`]: https://github.com/gulpjs/vinyl-fs#destfolder-options 134 | 135 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## [v10.1.0](https://github.com/reducejs/reduce-js/commit/19d24f0) (2016-06-12) 4 | 5 | * [[`eaf09a4`](https://github.com/reducejs/reduce-js/commit/eaf09a4)] move watchify to optionalDependencies 6 | 7 | ## [v10.0.2](https://github.com/reducejs/reduce-js/commit/6bf6d2b) (2016-05-26) 8 | 9 | * [[`49c7d65`](https://github.com/reducejs/reduce-js/commit/49c7d65)] Fix default arguments 10 | 11 | * [[`853ae6a`](https://github.com/reducejs/reduce-js/commit/853ae6a)] changelog 12 | 13 | ## [v10.0.1](https://github.com/reducejs/reduce-js/commit/fe56e5e) (2016-05-20) 14 | 15 | * [[`b1929a5`](https://github.com/reducejs/reduce-js/commit/b1929a5)] fix badge 16 | 17 | * [[`f07e522`](https://github.com/reducejs/reduce-js/commit/f07e522)] changelog 18 | 19 | ## [v10.0.0](https://github.com/reducejs/reduce-js/commit/c2393bc) (2016-05-20) 20 | 21 | * [[`75ad9b6`](https://github.com/reducejs/reduce-js/commit/75ad9b6)] travis: 5 22 | 23 | * [[`5be40eb`](https://github.com/reducejs/reduce-js/commit/5be40eb)] refactor 24 | 25 | ## [v9.4.0](https://github.com/reducejs/reduce-js/commit/68b5e1b) (2016-03-09) 26 | 27 | * [[`0b7001e`](https://github.com/reducejs/reduce-js/commit/0b7001e)] update: common-bundle 28 | 29 | * [[`bfa9f6d`](https://github.com/reducejs/reduce-js/commit/bfa9f6d)] changelog 30 | 31 | ## [v9.3.0](https://github.com/reducejs/reduce-js/commit/02110ac) (2016-03-06) 32 | 33 | * [[`3ab718e`](https://github.com/reducejs/reduce-js/commit/3ab718e)] example 34 | 35 | * [[`190bae6`](https://github.com/reducejs/reduce-js/commit/190bae6)] test: watch bundle event 36 | 37 | * [[`b84ae90`](https://github.com/reducejs/reduce-js/commit/b84ae90)] readme 38 | 39 | * [[`e88c550`](https://github.com/reducejs/reduce-js/commit/e88c550)] Feature: bundle event on watch stream 40 | 41 | * [[`312d13e`](https://github.com/reducejs/reduce-js/commit/312d13e)] Fix coveralls 42 | 43 | * [[`84a9802`](https://github.com/reducejs/reduce-js/commit/84a9802)] readme: typo 44 | 45 | * [[`7e5cb10`](https://github.com/reducejs/reduce-js/commit/7e5cb10)] readme 46 | 47 | * [[`4e5acd8`](https://github.com/reducejs/reduce-js/commit/4e5acd8)] changelog 48 | 49 | ## [v9.2.0](https://github.com/reducejs/reduce-js/commit/19c3a56) (2016-03-05) 50 | 51 | * [[`d02cf99`](https://github.com/reducejs/reduce-js/commit/d02cf99)] Feature: export browserify 52 | 53 | * [[`ee541a2`](https://github.com/reducejs/reduce-js/commit/ee541a2)] readme: typo 54 | 55 | * [[`3c9e0ba`](https://github.com/reducejs/reduce-js/commit/3c9e0ba)] readme: typo 56 | 57 | * [[`7034089`](https://github.com/reducejs/reduce-js/commit/7034089)] readme: fix typo 58 | 59 | * [[`67423e0`](https://github.com/reducejs/reduce-js/commit/67423e0)] changelog 60 | 61 | ## [v9.1.0](https://github.com/reducejs/reduce-js/commit/be80797) (2016-03-03) 62 | 63 | * [[`de4c82d`](https://github.com/reducejs/reduce-js/commit/de4c82d)] Fix: same logic 64 | 65 | * [[`72c7b96`](https://github.com/reducejs/reduce-js/commit/72c7b96)] eslint 66 | 67 | * [[`f0db6e7`](https://github.com/reducejs/reduce-js/commit/f0db6e7)] Feature: bundle and watch support stream and buffer 68 | 69 | * [[`2642200`](https://github.com/reducejs/reduce-js/commit/2642200)] changelog 70 | 71 | ## [v9.0.0](https://github.com/reducejs/reduce-js/commit/2b47c51) (2016-03-01) 72 | 73 | * [[`f7ce3ca`](https://github.com/reducejs/reduce-js/commit/f7ce3ca)] Refactor: even simpler api 74 | 75 | * [[`3f5a583`](https://github.com/reducejs/reduce-js/commit/3f5a583)] changelog 76 | 77 | ## [v8.1.0](https://github.com/reducejs/reduce-js/commit/9d0e9bc) (2016-02-26) 78 | 79 | * [[`92b35f6`](https://github.com/reducejs/reduce-js/commit/92b35f6)] Update: common-bundle 80 | 81 | * [[`16bb62a`](https://github.com/reducejs/reduce-js/commit/16bb62a)] changelog 82 | 83 | ## [v8.0.1](https://github.com/reducejs/reduce-js/commit/7999b39) (2016-02-24) 84 | 85 | * [[`f20870e`](https://github.com/reducejs/reduce-js/commit/f20870e)] Fix: watch output.pipe should be chainable 86 | 87 | * [[`7192f8e`](https://github.com/reducejs/reduce-js/commit/7192f8e)] Update README.md 88 | 89 | * [[`de82fb2`](https://github.com/reducejs/reduce-js/commit/de82fb2)] Update README.md 90 | 91 | * [[`42f77d3`](https://github.com/reducejs/reduce-js/commit/42f77d3)] changelog 92 | 93 | ## [v8.0.0](https://github.com/reducejs/reduce-js/commit/c8e5be2) (2016-02-23) 94 | 95 | * [[`54ce9f4`](https://github.com/reducejs/reduce-js/commit/54ce9f4)] Refactor: simple API 96 | 97 | * [[`24347b0`](https://github.com/reducejs/reduce-js/commit/24347b0)] changelog 98 | 99 | ## [v7.0.2](https://github.com/reducejs/reduce-js/commit/5359ad1) (2016-02-18) 100 | 101 | * [[`a615c0c`](https://github.com/reducejs/reduce-js/commit/a615c0c)] Update common-bundle 102 | 103 | ## [v7.0.1](https://github.com/reducejs/reduce-js/commit/ee19ea1) (2016-02-18) 104 | 105 | * [[`e942c56`](https://github.com/reducejs/reduce-js/commit/e942c56)] Fix links 106 | 107 | * [[`c48c912`](https://github.com/reducejs/reduce-js/commit/c48c912)] CHANGELOG 108 | 109 | ## [v7.0.0](https://github.com/reducejs/reduce-js/commit/91087c3) (2016-02-18) 110 | 111 | * [[`8ab74ad`](https://github.com/reducejs/reduce-js/commit/8ab74ad)] update watchify2 112 | 113 | * [[`449bd46`](https://github.com/reducejs/reduce-js/commit/449bd46)] Use watchify2 114 | 115 | ## [v6.0.0](https://github.com/reducejs/reduce-js/commit/cd80d02) (2016-01-18) 116 | 117 | * [[`0cf0d3f`](https://github.com/reducejs/reduce-js/commit/0cf0d3f)] Update browserify, tap 118 | 119 | * [[`4b062ef`](https://github.com/reducejs/reduce-js/commit/4b062ef)] CHANGELOG 120 | 121 | ## [v5.0.2](https://github.com/reducejs/reduce-js/commit/55aab48) (2015-12-31) 122 | 123 | * [[`ca02f96`](https://github.com/reducejs/reduce-js/commit/ca02f96)] Update tap 124 | 125 | * [[`01d7b58`](https://github.com/reducejs/reduce-js/commit/01d7b58)] Allow null entries 126 | 127 | * [[`6bfd10e`](https://github.com/reducejs/reduce-js/commit/6bfd10e)] CHANGELOG 128 | 129 | ## [v5.0.1](https://github.com/reducejs/reduce-js/commit/6e9567d) (2015-12-29) 130 | 131 | * [[`1bb5ff9`](https://github.com/reducejs/reduce-js/commit/1bb5ff9)] Fix sugar constructor Reduce 132 | 133 | * [[`754c878`](https://github.com/reducejs/reduce-js/commit/754c878)] CHANGELOG 134 | 135 | ## [v5.0.0](https://github.com/reducejs/reduce-js/commit/d0cd108) (2015-12-29) 136 | 137 | * [[`22f122f`](https://github.com/reducejs/reduce-js/commit/22f122f)] Fix readme 138 | 139 | * [[`a906e63`](https://github.com/reducejs/reduce-js/commit/a906e63)] Update common-bundle 140 | 141 | * [[`5e7d7a5`](https://github.com/reducejs/reduce-js/commit/5e7d7a5)] Compatible with non-pattern entries 142 | 143 | * [[`7744a74`](https://github.com/reducejs/reduce-js/commit/7744a74)] Fix keywords 144 | 145 | * [[`85fa005`](https://github.com/reducejs/reduce-js/commit/85fa005)] Use common-bundle instead of factor-vinylify 146 | 147 | * [[`54304e6`](https://github.com/reducejs/reduce-js/commit/54304e6)] Add more examples 148 | 149 | * [[`1e14f57`](https://github.com/reducejs/reduce-js/commit/1e14f57)] CHANGELOG 150 | 151 | ## [v4.0.0](https://github.com/reducejs/reduce-js/commit/8595c40) (2015-12-14) 152 | 153 | * [[`5a80976`](https://github.com/reducejs/reduce-js/commit/5a80976)] Update deps 154 | 155 | * [[`ec8f8d7`](https://github.com/reducejs/reduce-js/commit/ec8f8d7)] CHANGELOG 156 | 157 | ## [v3.0.1](https://github.com/reducejs/reduce-js/commit/19f5240) (2015-11-10) 158 | 159 | * [[`fad55c9`](https://github.com/reducejs/reduce-js/commit/fad55c9)] update gulp-tape 160 | 161 | * [[`c2e7a87`](https://github.com/reducejs/reduce-js/commit/c2e7a87)] export Reduce 162 | 163 | * [[`b53d252`](https://github.com/reducejs/reduce-js/commit/b53d252)] changelog 164 | 165 | ## [v3.0.0](https://github.com/reducejs/reduce-js/commit/a82437f) (2015-11-04) 166 | 167 | ## [v2.1.1](https://github.com/reducejs/reduce-js/commit/4258bdf) (2015-11-02) 168 | 169 | * [[`10edaad`](https://github.com/reducejs/reduce-js/commit/10edaad)] use factor-vinylify@3.0.0 170 | 171 | ## [v2.1.0](https://github.com/reducejs/reduce-js/commit/429baae) (2015-10-30) 172 | 173 | * [[`19b3f9f`](https://github.com/reducejs/reduce-js/commit/19b3f9f)] fix watch tests 174 | 175 | * [[`99e4f74`](https://github.com/reducejs/reduce-js/commit/99e4f74)] update vinyl-fs 176 | 177 | * [[`67d60cb`](https://github.com/reducejs/reduce-js/commit/67d60cb)] emit 'instance', propagate the browserify instance 178 | 179 | use browserify 12 180 | 181 | * [[`c12fbdc`](https://github.com/reducejs/reduce-js/commit/c12fbdc)] del unused var 182 | 183 | * [[`16b5929`](https://github.com/reducejs/reduce-js/commit/16b5929)] example, lazypipe 184 | 185 | ## [v2.0.0](https://github.com/reducejs/reduce-js/commit/d4bf4db) (2015-09-24) 186 | 187 | * [[`9501a35`](https://github.com/reducejs/reduce-js/commit/9501a35)] watch 188 | 189 | * [[`afdcaf2`](https://github.com/reducejs/reduce-js/commit/afdcaf2)] use factor-vinylify 190 | 191 | * [[`a195206`](https://github.com/reducejs/reduce-js/commit/a195206)] readme 192 | 193 | * [[`c8a61f2`](https://github.com/reducejs/reduce-js/commit/c8a61f2)] readme 194 | 195 | ## [v1.0.0](https://github.com/reducejs/reduce-js/commit/b560269) (2015-09-16) 196 | 197 | * [[`9b56649`](https://github.com/reducejs/reduce-js/commit/9b56649)] readme toc 198 | 199 | * [[`671f385`](https://github.com/reducejs/reduce-js/commit/671f385)] tests done 200 | 201 | * [[`c384b9b`](https://github.com/reducejs/reduce-js/commit/c384b9b)] more tests needed 202 | 203 | * [[`9797c12`](https://github.com/reducejs/reduce-js/commit/9797c12)] basics. no tests yet 204 | 205 | * [[`15050f9`](https://github.com/reducejs/reduce-js/commit/15050f9)] Initial commit 206 | 207 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/* 2 | !/node_modules/reduce-js 3 | -------------------------------------------------------------------------------- /example/node_modules/reduce-js: -------------------------------------------------------------------------------- 1 | ../../../reduce-js -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "gulp": "^3.9.0", 4 | "gulp-uglify": "^1.5.3", 5 | "gulp-util": "^3.0.7" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /example/reduce/build.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const reduce = require('reduce-js') 4 | const path = require('path') 5 | const del = require('del') 6 | const uglify = require('gulp-uglify') 7 | 8 | var i = process.argv.indexOf('-w') 9 | if (i === -1) { 10 | i = process.argv.indexOf('--watch') 11 | } 12 | var needWatch = i > -1 13 | if (needWatch) { 14 | var b = createBundler(true) 15 | b.on('update', function update() { 16 | bundle(b) 17 | return update 18 | }()) 19 | } else { 20 | bundle(createBundler()) 21 | } 22 | function createBundler(watch) { 23 | var b = reduce.create( 24 | /* glob for entries */ 25 | '*.js', 26 | 27 | /* options for depsify */ 28 | { 29 | basedir: path.join(__dirname, 'src'), 30 | cache: {}, 31 | packageCache: {}, 32 | }, 33 | 34 | /* options for common-bundle */ 35 | // single bundle 36 | // 'bundle.js', 37 | // multiple bundles 38 | { 39 | groups: '*.js', 40 | common: 'common.js', 41 | }, 42 | 43 | /* options for watchify2 */ 44 | watch && { entryGlob: '*.js' } 45 | ) 46 | return b 47 | } 48 | 49 | function bundle(b) { 50 | var build = path.join(__dirname, 'build') 51 | del.sync(build) 52 | return b.bundle().pipe(uglify()).pipe(b.dest(build)) 53 | } 54 | 55 | -------------------------------------------------------------------------------- /example/reduce/src/a.js: -------------------------------------------------------------------------------- 1 | require('./c'); 2 | 3 | module.exports = 'a'; 4 | -------------------------------------------------------------------------------- /example/reduce/src/b.js: -------------------------------------------------------------------------------- 1 | require('./c'); 2 | 3 | module.exports = 'b'; 4 | -------------------------------------------------------------------------------- /example/reduce/src/c/index.js: -------------------------------------------------------------------------------- 1 | module.exports = 'c'; 2 | 3 | -------------------------------------------------------------------------------- /example/work-with-gulp/gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const reduce = require('reduce-js') 4 | const gulp = require('gulp') 5 | const path = require('path') 6 | const del = require('del') 7 | const uglify = require('gulp-uglify') 8 | const gutil = require('gulp-util') 9 | const Transform = require('stream').Transform 10 | 11 | gulp.task('build', function () { 12 | return bundle(createBundler()) 13 | }) 14 | 15 | gulp.task('watch', function (cb) { 16 | var b = createBundler(true) 17 | b.on('update', function update() { 18 | bundle(b) 19 | return update 20 | }()) 21 | b.on('close', cb) 22 | }) 23 | 24 | function createBundler(watch) { 25 | var b = reduce.create( 26 | /* glob for entries */ 27 | 'page/**/index.js', 28 | 29 | /* options for depsify */ 30 | { 31 | basedir: path.join(__dirname, 'src'), 32 | cache: {}, 33 | packageCache: {}, 34 | }, 35 | 36 | /* options for common-bundle */ 37 | // single bundle 38 | // 'bundle.js', 39 | // multiple bundles 40 | { 41 | groups: 'page/**/index.js', 42 | common: 'common.js', 43 | }, 44 | 45 | /* options for watchify2 */ 46 | watch && { entryGlob: 'page/**/index.js' } 47 | ) 48 | return b 49 | } 50 | 51 | function bundle(b) { 52 | var startTime = Date.now() 53 | log('Start bundling') 54 | var build = path.join(__dirname, 'build') 55 | del.sync(build) 56 | return b.bundle().on('error', log) 57 | .pipe(Transform({ 58 | objectMode: true, 59 | transform: function (file, enc, next) { 60 | log('-', file.relative, file.contents.length, 'bytes') 61 | next(null, file) 62 | } 63 | })) 64 | .pipe(uglify()) 65 | .pipe(b.dest(build)) 66 | .on('end', () => log('End bundling in', Date.now() - startTime, 'ms')) 67 | } 68 | 69 | function log() { 70 | gutil.log.apply(gutil, [].map.call(arguments, function (msg) { 71 | if (typeof msg === 'string') { 72 | return msg 73 | } 74 | if (msg.stack) { 75 | return msg.stack 76 | } 77 | return JSON.stringify(msg, null, 2) 78 | })) 79 | } 80 | -------------------------------------------------------------------------------- /example/work-with-gulp/src/node_modules/exclamation/excl.js: -------------------------------------------------------------------------------- 1 | module.exports = '!' 2 | 3 | -------------------------------------------------------------------------------- /example/work-with-gulp/src/node_modules/exclamation/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "excl" 3 | } 4 | -------------------------------------------------------------------------------- /example/work-with-gulp/src/node_modules/world/index.js: -------------------------------------------------------------------------------- 1 | module.exports = 'world' + require('exclamation') 2 | -------------------------------------------------------------------------------- /example/work-with-gulp/src/page/hello/index.js: -------------------------------------------------------------------------------- 1 | var world = require('world') 2 | module.exports = 'hello, ' + world 3 | 4 | -------------------------------------------------------------------------------- /example/work-with-gulp/src/page/hi/index.js: -------------------------------------------------------------------------------- 1 | var world = require('world') 2 | module.exports = 'hi, ' + world 3 | 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const browserify = require('browserify') 4 | const vfs = require('vinyl-fs') 5 | const buffer = require('vinyl-buffer') 6 | const glob = require('globby') 7 | 8 | function bundler(b, opts) { 9 | b.plugin(require('common-bundle'), opts) 10 | b.on('reset', function reset() { 11 | b.pipeline.push(buffer()) 12 | return reset 13 | }()) 14 | b.on('bundle', output => { 15 | output.on('error', err => delete err.stream) 16 | }) 17 | } 18 | 19 | function watchify(b, opts) { 20 | b.plugin(require('watchify2'), opts) 21 | var close = b.close 22 | b.close = function () { 23 | close() 24 | b.emit('close') 25 | } 26 | } 27 | 28 | function create(entries, opts, bundleOptions, watchOpts) { 29 | if (typeof entries !== 'string' && !Array.isArray(entries)) { 30 | watchOpts = bundleOptions 31 | bundleOptions = opts 32 | opts = entries 33 | entries = null 34 | } 35 | opts = opts || {} 36 | var b = browserify(opts) 37 | if (entries) { 38 | glob.sync(entries, { cwd: b._options.basedir }).forEach(function (file) { 39 | b.add(file) 40 | }) 41 | } 42 | b.plugin(bundler, bundleOptions) 43 | if (watchOpts) { 44 | b.plugin(watchify, typeof watchOpts === 'object' ? watchOpts : {}) 45 | } 46 | b.dest = vfs.dest 47 | return b 48 | } 49 | 50 | module.exports = { 51 | bundler, 52 | watchify, 53 | create, 54 | } 55 | 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "reduce-js", 3 | "version": "10.1.0", 4 | "description": "Use browserify and factor-bundle to pack node-style modules into a single bundle or multiple bundles", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "npm run lint && tap test/*.js", 8 | "cov": "tap --cov test/*.js", 9 | "lint": "eslint *.js 'lib/**/*.js' test/*.js bin/*.js", 10 | "coveralls": "COVERALLS_REPO_TOKEN=1cnyqPV46MAutCfQZdwIMqa4LyYUEi2hQ npm run cov" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/reducejs/reduce-js.git" 15 | }, 16 | "keywords": [ 17 | "browserify", 18 | "common-bundle", 19 | "gulp", 20 | "watchify" 21 | ], 22 | "author": "zoubin", 23 | "license": "MIT", 24 | "engines": { 25 | "node": ">=4.2.1" 26 | }, 27 | "bugs": { 28 | "url": "https://github.com/reducejs/reduce-js/issues" 29 | }, 30 | "homepage": "https://github.com/reducejs/reduce-js#readme", 31 | "optionalDependencies": { 32 | "watchify2": "^0.1.0" 33 | }, 34 | "dependencies": { 35 | "browserify": "^13.0.0", 36 | "common-bundle": "^0.5.0", 37 | "globby": "^4.1.0", 38 | "vinyl-buffer": "^1.0.0", 39 | "vinyl-fs": "^2.2.1" 40 | }, 41 | "devDependencies": { 42 | "del": "^2.0.2", 43 | "eslint": "^2.1.0", 44 | "mkdirp": "^0.5.1", 45 | "tap": "^5.0.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/expected/multi-bundles/common.js: -------------------------------------------------------------------------------- 1 | require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o setTimeout(next, 50)) 50 | return update 51 | }()) 52 | 53 | function next() { 54 | let c = readDest('c.js') 55 | t.equal( 56 | run(c + readDest('a.js')), 57 | pool.a + pool.c 58 | ) 59 | t.equal( 60 | run(c + readDest('b.js')), 61 | pool.b + pool.c 62 | ) 63 | if (!--count) { 64 | return b.close() 65 | } 66 | let file = [src('c.js')].concat(entries)[count % 3] 67 | let k = path.basename(file, '.js') 68 | let n = Math.floor(Math.random() * 10) + 1 + pool[k] 69 | write(file, n) 70 | } 71 | 72 | }) 73 | 74 | function run (s) { 75 | let output = 0 76 | vm.runInNewContext(s, { 77 | console: { 78 | log: function (msg) { 79 | output += +msg 80 | }, 81 | }, 82 | }) 83 | return output 84 | } 85 | 86 | function write(file, n) { 87 | let base = path.basename(file, '.js') 88 | pool[base] = n 89 | let content = (base === 'c' ? '' : 'require("./c.js");') + 'console.log(' + n + ')' + '// ' + file 90 | fs.writeFileSync(file, content) 91 | } 92 | 93 | function readDest(file) { 94 | return fs.readFileSync(dest(file), 'utf8') 95 | } 96 | 97 | --------------------------------------------------------------------------------