|
| 1 | +## Modules Package |
| 2 | + |
| 3 | +The modules package provides build-steps for adding CommonJS modules to your |
| 4 | +design doc. Use this for loading JavaScript modules (using require() and exports) |
| 5 | +for use server-side with CouchDB and client-side in the browser. |
| 6 | + |
| 7 | + |
| 8 | +### Install |
| 9 | + |
| 10 | +Add `modules` to your dependencies section in `kanso.json`. |
| 11 | + |
| 12 | +```javascript |
| 13 | +... |
| 14 | + "dependencies": { |
| 15 | + "modules": null, |
| 16 | + ... |
| 17 | + } |
| 18 | +``` |
| 19 | + |
| 20 | +Run `kanso install` to fetch the package. |
| 21 | + |
| 22 | + |
| 23 | +### Configure |
| 24 | + |
| 25 | +To tell the package which files to read and add to the design doc, add the |
| 26 | +modules property to your `kanso.json` and list the files you want to load. |
| 27 | + |
| 28 | +```javascript |
| 29 | +... |
| 30 | + "modules": ["app.js", "lib"] |
| 31 | + ... |
| 32 | + "dependencies": { |
| 33 | + "modules": null, |
| 34 | + ... |
| 35 | + } |
| 36 | +``` |
| 37 | + |
| 38 | +You can list individual files or whole directories in the `modules` property. |
| 39 | +Hidden files and directories (with a preceeding '.') and files with an extension |
| 40 | +other than `.js` are ignored. |
| 41 | + |
| 42 | + |
| 43 | +### Usage |
| 44 | + |
| 45 | +Once you've updated your settings in `kanso.json`, the next time you `kanso push` |
| 46 | +these files will be read from the filesystem and added as modules to your |
| 47 | +app. These modules are then available inside your show, list, update etc functions |
| 48 | +server-side and can be accessed client-side by including the `modules.js` script |
| 49 | +in your page: |
| 50 | + |
| 51 | +```xml |
| 52 | +<script src="modules.js"></script> |
| 53 | +<script> |
| 54 | + var db = require('db'); |
| 55 | +</script> |
| 56 | +``` |
| 57 | + |
| 58 | +The require path used for the modules is the relative path to the file from it's |
| 59 | +package directory. |
| 60 | + |
| 61 | + |
| 62 | +### Modules.js Attachment |
| 63 | + |
| 64 | +This will bundle all your CommonJS modules into a single `.js` file for use in |
| 65 | +the browser. Once you include it in the page you can then use the `require()` |
| 66 | +function. If you're behind a _rewrite URL, you may have to set up a rewrite to the |
| 67 | +`modules.js` attachment before you can include it. |
| 68 | + |
| 69 | +If you only use modules server-side, you can avoid creating the attachments by |
| 70 | +adding the following property to your `kanso.json` file: |
| 71 | + |
| 72 | +```javascript |
| 73 | +... |
| 74 | + "modules_attachment": false, |
| 75 | + ... |
| 76 | + "dependencies": { |
| 77 | + "modules": null |
| 78 | + ... |
| 79 | + } |
| 80 | +``` |
| 81 | + |
| 82 | +You can minify the module.js attachment by adding the `--minify` flag when you push: |
| 83 | + |
| 84 | +``` |
| 85 | +kanso push http://localhost:5984/dbname --minify |
| 86 | +``` |
| 87 | + |
| 88 | +You can also add `minify` to the environment definitions in your `.kansorc` file: |
| 89 | + |
| 90 | +``` |
| 91 | +exports.env = { |
| 92 | + "default": { |
| 93 | + db: "http://user:password@localhost:5984/dbname" |
| 94 | + }, |
| 95 | + "prod": { |
| 96 | + db: "http://user:password@hostname/dbname", |
| 97 | + minify: true |
| 98 | + } |
| 99 | +}; |
| 100 | +``` |
0 commit comments