UNPKG

1.66 kBMarkdownView Raw
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
9npm install require-swapper browserify
10```
11
12## Usage ##
13
14Require-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
19Suppose you have following index.js and swapping require function to `myrequire()` for module `aaa`,
20
21``` javascript
22var aaa = require('aaa')
23 , bbb = require('./dir1/bbb')
24 , ccc = require('./dir2/ccc')
25
26module.exports = function() {
27 aaa(bbb, ccc);
28}
29```
30
31It will output the folowing content to downstream.
32
33``` javascript
34var aaa = myrequire('aaa')
35 , bbb = require('./dir1/bbb')
36 , ccc = require('./dir2/ccc')
37
38module.exports = function() {
39 aaa(bbb, ccc);
40}
41```
42
43As the loading function for module `aaa` become swapped, browserify worker will not resolve and bundle the module `aaa` statically.
44It is anticipated that your custom loader function `myrequire` would resolve it.
45
46Combining browserify CLI, you can use it like following:
47
48``` bash
49browserify index.js -t [ require-swapper --fn 'myrequire' --module 'aaa' ] > bundle.js
50```
51
52
53## Options ##
54
55**options.fn**
56
57Specify your custom function name to swap `require()` call.
58
59**options.modules**
60
61A target module list or glob pattern to swap `require()`. If the option is not specified, all `require()` call will be replaced.
62