1 | # Plugin.js
|
2 |
|
3 | ## Features
|
4 |
|
5 | - ability to load entire directories
|
6 | - add remote plugins (dnode)
|
7 |
|
8 | ## Example
|
9 |
|
10 | bootstrap.js
|
11 |
|
12 | ```javascript
|
13 | var plugin = require("plugin"),
|
14 | express = require("express"),
|
15 |
|
16 | plugin(express()).
|
17 | params({
|
18 | http: { port: 8080 }
|
19 | }).
|
20 | require(__dirname + "/config.js").
|
21 | require(__dirname + "/someRoutes.js").
|
22 | load();
|
23 | ```
|
24 |
|
25 | config.js
|
26 |
|
27 | ```javascript
|
28 | exports.plugin = function(server, loader) {
|
29 | server.listen(loader.params("http.port") || 80);
|
30 | }
|
31 | ```
|
32 |
|
33 | someRoutes.js
|
34 | ```javascript
|
35 | module.exports = function(server) {
|
36 | server.get("/hello", function(req, res) {
|
37 | res.end("world!");
|
38 | })
|
39 | }
|
40 | ```
|
41 |
|
42 |
|
43 | ## Plugin API
|
44 |
|
45 | ### loader .plugin(plugInto, ...)
|
46 |
|
47 | Initializes the loader for local / remote plugins. `plugInto` is passed into the first parameter when calling `.plugin()` on each required module.
|
48 |
|
49 | ### loader.use(extension)
|
50 |
|
51 | Extends the loader. This allows you to customize how modules are loaded into your sandbox.
|
52 |
|
53 | ```javascript
|
54 | require("plugin")().
|
55 | use(require("plugin-dnode")).
|
56 | require("dnode://localhost").
|
57 | load();
|
58 | ```
|
59 |
|
60 | ### loader.params(keyOrParams, value)
|
61 |
|
62 | Get / set params
|
63 |
|
64 | bootstrap.js
|
65 |
|
66 | ```javascript
|
67 | require("plugin")().
|
68 | params("some.message", 8080).
|
69 | require(__dirname + "/hello.js").
|
70 | load();
|
71 | ```
|
72 |
|
73 | server.js
|
74 | ```javascript
|
75 | exports.plugin = function(loader) {
|
76 | console.log(loader.params("some.message"));
|
77 | }
|
78 | ```
|
79 |
|
80 | ### loader.require(source, ...)
|
81 |
|
82 | path to the plugins
|
83 |
|
84 | ```javascript
|
85 | loader.
|
86 | require(__dirname + "/plugin.js").
|
87 | require(__dirname + "/someDirectory.js").
|
88 | require("multiple", "plugins").
|
89 | require("dnode://localhost").
|
90 | load();
|
91 | ```
|
92 |
|
93 | ### loader.load(callback)
|
94 |
|
95 | loads the required dependencies
|
96 |
|
97 | ### loader.module(search)
|
98 |
|
99 | Returns one loaded module based on the search query. Note that calling this method
|
100 | may load the given module if it hasn't already. Here's a real-world example:
|
101 |
|
102 | bootstrap.js
|
103 |
|
104 | ```javascript
|
105 | require("plugin")().
|
106 | params("http.port", 8080).
|
107 | require(__dirname + "/server.js").
|
108 | require(__dirname + "/routes.js").
|
109 | load();
|
110 | ```
|
111 |
|
112 | server.js
|
113 |
|
114 | ```javascript
|
115 | exports.isHttpServer = true; //not needed - just used for searching
|
116 | exports.plugin = function(loader) {
|
117 | var server = express();
|
118 | server.listen(loader.params("http.port"));
|
119 | return server;
|
120 | }
|
121 | ```
|
122 |
|
123 | routes.js
|
124 |
|
125 | ```javascript
|
126 | exports.plugin = function(loader) {
|
127 | var server = loader.module("server");
|
128 | server.get("/hello", function(req, res) {
|
129 | res.end("hello world!");
|
130 | })
|
131 | }
|
132 | ```
|
133 |
|
134 | Also note that you can search based on attributes. Here's `routes.js` again:
|
135 |
|
136 | ```javascript
|
137 | exports.plugin = function(loader) {
|
138 | var server = loader.module({ isHttpServer: true });
|
139 | //do stuff here
|
140 | }
|
141 | ```
|
142 |
|
143 | ### loader.modules(search)
|
144 |
|
145 | Just like `loader.module(search)`, but returns multiple modules.
|
146 |
|
147 | ### loader.exports
|
148 |
|
149 | This is all the plugins combined, and it's set once everything's loaded.
|
150 |
|