{"id":13671175,"url":"https://github.com/browserify/factor-bundle","last_synced_at":"2025-12-17T21:30:07.256Z","repository":{"id":13536003,"uuid":"16227592","full_name":"browserify/factor-bundle","owner":"browserify","description":"factor browser-pack bundles into common shared bundles","archived":false,"fork":false,"pushed_at":"2024-12-21T10:15:37.000Z","size":84,"stargazers_count":400,"open_issues_count":28,"forks_count":27,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-01T10:07:10.289Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/browserify.png","metadata":{"files":{"readme":"readme.markdown","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-01-25T08:39:45.000Z","updated_at":"2024-10-14T02:34:12.000Z","dependencies_parsed_at":"2025-01-05T18:04:11.580Z","dependency_job_id":"bd83fd4c-dc69-4c7c-a89c-abeac94f5b21","html_url":"https://github.com/browserify/factor-bundle","commit_stats":{"total_commits":116,"total_committers":11,"mean_commits":"10.545454545454545","dds":0.3362068965517241,"last_synced_commit":"532b1e48397a8a2580fa87c6a867b866069ad1b5"},"previous_names":["substack/factor-bundle"],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Ffactor-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Ffactor-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Ffactor-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Ffactor-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/browserify","download_url":"https://codeload.github.com/browserify/factor-bundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247829511,"owners_count":21002997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:01.948Z","updated_at":"2025-12-17T21:30:07.181Z","avatar_url":"https://github.com/browserify.png","language":"JavaScript","readme":"# factor-bundle\n\nfactor [browser-pack](https://npmjs.org/package/browser-pack) bundles into a\ncommon bundle and entry-specific bundles\n\n[![build status](https://secure.travis-ci.org/substack/factor-bundle.png)](http://travis-ci.org/substack/factor-bundle)\n\n# example\n\nx.js:\n\n``` js\nvar z = require('./z.js');\nvar w = require('./w.js');\nconsole.log(z(5) * w(2));\n```\n\ny.js:\n\n``` js\nvar z = require('./z.js');\nconsole.log(z(2) + 111);\n```\n\nz.js:\n\n``` js\nmodule.exports = function (n) { return n * 111 }\n```\n\nw.js:\n\n``` js\nmodule.exports = function (n) { return n * 50 }\n```\n\nNow run factor-bundle as a plugin (new in browserify 3.28.0):\n\n``` sh\nbrowserify x.js y.js -p [ factor-bundle -o bundle/x.js -o bundle/y.js ] \\\n  -o bundle/common.js\n```\n\nor you can pipe [module-deps](https://npmjs.org/package/module-deps) json\ndirectly into the `factor-bundle` command:\n\n``` sh\n$ module-deps x.js y.js | factor-bundle \\\n  x.js -o bundle/x.js \\\n  y.js -o bundle/y.js \\\n  \u003e bundle/common.js\n```\n\nor factor out an existing bundle already compiled by browserify:\n\n``` sh\n$ browserify x.js y.js \u003e bundle.js\n$ browser-unpack \u003c bundle.js | factor-bundle \\\n  x.js -o bundle/x.js \\\n  y.js -o bundle/y.js \\\n  \u003e bundle/common.js\n```\n\nWhichever one of these 3 options, you take, you can now have 2 pages, each with\na different combination of script tags but with all the common modules factored\nout into a `common.js` to avoid transferring the same code multiple times:\n\n``` html\n\u003cscript src=\"/bundle/common.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"/bundle/x.js\"\u003e\u003c/script\u003e\n```\n\n``` html\n\u003cscript src=\"/bundle/common.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"/bundle/y.js\"\u003e\u003c/script\u003e\n```\n\nto verify this works from node you can do:\n\n```\n$ cat bundle/common.js bundle/x.js | node\n55500\n$ cat bundle/common.js bundle/y.js | node\n333\n```\n\n## command-line outpipe example\n\nWe can pipe each output file through some other processes. Here we'll do\nminification with uglify compression with gzip:\n\n``` sh\nbrowserify files/*.js \\\n    -p [ factor-bundle -o 'uglifyjs -cm | gzip \u003e bundle/`basename $FILE`.gz' ] \\\n    | uglifyjs -cm | gzip \u003e bundle/common.js.gz\n```\n\n## api plugin example\n\nIf you prefer you can use the factor-bundle plugin api directly in code:\n\n``` js\nvar browserify = require('browserify');\nvar fs = require('fs');\n\nvar files = [ './files/x.js', './files/y.js' ];\nvar b = browserify(files);\nb.plugin('factor-bundle', { outputs: [ 'bundle/x.js', 'bundle/y.js' ] });\nb.bundle().pipe(fs.createWriteStream('bundle/common.js'));\n```\n\nor instead of writing to files you can output to writable streams:\n\n``` js\nvar browserify = require('browserify');\nvar concat = require('concat-stream');\n\nvar files = [ './files/x.js', './files/y.js' ];\nvar b = browserify(files);\n\nb.plugin('factor-bundle', { outputs: [ write('x'), write('y') ] });\nb.bundle().pipe(write('common'));\n\nfunction write (name) {\n    return concat(function (body) {\n        console.log('// ----- ' + name + ' -----');\n        console.log(body.toString('utf8'));\n    });\n}\n```\n\n# usage\n\nYou can use factor-bundle as a browserify plugin:\n\n```\nbrowserify -p [ factor-bundle OPTIONS ]\n\nwhere OPTIONS are:\n\n  -o  Output FILE or CMD that maps to a corresponding entry file at the same\n      index. CMDs are executed with $FILE set to the corresponding input file.\n      Optionally specify a function that returns a valid value for this argument.\n \n  -e  Entry file to use, overriding the entry files listed in the original\n      bundle.\n\n```\n\nor you can use the command:\n\n```\nusage: factor-bundle [ x.js -o bundle/x.js ... ] \u003e bundle/common.js\n\nRead `module-deps` json output from stdin, factoring each entry file out into\nthe corresponding output file (-o).\n\n  -o FILE, -o CMD\n\n    Write output to FILE or CMD. CMD is distinguished from FILE by the presence\n    of one or more `\u003e` or `|` characters. For example, use:\n    \n      factor-bundle browser/a.js browser/b.js \\\n        -o bundle/a.js -o bundle/b.js\n    \n    to write to a FILE. And do something like:\n\n      factor-bundle browser/*.js \\\n        -o 'uglifyjs -cm | gzip \u003e bundle/`basename $FILE`.gz'\n\n    to use a CMD. In the CMD, $FILE is set to the corresponding input file path.\n\n    If there is a trailing unpaired `-o`, that file will be used for the common\n    bundle output. Otherwise, the final bundle is written to stdout.\n\n```\n\n# methods\n\n``` js\nvar factor = require('factor-bundle')\n```\n\n## var fr = factor(files, opts={})\n\nReturn a transform stream `tr` that factors the array of entry path strings\n`files` out into bundle files. The input format that `fr` expects is described\nin the [module-deps package](https://npmjs.org/package/module-deps).\n\nThe output format for `fr` and each of the `fr` sub-streams given by each\n`'stream'` event is also in the\n[module-deps](https://npmjs.org/package/module-deps) format.\n\n`opts.o` or `opts.outputs` should be an array that pairs up with the `files` array to specify\nwhere each bundle output for each entry file should be written. The elements in\n`opts.o` can be string filenames, writable streams, or functions that return a\nstring filename or writable stream.\n\n`opts.entries` or `opts.e` should be the array of entry files to create\na page-specific bundle for each file. If you don't pass in an `opts.entries`,\nthis information is gathered from browserify itself.\n\nThe files held in common among `\u003e opts.threshold` (default: 1) bundles will be\noutput on the `fr` stream itself. The entry-specific bundles are diverted into\neach `'stream'` event's output. `opts.threshold` can be a number or a function\n`opts.threshold(row, groups)` where `row` is a \n[module-deps](https://github.com/substack/module-deps) object and `groups` is \nan array of bundles which depend on the row. If the threshold function returns \n`true`, that row and all its dependencies will go to the `common` bundle. If \nfalse, the row (but not its dependencies) will go to each bundle in `groups`.\nFor example:\n\n``` js\nfactor(files, {threshold: function(row, groups) {\n    if (/.*a\\.js$/.test(row.id)) return false;\n    if (/.*[z]\\.js$/.test(row.id)) return true;\n    return this._defaultThreshold(row, groups);\n}});\n```\n\n# events\n\n## fr.on('stream', function (stream) {})\n\nEach entry file emits a `'stream'` event containing all of the entry-specific\nrows that are only used by that entry file (when `opts.threshold === 1`, the\ndefault).\n\nThe entry file name is available as `stream.file`.\n\n## b.on('factor.pipeline', function (file, pipeline) {})\n\nEmits the full path to the entry file (`file`) and a [labeled-stream-splicer](https://npmjs.org/package/labeled-stream-splicer) (`pipeline`) for each entry file with these labels:\n\n* `'pack'` - [browser-pack](https://npmjs.org/package/browser-pack)\n* `'wrap'` - apply final wrapping\n\nYou can call `pipeline.get` with a label name to get a handle on a stream pipeline that you can `push()`, `unshift()`, or `splice()` to insert your own transform streams.\n\nEvent handlers must be attached *before* calling `b.plugin`.\n\n# install\n\nWith [npm](https://npmjs.org) do:\n\n```\nnpm install factor-bundle\n```\n\n# license\n\nMIT\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserify%2Ffactor-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrowserify%2Ffactor-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserify%2Ffactor-bundle/lists"}