|
| 1 | +# Develop A Package |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +You can follow the steps to develop a package using [spm](https://github.com/spmjs/spm). |
| 6 | + |
| 7 | +``` |
| 8 | +$ spm --version |
| 9 | +3.x.x |
| 10 | +``` |
| 11 | + |
| 12 | +Make sure that [email protected] is installed. |
| 13 | + |
| 14 | +## init |
| 15 | + |
| 16 | +```bash |
| 17 | +$ mkdir now |
| 18 | +$ cd now |
| 19 | +$ spm init |
| 20 | +``` |
| 21 | + |
| 22 | +``` |
| 23 | +Creating a spm package: |
| 24 | +[?] Package name: (now) |
| 25 | +[?] Version: (1.0.0) |
| 26 | +[?] Description: |
| 27 | +[?] Author: afc163 <[email protected]> |
| 28 | +
|
| 29 | +Initialize a spm package Succeccfully! |
| 30 | +``` |
| 31 | + |
| 32 | +Then you have a package named `now`. |
| 33 | + |
| 34 | +## Install dependencies |
| 35 | + |
| 36 | +Install default devDependencies first. |
| 37 | + |
| 38 | +```bash |
| 39 | +$ spm install |
| 40 | +``` |
| 41 | + |
| 42 | +We need [moment](http://momentjs.com) as a dependency which can be found [here](http://spmjs.io/package/moment). |
| 43 | + |
| 44 | +```bash |
| 45 | +$ spm install moment --save |
| 46 | +``` |
| 47 | + |
| 48 | +## Code and Debug |
| 49 | + |
| 50 | +Edit `index.js` as follow, just like nodejs. |
| 51 | + |
| 52 | +```javascript |
| 53 | +// require module in spm.dependencies |
| 54 | +var moment = require('moment'); |
| 55 | + |
| 56 | +// require relative file in you project |
| 57 | +// var util = require('./util'); |
| 58 | + |
| 59 | +var now = moment().format('MMMM Do YYYY, h:mm:ss a'); |
| 60 | +module.exports = now; |
| 61 | +``` |
| 62 | + |
| 63 | +Then edit `examples/index.md`: |
| 64 | + |
| 65 | +<pre> |
| 66 | +# Demo |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Normal usage |
| 71 | + |
| 72 | +````javascript |
| 73 | +var now = require('../index'); |
| 74 | +console.log(now); |
| 75 | +```` |
| 76 | +</pre> |
| 77 | + |
| 78 | + |
| 79 | +Run `spm doc` to start a documentation service at `127.0.0.1:8000` . |
| 80 | + |
| 81 | +```bash |
| 82 | +$ spm doc |
| 83 | +``` |
| 84 | + |
| 85 | +Open [http://127.0.0.1:8000/examples/](http://127.0.0.1:8000/examples/) in browser to see the result. |
| 86 | + |
| 87 | +Except using three ` in Markdown file, you can also use four ` to wrap your code. |
| 88 | + |
| 89 | +It is a special rule that make your code highlighted and would be inserted to document page as a script block so they can be excuted at the same time. That is very useful for debugging your demo and writing a beautiful documentation both. |
| 90 | + |
| 91 | +If you want to insert a iframe in your demo, make your code to `iframe` type. |
| 92 | + |
| 93 | +<pre> |
| 94 | +````iframe:600 |
| 95 | +I am in a iframe of 600px high |
| 96 | +```` |
| 97 | +</pre> |
| 98 | + |
| 99 | + |
| 100 | +> If you don't want to debug your code by `spm doc`, you can try [seajs-wrap](https://github.com/seajs/seajs-wrap) or [spm-server](https://github.com/spmjs/spm-server/) to debug `CommonJS` modules in development. |
| 101 | +
|
| 102 | +## Add Test Case |
| 103 | + |
| 104 | +Edit test file at `tests/now-spec.js`. We introduce a default assert solution [expect.js](http://spmjs.io/package/expect.js). |
| 105 | + |
| 106 | +```javascript |
| 107 | +var expect = require('expect.js'); |
| 108 | +var now = require('../index'); |
| 109 | + |
| 110 | +describe('now', function() { |
| 111 | + |
| 112 | + it('normal usage', function() { |
| 113 | + expect(now).to.be.a('string'); // add this |
| 114 | + }); |
| 115 | + |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | +See tests result. |
| 120 | + |
| 121 | +```bash |
| 122 | +$ spm test |
| 123 | +``` |
| 124 | + |
| 125 | +You can also open [http://127.0.0.1:8000/tests/runner.html](http://127.0.0.1:8000/tests/runner.html) in browser. |
| 126 | + |
| 127 | +## Publish |
| 128 | + |
| 129 | +Now you have a great package having wonderful function and complete tests case, you can publish the package to [spmjs.io](http://spmjs.io/). |
| 130 | + |
| 131 | +```bash |
| 132 | +$ spm publish |
| 133 | +``` |
| 134 | + |
| 135 | +You should run `spm login` first to get permission, otherwise it would propmt the authorization problem. |
| 136 | + |
| 137 | +```bash |
| 138 | +$ spm login |
| 139 | +``` |
| 140 | + |
| 141 | +`username` is the name of your github account, and `authkey` can be found at http://spmjs.io/account after signing in. |
| 142 | + |
| 143 | +> The package `now` is published by me, of cause. You should change other name and retry. |
| 144 | +
|
| 145 | +## Documentation |
| 146 | + |
| 147 | +spmjs.io can host your package's documentation. What all you need to do is editing `README.md` and `examples` folder, preview it by `spm doc watch`, then publish it to spmjs.io. |
| 148 | + |
| 149 | +```bash |
| 150 | +$ spm doc publish |
| 151 | +``` |
| 152 | + |
| 153 | +The documentation url is `http://spmjs.io/docs/{{name}}/` for latest version and `http://spmjs.io/docs/{{name}}/{{version}}/` for each versions. |
| 154 | + |
| 155 | +For example, http://spmjs.io/docs/now/. |
| 156 | + |
| 157 | +## Build |
| 158 | + |
| 159 | +```bash |
| 160 | +$ spm build |
| 161 | +``` |
| 162 | + |
| 163 | +This command will build the files indicated by `spm.main` and `spm.output` field to `dist` folder. The `spm.buildArgs` would be executed as arguments. |
| 164 | + |
| 165 | +The default build result is a package which could be deployed at cdn. Then you can use it by using [Sea.js ](https://github.com/seajs/seajs/), [[email protected]](https://raw.githubusercontent.com/seajs/seajs/2.2.1/dist/sea.js) for example. |
| 166 | + |
| 167 | + |
| 168 | +```html |
| 169 | +<script src="http://cdn.example.com/path/to/sea.js"></script> |
| 170 | +<script src="http://cdn.example.com/path/to/seajs-combo.js"></script><!-- If your need that --> |
| 171 | +<script> |
| 172 | + seajs.config({ |
| 173 | + base: 'http://cdn.example.com/', |
| 174 | + alias: { |
| 175 | + now: 'now/1.0.0/index.js' |
| 176 | + } |
| 177 | + }); |
| 178 | + // load http://cdn.example.com/??now/1.0.0/index.js,moment/2.6.0/moment.js |
| 179 | + seajs.use(['now'], function(now) { |
| 180 | + console.log(now); |
| 181 | + }); |
| 182 | +</script> |
| 183 | +``` |
| 184 | + |
| 185 | +And We strongly recommend building a [standalone](/documentation/spm-commands#spm-build) package by adding an argument. |
| 186 | + |
| 187 | +```bash |
| 188 | +$ spm build --include standalone |
| 189 | +``` |
| 190 | + |
| 191 | +```html |
| 192 | +<!-- use it without loader --> |
| 193 | +<script src="http://cdn.example.com/path/to/standalone.js"></script> |
| 194 | +``` |
| 195 | + |
| 196 | +## Congratulation |
| 197 | + |
| 198 | +Now you learn how to develop a package using spm, welcome to publish your packages here! |
0 commit comments