UNPKG

2.71 kBJavaScriptView Raw
1var Path = require('path');
2//Utility functions to enable use of nodejs modules in the browser. Used in
3//[html-builder](http://github.com/Michieljoris/html-builder) and
4//[bb-server](http://github.com/Michieljoris/bb-server).
5
6//###wrap
7
8//Wrap a `string` of a nodejs module with the proper code to enable its use in
9//the browser. Specify the `language` your nodejs module is written in to match
10//the added code to the code of the module. Defaults to javascript.
11
12//If you leave 2 lines open at the top of every module wrap will replace the top
13//line with the prefix wrapping code. This way line numbers in your modules will
14//match the line numbers of the javascript file loaded in the browser
15
16var prefix = "denodify(function(require,module,exports,__filename,__dirname,process,global){\n";
17var postfix = "},moduleid);";
18
19exports.wrap = function(moduleid, string) {
20 if (string[0] === '\n') string = string.slice(1);
21 //TODO add extra info about file here:
22 // var newPrefix = prefix.replace(/moduleid/g, moduleid);
23 var newPostfix = postfix.replace(/moduleid/g, moduleid);
24 return prefix + string + newPostfix;
25};
26
27//###script
28
29//Script to load in your html file before all denodified scripts.
30
31//Takes one argument: an object? or array? of resolve info on modules used on the client.
32//This gets added to the nodify script's closure.
33exports.script = require('./make-script');
34
35//###resolve
36
37//Given a main `module`, parses it for require calls, loads the corresponding
38//module files and recursively parses them. Once all dependencies are found
39//calls the `callback` with a list of tags that if loaded in the browser in the
40//listed order all dependencies would be fullfilled for each module.
41
42//* `www` : the directory the server's root.
43//* `parent` : the path from `www` to the main module
44//* `module` : the id of the file if you were requiring it (without the js)
45//* `callback` : called as `callback(err, list)`.
46//* `tags` : return list of src files wrapped in tags
47// Returns a list of file names to add to a html file in script tags.
48// exports.resolve = require('./resolve').resolve;
49
50//###expand
51
52//Takes a list of script files and 'expands' any entry of a module to a list of
53//its recursive dependencies and the module itself. At the moment module
54//detection is done through a marking of the module file name by enclosing it in
55//[ and ].
56
57//TODO
58//expand could detect modules by parsing them using detective.
59//add config:
60//at the moment this is specifically written for html-builder. It needs to be made more general and html-builder will have to be adapted.
61// function expand(scriptBlock, wwwPath, cb, isDebug) {
62
63exports.expand = require('./expand');