@@ -22,15 +22,16 @@ The Javascript files in the package may live anywhere, in any directory. You ne
22
22
The rule is that you should follow the same rules for a dynamic module as you would for a Node.js library module.
23
23
24
24
The package.json file must contain the following:
25
-
25
+ ``` javascript
26
26
{
27
- "main":"<path to main module"
27
+ " main" : " <path to main module> "
28
28
}
29
+ ```
29
30
30
31
If you use other Node.js libraries, you must include the standard NPM dependencies blocks. The actual libraries shown
31
32
below are for illustrative purposes only; you can put whatever you want in the dependencies section, as long as they
32
33
can be accessed by the runtime server when NPM installs the package:
33
-
34
+ ``` javascript
34
35
{
35
36
" main" : " ./index.js" ,
36
37
@@ -47,6 +48,7 @@ can be accessed by the runtime server when NPM installs the package:
47
48
.
48
49
}
49
50
}
51
+ ```
50
52
51
53
52
54
The Web Server
@@ -116,7 +118,7 @@ Let's say we have a package that consists of just two files: the _package.json_
116
118
it interesting, the package uses Futures. Here are the files in the package:
117
119
118
120
*package.json*
119
-
121
+ ` ` ` javascript
120
122
{
121
123
" name" : " test-dynamic-module" ,
122
124
" version" : " 0.0.1" ,
@@ -125,9 +127,10 @@ it interesting, the package uses Futures. Here are the files in the package:
125
127
" futures" : " 2.1.0"
126
128
}
127
129
}
130
+ ` ` `
128
131
129
132
*index.js*
130
-
133
+ ` ` ` javascript
131
134
var Future = require (' futures' ).future ;
132
135
133
136
module .exports .helloWorld = function ()
@@ -140,9 +143,10 @@ it interesting, the package uses Futures. Here are the files in the package:
140
143
141
144
return future;
142
145
};
146
+ ` ` `
143
147
144
148
We can call the _helloWorld_ function like this:
145
-
149
+ ` ` ` javascript
146
150
var moduleResult = dynamicModuleLoader .load (' test-dynamic-module' );
147
151
moduleResult .when (function (err , module )
148
152
{
@@ -156,23 +160,25 @@ We can call the _helloWorld_ function like this:
156
160
// Do something with the module.
157
161
console .log (module .helloWorld ());
158
162
});
163
+ ` ` `
159
164
160
165
Changing Defaults
161
166
-----------------
162
167
163
168
By default the _DynamicModuleLoader_ assumes you will be downloading packages in _tar.gz_ form. You can change the
164
169
default like this:
165
-
170
+ ` ` ` javascript
166
171
var dynamicModuleLoader = new DynamicModuleLoader ({ defaultRemoteServerPackageFileExtension: ' .zip' });
167
172
168
173
// Will assume a .zip extension when requesting the package from the web server.
169
174
var moduleResult = dynamicModuleLoader .load (' test-dynamic-module' );
170
175
.
171
176
.
172
177
.
178
+ ` ` `
173
179
174
180
You can always override the default by specifying the extension when calling _load_:
175
-
181
+ ` ` ` javascript
176
182
// By default assume .tar.gz.
177
183
var dynamicModuleLoader = new DynamicModuleLoader ();
178
184
@@ -187,6 +193,13 @@ You can always override the default by specifying the extension when calling _lo
187
193
.
188
194
.
189
195
.
196
+ ` ` `
197
+
198
+ And you can regroup the installed modules in subdirectories of ` moduleInstallationDir` :
199
+ ` ` ` javascript
200
+ // The module will be installed at moduleInstallationDir/demo_modules/
201
+ var moduleResult = dynamicModuleLoader .load (' test-dynamic-module' , undefined , undefined , ' demo_modules' );
202
+ ` ` `
190
203
191
204
192
205
External Dependencies
@@ -207,10 +220,11 @@ Customizing NPM Options
207
220
-----------------------
208
221
209
222
You can customize the NPM options. For example, you can set NPM to perform a verbose install:
210
-
223
+ ` ` ` javascript
211
224
var dynamicModuleLoader = new DynamicModuleLoader ({
212
225
npmOptions: [' --production' , ' --verbose' ]
213
226
});
227
+ ` ` `
214
228
215
229
216
230
Skipping Installation
@@ -247,31 +261,11 @@ the pre-installed `node_modules/` directory will be copied.
247
261
` ` `
248
262
249
263
250
- Sharing node_modules between modules
251
- ------------------------------------
252
-
253
- If you plan to load a lot of modules containing often the same dependencies (e.g. deploying regularly new versions
254
- of a module), you can have the dynamic module loader sharing the node_modules instead of reinstalling them each time,
255
- in order to speed up the installation. To do that, load your module with the following arguments:
256
- ``` javascript
257
- var dynamicModuleLoader = new DynamicModuleLoader ();
258
- var moduleResult = dynamicModuleLoader .load (' test-dynamic-module' , undefined , undefined , nodeModulesInstallDirName);
259
- ```
260
- Instead of installing your module in ` moduleInstallationDir/moduleName ` , this will create an extra directory and your module
261
- will be installed in ` moduleInstallationDir/nodeModulesInstallDirName/moduleName ` . Then, it will copy the package.json of your
262
- module in ` moduleInstallationDir/nodeModulesInstallDirName ` , do the ` npm install ` there, and copy back the ` node_modules/ ` in
263
- the ` moduleName ` directory.
264
-
265
- Next time you install a module in the same ` nodeModulesInstallDirName ` , if the ` package.json ` file is the same, it will skip
266
- the ` npm install ` and directly copy the ` node_modules/ ` in your newly installed module directory.
267
- If the dependencies have changed, it runs again ` npm install ` before copying the ` node_modules/ ` .
268
-
269
-
270
264
Events
271
265
------
272
266
273
267
_DynamicModuleLoader_ is an event emitter. Here's how to listen for the events it fires:
274
-
268
+ ` ` ` javascript
275
269
var dynamicModuleLoader = new DynamicModuleLoader ();
276
270
dynamicModuleLoader .on (dynamicModuleLoader .events .moduleDownloaded , function (moduleName , downloadedFile , next )
277
271
{
@@ -316,6 +310,7 @@ _DynamicModuleLoader_ is an event emitter. Here's how to listen for the events
316
310
317
311
// moduleName - The name of the module loaded.
318
312
});
313
+ ` ` `
319
314
320
315
Next Function
321
316
-------------
0 commit comments