@@ -27,3 +27,90 @@ npm install --save couchdb-builder
2727[couchdb-builder]: https://www.npmjs.com/package/couchdb-builder
2828
2929-->
30+
31+ ## What does it do?
32+
33+ * CouchDB builder* recursively reads a directory of flat files in various
34+ formats, and builds a JSON document suitable for use with CouchDB. The most
35+ common application is for CouchDB design documents, that typically contain
36+ embedded code.
37+
38+ ## Differences to [ couchdb-compile]
39+
40+ ### Better support for CommonJS modules
41+
42+ Module support in [ couchdb-compile] involves using [ ` .toString() ` ] on the
43+ module's exports. This does not mix well with some common patterns of CommonJS
44+ modules. Take the following module as an example:
45+
46+ ``` js
47+ var a = ' a' ;
48+
49+ module .exports = function () {
50+ return a;
51+ };
52+ ```
53+
54+ Using [ ` .toString() ` ] on this module would result in the following code:
55+
56+ ``` js
57+ function () {
58+ return a;
59+ };
60+ ```
61+
62+ Unfortunately, when this code is executed inside CouchDB, the variable ` a ` will
63+ be ` undefined ` , and hence the behavior has changed from the original module.
64+
65+ * CouchDB builder* takes a different approach, and instead surrounds the original
66+ source code in a light-weight wrapper. This wrapper allows the original code to
67+ function identically when used inside CouchDB, and when ` require ` 'd as a
68+ CommonJS module.
69+
70+ The same module would look something like this when surrounded in the wrapper:
71+
72+ ``` js
73+ if (! module ) { var module = {}; }
74+
75+ var a = ' a' ;
76+
77+ module .exports = function () {
78+ return a;
79+ };
80+
81+ module .exports ;
82+ ```
83+
84+ This works because CouchDB internally uses [ ` eval() ` ] , which returns the value
85+ of the last expression evaluated (i.e. ` module.exports ` ).
86+
87+ [ `.tostring()` ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function/toString
88+ [ `eval()` ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/eval
89+
90+ ### Built-in support for CoffeeScript
91+
92+ CoffeeScript is not directly supported by [ couchdb-compile] , although there is
93+ currently an open [ pull request] designed to address this.
94+
95+ With * CouchDB builder* , CoffeeScript is automatically compiled to JavaScript,
96+ then surrounded with the same light-weight wrapper as any other JavaScript file.
97+ This means that CoffeeScript "just works" without any special configuration.
98+
99+ Although CouchDB * does* natively support CoffeeScript, it currently seems to
100+ re-compile on every execution. It is almost certainly more performant to compile
101+ once (it also simplifies the wrapper code).
102+
103+ [ pull request ] : https://github.com/jo/couchdb-compile/pull/29
104+
105+ ### Unsupported features
106+
107+ The following features of [ couchdb-compile] are not currently supported. Please
108+ open an issue if further discussion is warranted:
109+
110+ - Attachments.
111+ - Automatic generation of the ` _id ` key. If no ` _id ` is specified, the document
112+ will not have one.
113+ - Special treatment of ` index.js ` or ` index.coffee ` files as CommonJS modules.
114+ All ` .js ` and ` .coffee ` files are treated as modules in * CouchDB builder* .
115+
116+ [ couchdb-compile ] : https://github.com/jo/couchdb-compile
0 commit comments