UNPKG

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