├── .gitignore ├── .npmignore ├── .npmrc ├── .travis.yml ├── LICENSE ├── bin └── cmd.js ├── example ├── compiler-pipeline │ ├── bundle.js │ ├── package.json │ └── src │ │ ├── bar.js │ │ ├── foo.js │ │ └── main.js └── hmr │ ├── main.js │ ├── msg.js │ ├── package.json │ └── public │ └── index.html ├── package.json └── readme.markdown /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | hmr/node_modules 2 | hmr/public/bundle.js 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "stable" 4 | - "lts/*" 5 | - "4" 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Attribution 3.0 Unported 2 | 3 | License 4 | 5 | THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS 6 | PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR 7 | OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS 8 | LICENSE OR COPYRIGHT LAW IS PROHIBITED. 9 | 10 | BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE 11 | BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE CONSIDERED 12 | TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN 13 | CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. 14 | 15 | 1. Definitions 16 | 17 | "Adaptation" means a work based upon the Work, or upon the Work and other 18 | pre-existing works, such as a translation, adaptation, derivative work, 19 | arrangement of music or other alterations of a literary or artistic work, or 20 | phonogram or performance and includes cinematographic adaptations or any other 21 | form in which the Work may be recast, transformed, or adapted including in any 22 | form recognizably derived from the original, except that a work that constitutes 23 | a Collection will not be considered an Adaptation for the purpose of this 24 | License. For the avoidance of doubt, where the Work is a musical work, 25 | performance or phonogram, the synchronization of the Work in timed-relation with 26 | a moving image ("synching") will be considered an Adaptation for the purpose of 27 | this License. "Collection" means a collection of literary or artistic works, 28 | such as encyclopedias and anthologies, or performances, phonograms or 29 | broadcasts, or other works or subject matter other than works listed in Section 30 | 1(f) below, which, by reason of the selection and arrangement of their contents, 31 | constitute intellectual creations, in which the Work is included in its entirety 32 | in unmodified form along with one or more other contributions, each constituting 33 | separate and independent works in themselves, which together are assembled into 34 | a collective whole. A work that constitutes a Collection will not be considered 35 | an Adaptation (as defined above) for the purposes of this License. "Distribute" 36 | means to make available to the public the original and copies of the Work or 37 | Adaptation, as appropriate, through sale or other transfer of ownership. 38 | "Licensor" means the individual, individuals, entity or entities that offer(s) 39 | the Work under the terms of this License. "Original Author" means, in the case 40 | of a literary or artistic work, the individual, individuals, entity or entities 41 | who created the Work or if no individual or entity can be identified, the 42 | publisher; and in addition (i) in the case of a performance the actors, singers, 43 | musicians, dancers, and other persons who act, sing, deliver, declaim, play in, 44 | interpret or otherwise perform literary or artistic works or expressions of 45 | folklore; (ii) in the case of a phonogram the producer being the person or legal 46 | entity who first fixes the sounds of a performance or other sounds; and, (iii) 47 | in the case of broadcasts, the organization that transmits the broadcast. "Work" 48 | means the literary and/or artistic work offered under the terms of this License 49 | including without limitation any production in the literary, scientific and 50 | artistic domain, whatever may be the mode or form of its expression including 51 | digital form, such as a book, pamphlet and other writing; a lecture, address, 52 | sermon or other work of the same nature; a dramatic or dramatico-musical work; a 53 | choreographic work or entertainment in dumb show; a musical composition with or 54 | without words; a cinematographic work to which are assimilated works expressed 55 | by a process analogous to cinematography; a work of drawing, painting, 56 | architecture, sculpture, engraving or lithography; a photographic work to which 57 | are assimilated works expressed by a process analogous to photography; a work of 58 | applied art; an illustration, map, plan, sketch or three-dimensional work 59 | relative to geography, topography, architecture or science; a performance; a 60 | broadcast; a phonogram; a compilation of data to the extent it is protected as a 61 | copyrightable work; or a work performed by a variety or circus performer to the 62 | extent it is not otherwise considered a literary or artistic work. "You" means 63 | an individual or entity exercising rights under this License who has not 64 | previously violated the terms of this License with respect to the Work, or who 65 | has received express permission from the Licensor to exercise rights under this 66 | License despite a previous violation. "Publicly Perform" means to perform public 67 | recitations of the Work and to communicate to the public those public 68 | recitations, by any means or process, including by wire or wireless means or 69 | public digital performances; to make available to the public Works in such a way 70 | that members of the public may access these Works from a place and at a place 71 | individually chosen by them; to perform the Work to the public by any means or 72 | process and the communication to the public of the performances of the Work, 73 | including by public digital performance; to broadcast and rebroadcast the Work 74 | by any means including signs, sounds or images. "Reproduce" means to make copies 75 | of the Work by any means including without limitation by sound or visual 76 | recordings and the right of fixation and reproducing fixations of the Work, 77 | including storage of a protected performance or phonogram in digital form or 78 | other electronic medium. 2. Fair Dealing Rights. Nothing in this License is 79 | intended to reduce, limit, or restrict any uses free from copyright or rights 80 | arising from limitations or exceptions that are provided for in connection with 81 | the copyright protection under copyright law or other applicable laws. 82 | 83 | 3. License Grant. Subject to the terms and conditions of this License, Licensor 84 | hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the 85 | duration of the applicable copyright) license to exercise the rights in the Work 86 | as stated below: 87 | 88 | to Reproduce the Work, to incorporate the Work into one or more Collections, and 89 | to Reproduce the Work as incorporated in the Collections; to create and 90 | Reproduce Adaptations provided that any such Adaptation, including any 91 | translation in any medium, takes reasonable steps to clearly label, demarcate or 92 | otherwise identify that changes were made to the original Work. For example, a 93 | translation could be marked "The original work was translated from English to 94 | Spanish," or a modification could indicate "The original work has been 95 | modified."; to Distribute and Publicly Perform the Work including as 96 | incorporated in Collections; and, to Distribute and Publicly Perform 97 | Adaptations. For the avoidance of doubt: 98 | 99 | Non-waivable Compulsory License Schemes. In those jurisdictions in which the 100 | right to collect royalties through any statutory or compulsory licensing scheme 101 | cannot be waived, the Licensor reserves the exclusive right to collect such 102 | royalties for any exercise by You of the rights granted under this License; 103 | Waivable Compulsory License Schemes. In those jurisdictions in which the right 104 | to collect royalties through any statutory or compulsory licensing scheme can be 105 | waived, the Licensor waives the exclusive right to collect such royalties for 106 | any exercise by You of the rights granted under this License; and, Voluntary 107 | License Schemes. The Licensor waives the right to collect royalties, whether 108 | individually or, in the event that the Licensor is a member of a collecting 109 | society that administers voluntary licensing schemes, via that society, from any 110 | exercise by You of the rights granted under this License. The above rights may 111 | be exercised in all media and formats whether now known or hereafter devised. 112 | The above rights include the right to make such modifications as are technically 113 | necessary to exercise the rights in other media and formats. Subject to Section 114 | 8(f), all rights not expressly granted by Licensor are hereby reserved. 115 | 116 | 4. Restrictions. The license granted in Section 3 above is expressly made 117 | subject to and limited by the following restrictions: 118 | 119 | You may Distribute or Publicly Perform the Work only under the terms of this 120 | License. You must include a copy of, or the Uniform Resource Identifier (URI) 121 | for, this License with every copy of the Work You Distribute or Publicly 122 | Perform. You may not offer or impose any terms on the Work that restrict the 123 | terms of this License or the ability of the recipient of the Work to exercise 124 | the rights granted to that recipient under the terms of the License. You may not 125 | sublicense the Work. You must keep intact all notices that refer to this License 126 | and to the disclaimer of warranties with every copy of the Work You Distribute 127 | or Publicly Perform. When You Distribute or Publicly Perform the Work, You may 128 | not impose any effective technological measures on the Work that restrict the 129 | ability of a recipient of the Work from You to exercise the rights granted to 130 | that recipient under the terms of the License. This Section 4(a) applies to the 131 | Work as incorporated in a Collection, but this does not require the Collection 132 | apart from the Work itself to be made subject to the terms of this License. If 133 | You create a Collection, upon notice from any Licensor You must, to the extent 134 | practicable, remove from the Collection any credit as required by Section 4(b), 135 | as requested. If You create an Adaptation, upon notice from any Licensor You 136 | must, to the extent practicable, remove from the Adaptation any credit as 137 | required by Section 4(b), as requested. If You Distribute, or Publicly Perform 138 | the Work or any Adaptations or Collections, You must, unless a request has been 139 | made pursuant to Section 4(a), keep intact all copyright notices for the Work 140 | and provide, reasonable to the medium or means You are utilizing: (i) the name 141 | of the Original Author (or pseudonym, if applicable) if supplied, and/or if the 142 | Original Author and/or Licensor designate another party or parties (e.g., a 143 | sponsor institute, publishing entity, journal) for attribution ("Attribution 144 | Parties") in Licensor's copyright notice, terms of service or by other 145 | reasonable means, the name of such party or parties; (ii) the title of the Work 146 | if supplied; (iii) to the extent reasonably practicable, the URI, if any, that 147 | Licensor specifies to be associated with the Work, unless such URI does not 148 | refer to the copyright notice or licensing information for the Work; and (iv) , 149 | consistent with Section 3(b), in the case of an Adaptation, a credit identifying 150 | the use of the Work in the Adaptation (e.g., "French translation of the Work by 151 | Original Author," or "Screenplay based on original Work by Original Author"). 152 | The credit required by this Section 4 (b) may be implemented in any reasonable 153 | manner; provided, however, that in the case of a Adaptation or Collection, at a 154 | minimum such credit will appear, if a credit for all contributing authors of the 155 | Adaptation or Collection appears, then as part of these credits and in a manner 156 | at least as prominent as the credits for the other contributing authors. For the 157 | avoidance of doubt, You may only use the credit required by this Section for the 158 | purpose of attribution in the manner set out above and, by exercising Your 159 | rights under this License, You may not implicitly or explicitly assert or imply 160 | any connection with, sponsorship or endorsement by the Original Author, Licensor 161 | and/or Attribution Parties, as appropriate, of You or Your use of the Work, 162 | without the separate, express prior written permission of the Original Author, 163 | Licensor and/or Attribution Parties. Except as otherwise agreed in writing by 164 | the Licensor or as may be otherwise permitted by applicable law, if You 165 | Reproduce, Distribute or Publicly Perform the Work either by itself or as part 166 | of any Adaptations or Collections, You must not distort, mutilate, modify or 167 | take other derogatory action in relation to the Work which would be prejudicial 168 | to the Original Author's honor or reputation. Licensor agrees that in those 169 | jurisdictions (e.g. Japan), in which any exercise of the right granted in 170 | Section 3(b) of this License (the right to make Adaptations) would be deemed to 171 | be a distortion, mutilation, modification or other derogatory action prejudicial 172 | to the Original Author's honor and reputation, the Licensor will waive or not 173 | assert, as appropriate, this Section, to the fullest extent permitted by the 174 | applicable national law, to enable You to reasonably exercise Your right under 175 | Section 3(b) of this License (right to make Adaptations) but not otherwise. 5. 176 | Representations, Warranties and Disclaimer 177 | 178 | UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS 179 | THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING 180 | THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT 181 | LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR 182 | PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, 183 | OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME 184 | JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH 185 | EXCLUSION MAY NOT APPLY TO YOU. 186 | 187 | 6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN 188 | NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, 189 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS 190 | LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE 191 | POSSIBILITY OF SUCH DAMAGES. 192 | 193 | 7. Termination 194 | 195 | This License and the rights granted hereunder will terminate automatically upon 196 | any breach by You of the terms of this License. Individuals or entities who have 197 | received Adaptations or Collections from You under this License, however, will 198 | not have their licenses terminated provided such individuals or entities remain 199 | in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will 200 | survive any termination of this License. Subject to the above terms and 201 | conditions, the license granted here is perpetual (for the duration of the 202 | applicable copyright in the Work). Notwithstanding the above, Licensor reserves 203 | the right to release the Work under different license terms or to stop 204 | distributing the Work at any time; provided, however that any such election will 205 | not serve to withdraw this License (or any other license that has been, or is 206 | required to be, granted under the terms of this License), and this License will 207 | continue in full force and effect unless terminated as stated above. 8. 208 | Miscellaneous 209 | 210 | Each time You Distribute or Publicly Perform the Work or a Collection, the 211 | Licensor offers to the recipient a license to the Work on the same terms and 212 | conditions as the license granted to You under this License. Each time You 213 | Distribute or Publicly Perform an Adaptation, Licensor offers to the recipient a 214 | license to the original Work on the same terms and conditions as the license 215 | granted to You under this License. If any provision of this License is invalid 216 | or unenforceable under applicable law, it shall not affect the validity or 217 | enforceability of the remainder of the terms of this License, and without 218 | further action by the parties to this agreement, such provision shall be 219 | reformed to the minimum extent necessary to make such provision valid and 220 | enforceable. No term or provision of this License shall be deemed waived and no 221 | breach consented to unless such waiver or consent shall be in writing and signed 222 | by the party to be charged with such waiver or consent. This License constitutes 223 | the entire agreement between the parties with respect to the Work licensed here. 224 | There are no understandings, agreements or representations with respect to the 225 | Work not specified here. Licensor shall not be bound by any additional 226 | provisions that may appear in any communication from You. This License may not 227 | be modified without the mutual written agreement of the Licensor and You. The 228 | rights granted under, and the subject matter referenced, in this License were 229 | drafted utilizing the terminology of the Berne Convention for the Protection of 230 | Literary and Artistic Works (as amended on September 28, 1979), the Rome 231 | Convention of 1961, the WIPO Copyright Treaty of 1996, the WIPO Performances and 232 | Phonograms Treaty of 1996 and the Universal Copyright Convention (as revised on 233 | July 24, 1971). These rights and subject matter take effect in the relevant 234 | jurisdiction in which the License terms are sought to be enforced according to 235 | the corresponding provisions of the implementation of those treaty provisions in 236 | the applicable national law. If the standard suite of rights granted under 237 | applicable copyright law includes additional rights not granted under this 238 | License, such additional rights are deemed to be included in the License; this 239 | License is not intended to restrict the license of any rights under applicable 240 | law. Creative Commons Notice 241 | 242 | Creative Commons is not a party to this License, and makes no warranty 243 | whatsoever in connection with the Work. Creative Commons will not be liable to 244 | You or any party on any legal theory for any damages whatsoever, including 245 | without limitation any general, special, incidental or consequential damages 246 | arising in connection to this license. Notwithstanding the foregoing two (2) 247 | sentences, if Creative Commons has expressly identified itself as the Licensor 248 | hereunder, it shall have all rights and obligations of Licensor. 249 | 250 | Except for the limited purpose of indicating to the public that the Work is 251 | licensed under the CCPL, Creative Commons does not authorize the use by either 252 | party of the trademark "Creative Commons" or any related trademark or logo of 253 | Creative Commons without the prior written consent of Creative Commons. Any 254 | permitted use will be in compliance with Creative Commons' then-current 255 | trademark usage guidelines, as may be published on its website or otherwise made 256 | available upon request from time to time. For the avoidance of doubt, this 257 | trademark restriction does not form part of this License. 258 | 259 | Creative Commons may be contacted at http://creativecommons.org/. -------------------------------------------------------------------------------- /bin/cmd.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs'); 4 | var pager = require('default-pager'); 5 | var through = require('through2'); 6 | var duplexer = require('duplexer2'); 7 | var concat = require('concat-stream'); 8 | var marked = require('marked'); 9 | var MarkedTerminal = require('marked-terminal'); 10 | 11 | marked.setOptions({ 12 | renderer: new MarkedTerminal() 13 | }); 14 | 15 | var moutput = through(); 16 | var minput = concat({ encoding: 'string' }, function (res) { 17 | moutput.end(marked(res)); 18 | }); 19 | var markdown = duplexer(minput, moutput); 20 | 21 | fs.createReadStream(__dirname + '/../readme.markdown') 22 | .pipe(markdown) 23 | .pipe(pager()); 24 | -------------------------------------------------------------------------------- /example/compiler-pipeline/bundle.js: -------------------------------------------------------------------------------- 1 | var browserify = require('browserify'); 2 | var through = require('through2'); 3 | var shasum = require('shasum'); 4 | 5 | var b = browserify(__dirname + '/src/main.js'); 6 | 7 | var hashes = {}; 8 | var hasher = through.obj(function (row, enc, next) { 9 | hashes[row.id] = shasum(row.source); 10 | this.push(row); 11 | next(); 12 | }); 13 | b.pipeline.get('deps').push(hasher); 14 | 15 | var labeler = through.obj(function (row, enc, next) { 16 | row.id = hashes[row.id]; 17 | 18 | Object.keys(row.deps).forEach(function (key) { 19 | row.deps[key] = hashes[row.deps[key]]; 20 | }); 21 | 22 | this.push(row); 23 | next(); 24 | }); 25 | b.pipeline.get('label').splice(0, 1, labeler); 26 | 27 | b.bundle().pipe(process.stdout); 28 | -------------------------------------------------------------------------------- /example/compiler-pipeline/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "browserify": "^16.2.3", 4 | "shasum": "^1.0.0", 5 | "through2": "^3.0.1" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /example/compiler-pipeline/src/bar.js: -------------------------------------------------------------------------------- 1 | module.exports = function (n) { return n * 100 }; 2 | -------------------------------------------------------------------------------- /example/compiler-pipeline/src/foo.js: -------------------------------------------------------------------------------- 1 | module.exports = function (n) { return n + 1 }; 2 | -------------------------------------------------------------------------------- /example/compiler-pipeline/src/main.js: -------------------------------------------------------------------------------- 1 | var foo = require('./foo.js'); 2 | var bar = require('./bar.js'); 3 | 4 | console.log(foo(3) + bar(4)); 5 | -------------------------------------------------------------------------------- /example/hmr/main.js: -------------------------------------------------------------------------------- 1 | document.body.textContent = require('./msg.js') 2 | 3 | if (module.hot) module.hot.accept() 4 | -------------------------------------------------------------------------------- /example/hmr/msg.js: -------------------------------------------------------------------------------- 1 | module.exports = 'cool' 2 | -------------------------------------------------------------------------------- /example/hmr/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-hmr-example", 3 | "dependencies": { 4 | "browserify-hmr": "^0.4.1", 5 | "http-server": "^0.11.1", 6 | "watchify": "^3.9.0" 7 | }, 8 | "scripts": { 9 | "watch": "watchify -p browserify-hmr main.js -o public/bundle.js -dv", 10 | "build": "browserify main.js > public/bundle.js", 11 | "www": "http-server -p 8000 public", 12 | "start": "npm run www & npm run watch" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /example/hmr/public/index.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-handbook", 3 | "description": "how to build modular applications with browserify", 4 | "version": "1.9.0", 5 | "author": { 6 | "name": "James Halliday", 7 | "email": "mail@substack.net", 8 | "url": "http://substack.net" 9 | }, 10 | "bin": { 11 | "browserify-handbook": "bin/cmd.js" 12 | }, 13 | "dependencies": { 14 | "concat-stream": "^1.6.2", 15 | "default-pager": "^1.0.1", 16 | "duplexer2": "^0.1.4", 17 | "marked": "^0.6.2", 18 | "marked-terminal": "^3.2.0", 19 | "through2": "^2.0.0" 20 | }, 21 | "engines": { 22 | "node": ">= 4" 23 | }, 24 | "homepage": "https://github.com/browserify/browserify-handbook", 25 | "bugs": "https://github.com/browserify/browserify-handbook/issues", 26 | "keywords": [ 27 | "browserify", 28 | "documentation", 29 | "guide", 30 | "handbook" 31 | ], 32 | "license": "CC-BY-3.0", 33 | "repository": { 34 | "type": "git", 35 | "url": "git://github.com/browserify/browserify-handbook.git" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /readme.markdown: -------------------------------------------------------------------------------- 1 | # introduction 2 | 3 | This document covers how to use [browserify](http://browserify.org) to build 4 | modular applications. 5 | 6 | [](http://creativecommons.org/licenses/by/3.0/) 7 | 8 | browserify is a tool for compiling 9 | [node-flavored](http://nodejs.org/docs/latest/api/modules.html) commonjs modules 10 | for the browser. 11 | 12 | You can use browserify to organize your code and use third-party libraries even 13 | if you don't use [node](http://nodejs.org) itself in any other capacity except 14 | for bundling and installing packages with npm. 15 | 16 | The module system that browserify uses is the same as node, so 17 | packages published to [npm](https://npmjs.org) that were originally intended for 18 | use in node but not browsers will work just fine in the browser too. 19 | 20 | Increasingly, people are publishing modules to npm which are intentionally 21 | designed to work in both node and in the browser using browserify and many 22 | packages on npm are intended for use in just the browser. 23 | [npm is for all javascript](http://maxogden.com/node-packaged-modules.html), 24 | front or backend alike. 25 | 26 | # table of contents 27 | 28 | - [introduction](#introduction) 29 | - [table of contents](#table-of-contents) 30 | - [node packaged manuscript](#node-packaged-manuscript) 31 | - [node packaged modules](#node-packaged-modules) 32 | - [require](#require) 33 | - [exports](#exports) 34 | - [bundling for the browser](#bundling-for-the-browser) 35 | - [how browserify works](#how-browserify-works) 36 | - [how node_modules works](#how-node_modules-works) 37 | - [why concatenate](#why-concatenate) 38 | - [development](#development) 39 | - [source maps](#source-maps) 40 | - [exorcist](#exorcist) 41 | - [auto-recompile](#auto-recompile) 42 | - [watchify](#watchify) 43 | - [beefy](#beefy) 44 | - [wzrd](#wzrd) 45 | - [browserify-middleware, enchilada](#browserify-middleware-enchilada) 46 | - [livereactload](#livereactload) 47 | - [browserify-hmr](#browserify-hmr) 48 | - [budo](#budo) 49 | - [using the api directly](#using-the-api-directly) 50 | - [grunt](#grunt) 51 | - [gulp](#gulp) 52 | - [builtins](#builtins) 53 | - [Buffer](#Buffer) 54 | - [process](#process) 55 | - [global](#global) 56 | - [__filename](#__filename) 57 | - [__dirname](#__dirname) 58 | - [transforms](#transforms) 59 | - [writing your own](#writing-your-own) 60 | - [package.json](#packagejson) 61 | - [browser field](#browser-field) 62 | - [browserify.transform field](#browserifytransform-field) 63 | - [finding good modules](#finding-good-modules) 64 | - [module philosophy](#module-philosophy) 65 | - [organizing modules](#organizing-modules) 66 | - [avoiding ../../../../../../..](#avoiding-) 67 | - [non-javascript assets](#non-javascript-assets) 68 | - [reusable components](#reusable-components) 69 | - [testing in node and the browser](#testing-in-node-and-the-browser) 70 | - [testing libraries](#testing-libraries) 71 | - [code coverage](#code-coverage) 72 | - [testling-ci](#testling-ci) 73 | - [bundling](#bundling) 74 | - [saving bytes](#saving-bytes) 75 | - [tinyify](#tinyify) 76 | - [standalone](#standalone) 77 | - [external bundles](#external-bundles) 78 | - [ignoring and excluding](#ignoring-and-excluding) 79 | - [browserify cdn](#browserify-cdn) 80 | - [shimming](#shimming) 81 | - [browserify-shim](#browserify-shim) 82 | - [partitioning](#partitioning) 83 | - [factor-bundle](#factor-bundle) 84 | - [partition-bundle](#partition-bundle) 85 | - [compiler pipeline](#compiler-pipeline) 86 | - [build your own browserify](#build-your-own-browserify) 87 | - [labeled phases](#labeled-phases) 88 | - [deps](#deps) 89 | - [json](#json) 90 | - [unbom](#unbom) 91 | - [syntax](#syntax) 92 | - [sort](#sort) 93 | - [dedupe](#dedupe) 94 | - [label](#label) 95 | - [emit-deps](#emit-deps) 96 | - [debug](#debug) 97 | - [pack](#pack) 98 | - [wrap](#wrap) 99 | - [browser-unpack](#browser-unpack) 100 | - [plugins](#plugins) 101 | - [using plugins](#using-plugins) 102 | - [authoring plugins](#authoring-plugins) 103 | 104 | # node packaged manuscript 105 | 106 | You can install this handbook with npm, appropriately enough. Just do: 107 | 108 | ``` 109 | npm install -g browserify-handbook 110 | ``` 111 | 112 | Now you will have a `browserify-handbook` command that will open this readme 113 | file in your `$PAGER`. Otherwise, you may continue reading this document as you 114 | are presently doing. 115 | 116 | # node packaged modules 117 | 118 | Before we can dive too deeply into how to use browserify and how it works, it is 119 | important to first understand how the 120 | [node-flavored version](http://nodejs.org/docs/latest/api/modules.html) 121 | of the commonjs module system works. 122 | 123 | ## require 124 | 125 | In node, there is a `require()` function for loading code from other files. 126 | 127 | If you install a module with [npm](https://npmjs.org): 128 | 129 | ``` 130 | npm install uniq 131 | ``` 132 | 133 | Then in a file `nums.js` we can `require('uniq')`: 134 | 135 | ``` 136 | var uniq = require('uniq'); 137 | var nums = [ 5, 2, 1, 3, 2, 5, 4, 2, 0, 1 ]; 138 | console.log(uniq(nums)); 139 | ``` 140 | 141 | The output of this program when run with node is: 142 | 143 | ``` 144 | $ node nums.js 145 | [ 0, 1, 2, 3, 4, 5 ] 146 | ``` 147 | 148 | You can require relative files by requiring a string that starts with a `.`. For 149 | example, to load a file `foo.js` from `main.js`, in `main.js` you can do: 150 | 151 | ``` js 152 | var foo = require('./foo.js'); 153 | console.log(foo(4)); 154 | ``` 155 | 156 | If `foo.js` was in the parent directory, you could use `../foo.js` instead: 157 | 158 | ``` js 159 | var foo = require('../foo.js'); 160 | console.log(foo(4)); 161 | ``` 162 | 163 | or likewise for any other kind of relative path. Relative paths are always 164 | resolved with respect to the invoking file's location. 165 | 166 | Note that `require()` returned a function and we assigned that return value to a 167 | variable called `uniq`. We could have picked any other name and it would have 168 | worked the same. `require()` returns the exports of the module name that you 169 | specify. 170 | 171 | How `require()` works is unlike many other module systems where imports are akin 172 | to statements that expose themselves as globals or file-local lexicals with 173 | names declared in the module itself outside of your control. Under the node 174 | style of code import with `require()`, someone reading your program can easily 175 | tell where each piece of functionality came from. This approach scales much 176 | better as the number of modules in an application grows. 177 | 178 | ## exports 179 | 180 | To export a single thing from a file so that other files may import it, assign 181 | over the value at `module.exports`: 182 | 183 | ``` js 184 | module.exports = function (n) { 185 | return n * 111 186 | }; 187 | ``` 188 | 189 | Now when some module `main.js` loads your `foo.js`, the return value of 190 | `require('./foo.js')` will be the exported function: 191 | 192 | ``` js 193 | var foo = require('./foo.js'); 194 | console.log(foo(5)); 195 | ``` 196 | 197 | This program will print: 198 | 199 | ``` 200 | 555 201 | ``` 202 | 203 | You can export any kind of value with `module.exports`, not just functions. 204 | 205 | For example, this is perfectly fine: 206 | 207 | ``` js 208 | module.exports = 555 209 | ``` 210 | 211 | and so is this: 212 | 213 | ``` js 214 | var numbers = []; 215 | for (var i = 0; i < 100; i++) numbers.push(i); 216 | 217 | module.exports = numbers; 218 | ``` 219 | 220 | There is another form of doing exports specifically for exporting items onto an 221 | object. Here, `exports` is used instead of `module.exports`: 222 | 223 | ``` js 224 | exports.beep = function (n) { return n * 1000 } 225 | exports.boop = 555 226 | ``` 227 | 228 | This program is the same as: 229 | 230 | ``` js 231 | module.exports.beep = function (n) { return n * 1000 } 232 | module.exports.boop = 555 233 | ``` 234 | 235 | because `module.exports` is the same as `exports` and is initially set to an 236 | empty object. 237 | 238 | Note however that you can't do: 239 | 240 | ``` js 241 | // this doesn't work 242 | exports = function (n) { return n * 1000 } 243 | ``` 244 | 245 | because the export value lives on the `module` object, and so assigning a new 246 | value for `exports` instead of `module.exports` masks the original reference. 247 | 248 | Instead if you are going to export a single item, always do: 249 | 250 | ``` js 251 | // instead 252 | module.exports = function (n) { return n * 1000 } 253 | ``` 254 | 255 | If you're still confused, try to understand how modules work in 256 | the background: 257 | 258 | ``` js 259 | var module = { 260 | exports: {} 261 | }; 262 | 263 | // If you require a module, it's basically wrapped in a function 264 | (function(module, exports) { 265 | exports = function (n) { return n * 1000 }; 266 | }(module, module.exports)) 267 | 268 | console.log(module.exports); // it's still an empty object :( 269 | ``` 270 | 271 | Most of the time, you will want to export a single function or constructor with 272 | `module.exports` because it's usually best for a module to do one thing. 273 | 274 | The `exports` feature was originally the primary way of exporting functionality 275 | and `module.exports` was an afterthought, but `module.exports` proved to be much 276 | more useful in practice at being more direct, clear, and avoiding duplication. 277 | 278 | In the early days, this style used to be much more common: 279 | 280 | foo.js: 281 | 282 | ``` js 283 | exports.foo = function (n) { return n * 111 } 284 | ``` 285 | 286 | main.js: 287 | 288 | ``` js 289 | var foo = require('./foo.js'); 290 | console.log(foo.foo(5)); 291 | ``` 292 | 293 | but note that the `foo.foo` is a bit superfluous. Using `module.exports` it 294 | becomes more clear: 295 | 296 | foo.js: 297 | 298 | ``` js 299 | module.exports = function (n) { return n * 111 } 300 | ``` 301 | 302 | main.js: 303 | 304 | ``` js 305 | var foo = require('./foo.js'); 306 | console.log(foo(5)); 307 | ``` 308 | 309 | ## bundling for the browser 310 | 311 | To run a module in node, you've got to start from somewhere. 312 | 313 | In node you pass a file to the `node` command to run a file: 314 | 315 | ``` 316 | $ node robot.js 317 | beep boop 318 | ``` 319 | 320 | In browserify, you do this same thing, but instead of running the file, you 321 | generate a stream of concatenated javascript files on stdout that you can write 322 | to a file with the `>` operator: 323 | 324 | ``` 325 | $ browserify robot.js > bundle.js 326 | ``` 327 | 328 | Now `bundle.js` contains all the javascript that `robot.js` needs to work. 329 | Just plop it into a single script tag in some html: 330 | 331 | ``` html 332 | 333 | 334 | 335 | 336 | 337 | ``` 338 | 339 | Bonus: if you put your script tag right before the `