1 | # browserify-require-swapper
|
2 | [![Build Status](https://secure.travis-ci.org/stomita/browserify-require-swapper.png?branch=master)](http://travis-ci.org/stomita/browserify-require-swapper)
|
3 |
|
4 | [Browserify](http://browserify.org) v2 transform to swap CommonJS require call to custom function call
|
5 |
|
6 | ## Installation ##
|
7 |
|
8 | ``` bash
|
9 | npm install require-swapper browserify
|
10 | ```
|
11 |
|
12 | ## Usage ##
|
13 |
|
14 | Require-swapper swaps all CommonJS `require()` function calls in your code to specified custom function, or for certain module which matches given target module list.
|
15 |
|
16 |
|
17 | ## Example ###
|
18 |
|
19 | Suppose you have following index.js and swapping require function to `myrequire()` for module `aaa`,
|
20 |
|
21 | ``` javascript
|
22 | var aaa = require('aaa')
|
23 | , bbb = require('./dir1/bbb')
|
24 | , ccc = require('./dir2/ccc')
|
25 |
|
26 | module.exports = function() {
|
27 | aaa(bbb, ccc);
|
28 | }
|
29 | ```
|
30 |
|
31 | It will output the folowing content to downstream.
|
32 |
|
33 | ``` javascript
|
34 | var aaa = myrequire('aaa')
|
35 | , bbb = require('./dir1/bbb')
|
36 | , ccc = require('./dir2/ccc')
|
37 |
|
38 | module.exports = function() {
|
39 | aaa(bbb, ccc);
|
40 | }
|
41 | ```
|
42 |
|
43 | As the loading function for module `aaa` become swapped, browserify worker will not resolve and bundle the module `aaa` statically.
|
44 | It is anticipated that your custom loader function `myrequire` would resolve it.
|
45 |
|
46 | Combining browserify CLI, you can use it like following:
|
47 |
|
48 | ``` bash
|
49 | browserify index.js -t [ require-swapper --fn 'myrequire' --module 'aaa' ] > bundle.js
|
50 | ```
|
51 |
|
52 |
|
53 | ## Options ##
|
54 |
|
55 | **options.fn**
|
56 |
|
57 | Specify your custom function name to swap `require()` call.
|
58 |
|
59 | **options.modules**
|
60 |
|
61 | A target module list or glob pattern to swap `require()`. If the option is not specified, all `require()` call will be replaced.
|
62 |
|