{"id":16186178,"url":"https://github.com/zenflow/bpb","last_synced_at":"2025-07-05T18:03:24.595Z","repository":{"id":71685006,"uuid":"36581118","full_name":"zenflow/bpb","owner":"zenflow","description":"browserify transform to safely replace `process.browser` with `true` by working on the Abstract Syntax Tree (AST)","archived":false,"fork":false,"pushed_at":"2015-09-26T14:41:13.000Z","size":222,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-18T17:01:43.289Z","etag":null,"topics":[],"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/zenflow.png","metadata":{"files":{"readme":"README.md","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}},"created_at":"2015-05-31T00:20:25.000Z","updated_at":"2022-04-08T03:34:54.000Z","dependencies_parsed_at":"2023-02-23T13:30:39.293Z","dependency_job_id":null,"html_url":"https://github.com/zenflow/bpb","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenflow%2Fbpb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenflow%2Fbpb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenflow%2Fbpb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenflow%2Fbpb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zenflow","download_url":"https://codeload.github.com/zenflow/bpb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243961568,"owners_count":20375271,"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-10-10T07:17:22.456Z","updated_at":"2025-03-19T03:30:27.172Z","avatar_url":"https://github.com/zenflow.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bpb\nbrowserify transform to safely replace `process.browser` with `true` by working on the Abstract Syntax Tree (AST) \n\n[![build status](https://travis-ci.org/zenflow/bpb.svg?branch=master)](https://travis-ci.org/zenflow/bpb?branch=master)\n[![dependencies](https://david-dm.org/zenflow/bpb.svg)](https://david-dm.org/zenflow/bpb)\n[![dev-dependencies](https://david-dm.org/zenflow/bpb/dev-status.svg)](https://david-dm.org/zenflow/bpb#info=devDependencies)\n\n[![npm](https://nodei.co/npm/bpb.svg?downloads=true\u0026downloadRank=true\u0026stars=true)](https://www.npmjs.com/package/bpb)\n\n## introduction\n\n[The browserify/webpack implementation of `process`](https://github.com/defunctzombie/node-process) \nhas a special `browser` member set to `true`, which is not present in \n[the nodejs/iojs implementation of `process`](https://nodejs.org/api/process.html).\n\n```js\n\t// on node\n\tprocess.browser === undefined\n\t// -\u003e true\n\t\n\t// on browser\n\tprocess.browser === true\n\t// -\u003e true\n```\n\nThis can be referenced in isomorphic code that\n* adjusts it's behavior depending on which environment it's in\n* is fully portable within the nodejs/iojs/browserify/webpack ecosystem, and\n* is concise and [Don't Repeat Yourself (DRY)](http://programmer.97things.oreilly.com/wiki/index.php/Don't_Repeat_Yourself)\n\n```js\n  // example #1\n  function animateElement(element) {\n    if (!process.browser){\n      throw new Error('animateElement function is for use client-side only!'); \n    }\n    $(element).animate({/*...*/});\n  }\n  \n  // example #2\n  function getCookie(name) {\n    return process.browser ? getCookieFromWindow(name) : getCookieFromRequest(req, name);\n  }\n```\n\n__This is where bpb (short for \"browserify-processisfy.browserify\") comes in.__\n\nFor optimized build-sizes and or security, use bpb in combination with [unreachable-branch-transform](https://github.com/zertosh/unreachable-branch-transform) (or a good minifier like [UglifyJS](https://github.com/mishoo/UglifyJS)) to strip out server-side-only code.\n\n```js\n  // example #1 after bpb\n  function animateElement(element) {\n    if (!true){\n      throw new Error('animateElement function is for use client-side only!');\n    }\n    $(element).animate({/*...*/});\n  }\n\n  // example #1 after bpb + unreachable-branch-transform\n  function animateElement(element) {\n    $(element).animate({/*...*/});\n  }\n  \n  // example #2 after bpb\n  function getCookie(name) {\n    return true ? getCookieFromWindow(name) : getCookieFromRequest(req, name);\n  }\n  \n  // example #2 after bpb + unreachable-branch-transform\n  function getCookie(name) {\n    return getCookieFromWindow(name);\n  }\n```\n## usage \n\n*bpb can be used as a [browserify transform](https://github.com/substack/browserify-handbook#transforms), a\n[transform stream](https://nodejs.org/api/stream.html), or a synchronous function.*\n\n### options\n\n* ecmaVersion: Must be either 3, 5, or 6. Default is 5.\n\nAll options are passed directly to [falafel](https://github.com/substack/node-falafel) which passes them directly to \n[acorn](https://github.com/marijnh/acorn).\n\n### examples\n\n```js\n  // as a browserify transform\n  var browserify = require('browserify');\n  var fs = require('fs');\n  browserify('input.js')\n  \t.transform('bpb', {/* options */})\n  \t.transform('unreachable-branch-transform')\n  \t.bundle()\n  \t.pipe(fs.createWriteStream('output.js'));\n\n  // as a transform stream\n  var fs = require('fs');\n  var bpb = require('bpb');\n  var unreachable = require('unreachable-branch-transform');\n  fs.createReadStream('input.js')\n    .pipe(bpb({/* options */}))\n    .pipe(unreachable())\n    .pipe(fs.createWriteStream('output.js'));\n    \n  // as a synchronous function\n  var bpb = require('bpb');\n  var unreachable = require('unreachable-branch-transform');\n  unreachable.transform(bpb.sync('foo(process.browser ? 1 : 2);', {/* options */}))\n  // -\u003e 'foo(1)'\n```\n\n## changelog\n\n### 0.2.2\n\n* emit errors rather than throw them\n\n### 0.2.1\n\n* return passthrough for json files\n\n### 0.2.0\n\n* added es6 support\n\n### 0.1.1\n\n* added changelog to readme\n* include readme in package files\n\n### 0.1.0\n\n* safe implementation\n* advanced tests\n\n### 0.0.1\n\n* tests\n* initial dumb implementation\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzenflow%2Fbpb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzenflow%2Fbpb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzenflow%2Fbpb/lists"}