UNPKG

5.57 kBMarkdownView Raw
1# Requizzle #
2
3Swizzle a little something into your Node.js modules.
4
5
6## What's Requizzle? ##
7
8Requizzle provides a drop-in replacement for Node.js's `require()` function. This replacement
9allows you to change a module's source code at runtime.
10
11You can use Requizzle in your test cases, or in production code if you like to live dangerously.
12Requizzle has been tested with Node.js 0.10 and 0.11.
13
14Here's what Requizzle can do:
15
16### Look for modules in new places ###
17
18With Requizzle, you can add directories to the module lookup path, which forces Node.js to search
19those directories for modules. This can be useful if:
20
21+ You're tired of writing code like `require('../../../../../lib/foo')`.
22+ You want to expose your app's modules to external plugins. (Without Requizzle, it can be tough to
23do this if the plugin is located outside of your app directory.)
24
25### Add code before or after the module's source code ###
26
27Tamper with modules to your heart's delight by adding arbitrary code before or after the module's
28own source code.
29
30### Mess with child modules ###
31
32When you use Requizzle to require a module, you can force each child module's `require` method to
33inherit your changes to the parent module. (By default, only the parent module is changed.)
34
35### Preserve strict-mode declarations ###
36
37If a module starts with a strict-mode declaration, Requizzle keeps it in place. Your changes will
38appear after the strict-mode declaration.
39
40### Leave native modules alone ###
41
42If you use one of Node.js's built-in modules, such as `fs` or `path`, Requizzle won't mess with it.
43
44
45## Installation ##
46
47With npm:
48
49 npm install requizzle
50
51With git:
52
53 git clone git://github.com/hegemonic/requizzle.git
54 cd requizzle
55 npm install
56
57
58## Usage ##
59
60The Requizzle module exports a single function, which returns a drop-in replacement for
61`require()`.
62
63When you call the function, you must pass in an `options` object, which can include any of these
64properties:
65
66+ `extras`: A pair of functions that return text to insert before or after the module's source code.
67Each function accepts two parameters: `targetPath`, the path to the required module, and
68`parentModule`, the `Module` object for the module's parent. Each function must return a string.
69 + `extras.before`: A function that returns text to insert before the module's source code.
70 + `extras.after`: A function that returns text to insert after the module's source code.
71+ `infect`: Determines whether child modules are infected with the same changes as the parent
72module. Set to `true` to force child modules to inherit your changes. Defaults to `false`.
73+ `requirePaths`: An array of additional paths to search for required modules. For example, if
74`requirePaths` is set to `['/usr/lib/junk/modules']`, and you save a JavaScript module at
75`/usr/lib/junk/modules/mymodule.js`, you can require the module as `mymodule`. By default, the
76require path is not changed.
77
78
79## Examples ##
80
81```js
82var requizzle = require('requizzle');
83
84// Say hello and goodbye to each module.
85var logRequire = requizzle({
86 extras: {
87 before: function(targetPath, parentModule) {
88 return 'console.log("Hello %s!", ' + targetPath + ');\n';
89 },
90 after: function(targetPath, parentModule) {
91 return 'console.log("Goodbye %s!", ' + targetPath + ');\n';
92 }
93 }
94});
95// Prints "Hello /path/to/mymodule.js!" and "Goodbye /path/to/mymodule.js!"
96var myModule = logRequire('mymodule');
97
98// Look for modules in the current module's `lib` directory, and force child
99// modules to do the same.
100var path = require('path');
101var extraPathRequire = requizzle({
102 infect: true,
103 requirePaths: [path.join(__dirname, 'lib')]
104});
105// If `foo` needs to require a module in `./lib`, it can use `require('bar')`
106// instead of `require('./lib/bar')`.
107var foo = extraPathRequire('./foo');
108```
109
110
111## Troubleshooting ##
112
113Here are some problems you may run into when you use Requizzle, along with solutions to each
114problem. If you run into any problems that aren't addressed here, please file a new issue!
115
116### Requizzle slowed down my code! A lot! ###
117
118Requizzle adds minimal overhead to the module-loading process. In some cases, it may even be faster
119than Node.js's built-in `require()` function.
120
121However, your code will run _much_ slower than usual if you do both of the following:
122
123+ Use Requizzle's `infect` option.
124+ Require modules that have a lot of `require()` calls within the scope of individual functions.
125
126To fix this issue, find the module calls that are within function scope, and move them to each
127module's top-level scope. You can find the biggest offenders by using Node.js's built-in `--prof`
128option to profile your app, then running [node-tick](https://github.com/sidorares/node-tick) to
129create a report that shows the number of ticks per function.
130
131### Requizzle made my module do something weird! ###
132
133Do you have any [circular dependencies](http://nodejs.org/api/modules.html#modules_cycles) in the
134modules that aren't working? Circular dependencies can cause unusual behavior with Requizzle, just
135as they can without Requizzle. Try breaking the circular dependency.
136
137### Requizzle violates the sacred Law of Demeter! It's an unnatural abomination! ###
138
139Fair enough.
140
141
142## Acknowledgements ##
143
144Requizzle is very loosely adapted from Johannes Ewald's [rewire](https://github.com/jhnns/rewire)
145module, which is designed to modify a module's behavior for unit testing. If Requizzle doesn't meet
146your needs, please take a look at rewire!
147
148
149## License ##
150
151[MIT license](LICENSE).