1 | # bundle-collapser
|
2 |
|
3 | convert bundle paths to IDs to save bytes in browserify bundles
|
4 |
|
5 | [![build status](https://secure.travis-ci.org/substack/bundle-collapser.png)](http://travis-ci.org/substack/bundle-collapser)
|
6 |
|
7 | # example
|
8 |
|
9 | The easiest way to use bundle-collapser is from the plugin:
|
10 |
|
11 | ```
|
12 | $ browserify -p bundle-collapser/plugin main.js
|
13 | ```
|
14 |
|
15 | Instead of the usual output which would have the original `require('./foo.js')`
|
16 | style calls in it, the output just has `require(2)` style paths, which minifies
|
17 | more compactly:
|
18 |
|
19 | ``` js
|
20 | (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)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"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
21 | module.exports = function (n) { return n * 5 }
|
22 |
|
23 | },{}],2:[function(require,module,exports){
|
24 | var bar = require(1);
|
25 |
|
26 | module.exports = function (n) { return bar(n+1) }
|
27 |
|
28 | },{}],3:[function(require,module,exports){
|
29 | var foo = require(2);
|
30 | var bar = require(1);
|
31 |
|
32 | console.log(foo(5) * bar(2));
|
33 |
|
34 | },{}]},{},[3]);
|
35 | ```
|
36 | ## api
|
37 |
|
38 | You can use bundle-collapser from the api too:
|
39 |
|
40 | ``` js
|
41 | var collapse = require('bundle-collapser');
|
42 | var fs = require('fs');
|
43 |
|
44 | var src = fs.readFileSync(__dirname + '/bundle.js', 'utf8');
|
45 | collapse(src).pipe(process.stdout);
|
46 | ```
|
47 |
|
48 | # usage
|
49 |
|
50 | There is also a command-line program included in this distribution:
|
51 |
|
52 | ```
|
53 | usage: bundle-collapser {FILE | -} {OPTIONS}
|
54 |
|
55 | Collapse a browser-pack/browserify bundle from STDIN or a FILE.
|
56 |
|
57 | OPTIONS:
|
58 |
|
59 | -h --help Show this message.
|
60 |
|
61 | ```
|
62 |
|
63 | # methods
|
64 |
|
65 | ``` js
|
66 | var collapse = require('bundle-collapser')
|
67 | ```
|
68 |
|
69 | ## var stream = collapse(src)
|
70 |
|
71 | Return a readable `stream` of output from
|
72 | [browser-pack](https://npmjs.org/package/browser-pack) with the input source
|
73 | string `src` converted to have its `require()` calls collapsed down to the
|
74 | dependency targets in the "deps" fields from the unpacking.
|
75 |
|
76 | # install
|
77 |
|
78 | With [npm](https://npmjs.org), to get the library do:
|
79 |
|
80 | ```
|
81 | npm install bundle-collapser
|
82 | ```
|
83 |
|
84 | and to get the command-line program do:
|
85 |
|
86 | ```
|
87 | npm install -g bundle-collapser
|
88 | ```
|
89 |
|
90 | # license
|
91 |
|
92 | MIT
|