{"id":15395016,"url":"https://github.com/goto-bus-stop/browser-pack-flat","last_synced_at":"2025-04-04T10:07:33.622Z","repository":{"id":40545820,"uuid":"93626965","full_name":"goto-bus-stop/browser-pack-flat","owner":"goto-bus-stop","description":"bundle browserify modules into a single scope, a la rollup","archived":false,"fork":false,"pushed_at":"2024-10-10T06:39:43.000Z","size":359,"stargazers_count":56,"open_issues_count":7,"forks_count":13,"subscribers_count":2,"default_branch":"default","last_synced_at":"2025-03-28T09:08:50.004Z","etag":null,"topics":["browserify","browserify-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goto-bus-stop.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","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":"2017-06-07T11:16:34.000Z","updated_at":"2025-02-11T15:49:25.000Z","dependencies_parsed_at":"2024-10-19T02:13:21.957Z","dependency_job_id":"6a43a0c9-6ec8-4a06-8144-7c4f30643a0c","html_url":"https://github.com/goto-bus-stop/browser-pack-flat","commit_stats":{"total_commits":193,"total_committers":6,"mean_commits":"32.166666666666664","dds":"0.051813471502590636","last_synced_commit":"df3782693dfc66c3bcd338a581beff10d7815cf2"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Fbrowser-pack-flat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Fbrowser-pack-flat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Fbrowser-pack-flat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Fbrowser-pack-flat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goto-bus-stop","download_url":"https://codeload.github.com/goto-bus-stop/browser-pack-flat/tar.gz/refs/heads/default","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247157281,"owners_count":20893220,"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":["browserify","browserify-plugin"],"created_at":"2024-10-01T15:25:21.082Z","updated_at":"2025-04-04T10:07:33.589Z","avatar_url":"https://github.com/goto-bus-stop.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# browser-pack-flat\n\nBundle browserify modules into a single scope, a la rollup.\n\nCaveats:\n\n - Modules are executed fully, one after another, instead of inline.\n   This is a potential difference from Node.js and the default browserify behaviour.\n   Usually this does not matter, but rarely the order that some things are executed in may change.\n - This rewrites `require()` calls to simple variable assignments.\n   If a module wraps `require()` somehow it probably will not work.\n   In practice this is quite rare.\n - Using `factor-bundle` to split output code into separate files will not work with this plugin.\n\n## Install\n\n```bash\nnpm install --save-dev browser-pack-flat\n```\n\n## Usage\n\n```bash\nbrowserify /path/to/app.js | browser-unpack | browser-pack-flat\n```\n\nOr as a plugin:\n\n```bash\nbrowserify /path/to/app.js -p browser-pack-flat\n```\n\nThe plugin replaces the `browser-pack` module used by default by browserify.\n\nWith the Node API:\n\n```js\nvar browserify = require('browserify')\nvar packFlat = require('browser-pack-flat')\n\nbrowserify({ entries: './src/app.js' })\n  .plugin(packFlat, { /* options */ })\n  .bundle()\n  .pipe(fs.createWriteStream('bundle.js'))\n```\n\n## What exactly?\n\nbrowserify uses [browser-pack](https://github.com/browserify/browser-pack) to output a bundle.\nbrowser-pack uses a small `require`-like runtime and wraps modules in functions to get a module loading behaviour that's almost identical to Node.js.\nHowever this resolution can take a few milliseconds across an entire bundle.\n\nInput:\n\n```js\nvar unique = require('uniq');\n\nvar data = [1, 2, 2, 3, 4, 5, 5, 5, 6];\n\nconsole.log(unique(data));\n```\n\nWith browser-pack, this bundle would output:\n\n```js\n(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"\u0026\u0026require;if(!u\u0026\u0026a)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\"\u0026\u0026require;for(var o=0;o\u003cr.length;o++)s(r[o]);return s})({1:[function(require,module,exports){\nvar unique = require('uniq');\n\nvar data = [1, 2, 2, 3, 4, 5, 5, 5, 6];\n\nconsole.log(unique(data));\n},{\"uniq\":2}],2:[function(require,module,exports){\n\"use strict\"\n\n/* -- snip -- */\n\nfunction unique(list, compare, sorted) {\n  if(list.length === 0) {\n    return list\n  }\n  if(compare) {\n    if(!sorted) {\n      list.sort(compare)\n    }\n    return unique_pred(list, compare)\n  }\n  if(!sorted) {\n    list.sort()\n  }\n  return unique_eq(list)\n}\n\nmodule.exports = unique\n\n},{}]},{},[1]);\n```\n\nbrowser-pack-flat instead rewrites `require()` calls and `module.exports` assignments to simple variables, and sorts the modules so that the module that would be executed first, is at the top of the bundle.\nIt doesn't need a runtime in most cases, and no function calls to execute modules.\n\n```js\n(function(){\n\"use strict\"\n\n/* -- snip -- */\n\nfunction unique(list, compare, sorted) {\n  if(list.length === 0) {\n    return list\n  }\n  if(compare) {\n    if(!sorted) {\n      list.sort(compare)\n    }\n    return unique_pred(list, compare)\n  }\n  if(!sorted) {\n    list.sort()\n  }\n  return unique_eq(list)\n}\n\nvar _$unique_2 = unique\n\nvar _$main_1 = {};\n/* removed: var _$unique_2 = require('uniq'); */;\n\nvar data = [1, 2, 2, 3, 4, 5, 5, 5, 6];\n\nconsole.log(_$unique_2(data));\n}());\n```\n\nInstead of `require('uniq')`, the main module simply refers to `_$unique_2`, which is the exports value of the `uniq` module.\nThe only function wrapper is the outermost one, which prevents variables from leaking into the `window` (global scope).\n\nSometimes it's not possible to sort modules in their execution order, because in the Node.js module system, a module can require another module that requires the first module: a circular dependency.\nbrowser-pack-flat addresses this with a small runtime, to lazily execute modules that are part of a circular dependency chain.\nThis works similarly to how the Node.js module system works, and to how the standard browser-pack works too.\nInstead of rewriting `require()`s to variables and `module.exports` to a variable assignment, in \"circular modules\" browser-pack-flat adds a function wrapper.\nWhen a circular module is `require()`d, browser-pack-flat will call the function wrapper, which executes the module and caches the exports.\n\nBelow, `a.js` depends on `b.js`, and `b.js` depends on `a.js`:\n\n```js\n// app.js\nconsole.log(\n  require('./a')()\n)\n// a.js\nvar b = require('./b')\nmodule.exports = function () {\n  return b()\n}\n// b.js\nmodule.exports = function () {\n  return require('./a').toString()\n}\n```\n\nWith browser-pack-flat, this becomes:\n\n```js\n(function(){\nvar createModuleFactory = function createModuleFactory(factory) {\n  var module; return function () { if (!module) { module = { exports: {} }; factory(module, module.exports) } return module.exports }\n};\nvar _$a_1 = createModuleFactory(function (module, exports) {\nvar b = _$b_3()\nmodule.exports = function () {\n  return b()\n}\n\n});\nvar _$b_3 = createModuleFactory(function (module, exports) {\nmodule.exports = function () {\n  return _$a_1().toString()\n}\n\n});\nvar _$app_2 = {};\nconsole.log(\n  _$a_1()()\n)\n\n}());\n```\n\nThe `createModuleFactory` helper returns the exports of the module it wraps, evaluating the module on the first call.\nInstead of replacing `require('./a')` with `_$a_1` like browser-pack-flat normally would, it replaced it with `_$a_1()`.\n\nbrowser-pack-flat does some more things like rewriting top-level variables in modules in case there is another variable with the same name in another module, but that's most of the magic!\n\n## Related\n\n * [common-shakeify](https://github.com/goto-bus-stop/common-shakeify) - Tree-shaking plugin for browserify based on [@indutny](https://github.com/indutny)'s [common-shake](https://github.com/indutny/common-shake) library\n * [tinyify](https://github.com/browserify/tinyify) - Optimization plugin for browserify, includes browser-pack-flat\n\n## License\n\n[MIT](./LICENSE)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoto-bus-stop%2Fbrowser-pack-flat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoto-bus-stop%2Fbrowser-pack-flat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoto-bus-stop%2Fbrowser-pack-flat/lists"}