• Stars
    star
    402
  • Rank 103,323 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

factor browser-pack bundles into common shared bundles

factor-bundle

factor browser-pack bundles into a common bundle and entry-specific bundles

build status

example

x.js:

var z = require('./z.js');
var w = require('./w.js');
console.log(z(5) * w(2));

y.js:

var z = require('./z.js');
console.log(z(2) + 111);

z.js:

module.exports = function (n) { return n * 111 }

w.js:

module.exports = function (n) { return n * 50 }

Now run factor-bundle as a plugin (new in browserify 3.28.0):

browserify x.js y.js -p [ factor-bundle -o bundle/x.js -o bundle/y.js ] \
  -o bundle/common.js

or you can pipe module-deps json directly into the factor-bundle command:

$ module-deps x.js y.js | factor-bundle \
  x.js -o bundle/x.js \
  y.js -o bundle/y.js \
  > bundle/common.js

or factor out an existing bundle already compiled by browserify:

$ browserify x.js y.js > bundle.js
$ browser-unpack < bundle.js | factor-bundle \
  x.js -o bundle/x.js \
  y.js -o bundle/y.js \
  > bundle/common.js

Whichever one of these 3 options, you take, you can now have 2 pages, each with a different combination of script tags but with all the common modules factored out into a common.js to avoid transferring the same code multiple times:

<script src="/bundle/common.js"></script>
<script src="/bundle/x.js"></script>
<script src="/bundle/common.js"></script>
<script src="/bundle/y.js"></script>

to verify this works from node you can do:

$ cat bundle/common.js bundle/x.js | node
55500
$ cat bundle/common.js bundle/y.js | node
333

command-line outpipe example

We can pipe each output file through some other processes. Here we'll do minification with uglify compression with gzip:

browserify files/*.js \
    -p [ factor-bundle -o 'uglifyjs -cm | gzip > bundle/`basename $FILE`.gz' ] \
    | uglifyjs -cm | gzip > bundle/common.js.gz

api plugin example

If you prefer you can use the factor-bundle plugin api directly in code:

var browserify = require('browserify');
var fs = require('fs');

var files = [ './files/x.js', './files/y.js' ];
var b = browserify(files);
b.plugin('factor-bundle', { outputs: [ 'bundle/x.js', 'bundle/y.js' ] });
b.bundle().pipe(fs.createWriteStream('bundle/common.js'));

or instead of writing to files you can output to writable streams:

var browserify = require('browserify');
var concat = require('concat-stream');

var files = [ './files/x.js', './files/y.js' ];
var b = browserify(files);

b.plugin('factor-bundle', { outputs: [ write('x'), write('y') ] });
b.bundle().pipe(write('common'));

function write (name) {
    return concat(function (body) {
        console.log('// ----- ' + name + ' -----');
        console.log(body.toString('utf8'));
    });
}

usage

You can use factor-bundle as a browserify plugin:

browserify -p [ factor-bundle OPTIONS ]

where OPTIONS are:

  -o  Output FILE or CMD that maps to a corresponding entry file at the same
      index. CMDs are executed with $FILE set to the corresponding input file.
      Optionally specify a function that returns a valid value for this argument.
 
  -e  Entry file to use, overriding the entry files listed in the original
      bundle.

or you can use the command:

usage: factor-bundle [ x.js -o bundle/x.js ... ] > bundle/common.js

Read `module-deps` json output from stdin, factoring each entry file out into
the corresponding output file (-o).

  -o FILE, -o CMD

    Write output to FILE or CMD. CMD is distinguished from FILE by the presence
    of one or more `>` or `|` characters. For example, use:
    
      factor-bundle browser/a.js browser/b.js \
        -o bundle/a.js -o bundle/b.js
    
    to write to a FILE. And do something like:

      factor-bundle browser/*.js \
        -o 'uglifyjs -cm | gzip > bundle/`basename $FILE`.gz'

    to use a CMD. In the CMD, $FILE is set to the corresponding input file path.

    If there is a trailing unpaired `-o`, that file will be used for the common
    bundle output. Otherwise, the final bundle is written to stdout.

methods

var factor = require('factor-bundle')

var fr = factor(files, opts={})

Return a transform stream tr that factors the array of entry path strings files out into bundle files. The input format that fr expects is described in the module-deps package.

The output format for fr and each of the fr sub-streams given by each 'stream' event is also in the module-deps format.

opts.o or opts.outputs should be an array that pairs up with the files array to specify where each bundle output for each entry file should be written. The elements in opts.o can be string filenames, writable streams, or functions that return a string filename or writable stream.

opts.entries or opts.e should be the array of entry files to create a page-specific bundle for each file. If you don't pass in an opts.entries, this information is gathered from browserify itself.

The files held in common among > opts.threshold (default: 1) bundles will be output on the fr stream itself. The entry-specific bundles are diverted into each 'stream' event's output. opts.threshold can be a number or a function opts.threshold(row, groups) where row is a module-deps object and groups is an array of bundles which depend on the row. If the threshold function returns true, that row and all its dependencies will go to the common bundle. If false, the row (but not its dependencies) will go to each bundle in groups. For example:

factor(files, {threshold: function(row, groups) {
    if (/.*a\.js$/.test(row.id)) return false;
    if (/.*[z]\.js$/.test(row.id)) return true;
    return this._defaultThreshold(row, groups);
}});

events

fr.on('stream', function (stream) {})

Each entry file emits a 'stream' event containing all of the entry-specific rows that are only used by that entry file (when opts.threshold === 1, the default).

The entry file name is available as stream.file.

b.on('factor.pipeline', function (file, pipeline) {})

Emits the full path to the entry file (file) and a labeled-stream-splicer (pipeline) for each entry file with these labels:

You 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.

Event handlers must be attached before calling b.plugin.

install

With npm do:

npm install factor-bundle

license

MIT

More Repositories

1

browserify

browser-side require() the node.js way
JavaScript
14,500
star
2

browserify-handbook

how to build modular applications with browserify
JavaScript
4,574
star
3

watchify

watch mode for browserify builds
JavaScript
1,783
star
4

events

Node's event emitter for all engines.
JavaScript
1,351
star
5

resolve

Implements the node.js require.resolve() algorithm
JavaScript
765
star
6

crypto-browserify

partial implementation of node's `crypto` for the browser
JavaScript
641
star
7

wzrd.in

browserify as a service.
JavaScript
633
star
8

browserify-website

the code that runs http://browserify.org
HTML
590
star
9

brfs

browserify fs.readFileSync() static asset inliner
JavaScript
559
star
10

rustify

Rust WebAssembly transform for Browserify
JavaScript
494
star
11

webworkify

launch a web worker that can require() in the browser with browserify
JavaScript
412
star
12

tinyify

a browserify plugin that runs various optimizations, so you don't have to install them all manually. makes your bundles tiny!
JavaScript
409
star
13

detective

Find all calls to require() no matter how deeply nested using a proper walk of the AST
JavaScript
409
star
14

commonjs-assert

Node.js's require('assert') for all engines
JavaScript
292
star
15

sha.js

Streamable SHA hashes in pure javascript
JavaScript
285
star
16

http-browserify

node's http module, but for the browser
JavaScript
243
star
17

node-util

node.js util module as a module
JavaScript
239
star
18

module-deps

walk the dependency graph to generate a stream of json output
JavaScript
209
star
19

bundle-collapser

convert bundle paths to IDs to save bytes in browserify bundles
JavaScript
194
star
20

vm-browserify

require('vm') like in node but for the browser
JavaScript
189
star
21

pbkdf2

PBKDF2 with any supported hashing algorithm in Node
JavaScript
184
star
22

browser-pack

pack node-style source files from a json stream into a browser bundle
JavaScript
173
star
23

static-eval

evaluate statically-analyzable expressions
JavaScript
169
star
24

path-browserify

The path module from Node.js for browsers
JavaScript
154
star
25

common-shakeify

browserify tree shaking plugin using `common-shake`
JavaScript
105
star
26

browser-resolve

resolve function which support the browser field in package.json
JavaScript
99
star
27

stream-browserify

the stream module from node core for browsers
JavaScript
98
star
28

randombytes

random bytes from browserify stand alone
JavaScript
94
star
29

diffie-hellman

pure js diffie-hellman
JavaScript
89
star
30

awesome-browserify

🔮 A curated list of awesome Browserify resources, libraries, and tools.
86
star
31

syntax-error

detect and report syntax errors in source code strings
JavaScript
78
star
32

static-module

convert module usage to inline expressions
JavaScript
73
star
33

ify-loader

Webpack loader to handle browserify transforms as intended.
JavaScript
67
star
34

browserify-aes

aes, for browserify
JavaScript
61
star
35

createHmac

Node style HMAC for use in the browser, with native implementation in node
JavaScript
61
star
36

stream-splicer

streaming pipeline with a mutable configuration
JavaScript
55
star
37

browser-unpack

parse a bundle generated by browser-pack
JavaScript
54
star
38

browserify-zlib

Full zlib module for browserify
JavaScript
54
star
39

createHash

Node style hashes for use in the browser, with native hash functions in node
JavaScript
52
star
40

md5.js

node style md5 on pure JavaScript
JavaScript
41
star
41

labeled-stream-splicer

stream splicer with labels
JavaScript
39
star
42

browserify-sign

createSign and createVerify in your browser
JavaScript
37
star
43

ripemd160

JavaScript component to compute the RIPEMD160 hash of strings or bytes.
JavaScript
32
star
44

console-browserify

Emulate console for all the browsers
JavaScript
31
star
45

publicEncrypt

publicEncrypt/privateDecrypt for browserify
JavaScript
29
star
46

buffer-xor

A simple module for bitwise-xor on buffers
JavaScript
29
star
47

insert-module-globals

insert implicit module globals into a module-deps stream
JavaScript
26
star
48

createECDH

browserify version of crypto.createECDH
JavaScript
24
star
49

timers-browserify

timers module for browserify
JavaScript
23
star
50

EVP_BytesToKey

JavaScript
20
star
51

parse-asn1

JavaScript
19
star
52

browserify-rsa

JavaScript
19
star
53

cipher-base

abstract base class for crypto-streams
JavaScript
18
star
54

browserify-cipher

JavaScript
18
star
55

deps-sort

sort module-deps output for deterministic browserify bundles
JavaScript
16
star
56

tty-browserify

the tty module from node core for browsers
JavaScript
14
star
57

acorn-node

the acorn javascript parser, preloaded with plugins for syntax parity with recent node versions
JavaScript
12
star
58

hash-base

abstract base class for hash-streams
JavaScript
11
star
59

admin

administrative procedures for the browserify org
11
star
60

discuss

discuss project organization, initiatives, and whatever!
11
star
61

browserify-des

DES for browserify
JavaScript
10
star
62

randomfill

JavaScript
9
star
63

buffer-reverse

A lite module for byte reversal on buffers.
JavaScript
6
star
64

hash-test-vectors

JavaScript
3
star
65

pseudorandombytes

pseudorandombytes for the browser
JavaScript
3
star
66

detective-esm

find es module dependencies [experimental]
JavaScript
2
star
67

timing-safe-equal

JavaScript
2
star
68

perf-hooks-browserify

[WIP] The perf_hooks node module API for browserify
JavaScript
2
star
69

.github

Housing of Browserify's GitHub configuration and base files
1
star
70

scrypt

JavaScript
1
star