Slide 1

Slide 1 text

Creating JS modules jakobm.com @jakobmattsson

Slide 2

Slide 2 text

FishBrain is the fastest, easiest way to log and share your fishing. ! Get instant updates from anglers on nearby lakes, rivers, and the coast.

Slide 3

Slide 3 text

Google: fishbrain careers

Slide 4

Slide 4 text

baxter bucketful bucketful-loopia buffet-brunch cellsynt chalcogen commonjs-jquery confirm connect-rewrite dinode express-jit-coffee fn ftp-replace iota.js jakobmattsson-client-cookies jakobmattsson-cucumber jakobmattsson-serenade jakobmattsson-swfobject jscov json-request jsonrpc-http jsonrpc-http-browser-client jsonrpc-http-client-browserify jsonrpc-http-client-node jsonrpc-http-server locke locke-api locke-client locke-client-jsonrpc locke-consumer locke-store-mem locke-store-mongo locke-store-test loopia-api manikin manikin-mem manikin-mongodb manikin-tools mocha-http mocha-term-cov-reporter mongocmd nameify node-auther opra opra-compiler path-router path-router-decorator piescore plusone-mongohq plusone-nodejitsu powerfs rester rester-tools runr selenian trester underscore.plus viaduct-client viaduct-server wd-tools z-builtins z-core z-std-pack z-underscore zee Author of some modules

Slide 5

Slide 5 text

Hipster or homeless?

Slide 6

Slide 6 text

Hipster or homeless?

Slide 7

Slide 7 text

James ”substack” Halliday 400 modules

Slide 8

Slide 8 text

”But I’m a fronted dev!” ”I don’t care for Node.js!”

Slide 9

Slide 9 text

Your tools do ”But I’m a fronted dev!” ”I don’t care for Node.js!”

Slide 10

Slide 10 text

Your tools do You can use npm in the browser ”But I’m a fronted dev!” ”I don’t care for Node.js!”

Slide 11

Slide 11 text

*previous talk by Johan Nilsson

Slide 12

Slide 12 text

*previous talk by Johan Nilsson

Slide 13

Slide 13 text

Creating JS modules 5 things you don’t want to miss

Slide 14

Slide 14 text

#1

Slide 15

Slide 15 text

#1

Slide 16

Slide 16 text

package.json { "name": "z-core", "version": "0.6.0" } Then run ”npm publish” and you’re done. Collect fame and fortune.

Slide 17

Slide 17 text

{ "name": "z-core", "version": "0.6.0", "description": "Utility library for JavaScript promises", "keywords": [ "promises", "util", "functional" ], "author": "Jakob Mattsson (jakobm.com)", "license": "MIT", "main": "./lib/index.js", "repository": { "type": "git", "url": "http://github.com/jakobmattsson/z-core" }, "bugs": ”http://github.com/jakobmattsson/z-core/issues", "private": false,

Slide 18

Slide 18 text

"files": [ "lib", "LICENSE" ], "engines": { "npm": ">= 1.3 < 2", "node": ">= 0.10 <= 0.11" }, "devDependencies": { "mocha": "^1.18.0", "coffee-script": "^1.7.1" }, "dependencies": { "underscore": "*" }, "optionalDependencies": { "es6-promise": "^0.1.1" } } Files to be included Where does it work? Required to build/ test/etc Required to use Not required, but nice to have

Slide 19

Slide 19 text

Scripts "scripts": { "test": "mocha tests/*.js", "run": "node lib/server.js", "publish": "...", "install": "...", "stop": "...", "restart": ”...", ! "prepublish": ”...", "postpublish": ”..." }

Slide 20

Slide 20 text

Scripts mocha tests/*.js ! VS ! ./node_modules/.bin/mocha tests/*.js

Slide 21

Slide 21 text

Remember No global installs ! Leverage the scripts-attribute ! PATH=$PATH;./node_modules/.bin/ !

Slide 22

Slide 22 text

#2

Slide 23

Slide 23 text

#2 Semantic Versions

Slide 24

Slide 24 text

1 . 2 . 3 Major Minor Patch

Slide 25

Slide 25 text

Given a version number MAJOR.MINOR.PATCH, increment the: ! MAJOR version when you make incompatible API changes ! MINOR version when you add functionality in a backwards- compatible manner. ! PATCH version when you make backwards-compatible bug fixes. ! Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format. Summary

Slide 26

Slide 26 text

Note Software using Semantic Versioning MUST declare a public API. ! This API could be declared in the code itself or exist strictly in documentation. However it is done, it should be precise and comprehensive.

Slide 27

Slide 27 text

Note Major version zero (0.y.z) is for initial development. ! Anything may change at any time. The public API should not be considered stable.

Slide 28

Slide 28 text

Note Once a versioned package has been released, the contents of that version MUST NOT be modified. ! Any modifications MUST be released as a new version.

Slide 29

Slide 29 text

http://semver.org

Slide 30

Slide 30 text

IRL Things change, break, disappear and rules are bent. ! People can’t be controlled. This is open source. ! And there are crazy rebels out there…

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Solution!!

Slide 35

Slide 35 text

Lock the version numbers "dependencies": { "underscore": ”1.4.2” },

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Why? Bug fixes won’t propagate ! Features take longer to reach developers ! Tons of ”version bump” commits

Slide 41

Slide 41 text

Follow the semantics "dependencies": { "underscore": ”>= 1.4.2 < 2”, "foobar": ”>= 0.4.2 < 0.5”, },

Slide 42

Slide 42 text

Or, simpler "dependencies": { "underscore": ”^1.4.2”, "foobar": ”^0.4.2”, },

Slide 43

Slide 43 text

Building an app? Different story… Check in dependencies or Shrinkwrap

Slide 44

Slide 44 text

Keep up-to-date npmedge $ npmedge es6-promise has 1.0.0, but ^0.1.1 is specified jsontool has 7.0.2, but 7.0.1 is specified jscov has 0.6.14, but ^0.5.7 is specified chai-as-promised has 4.1.1, but ^3.3.1 is specified

Slide 45

Slide 45 text

So… Use semver when versioning your module ! Mind it when you depend on something ! Use shrinkwrap and npmedge

Slide 46

Slide 46 text

#3

Slide 47

Slide 47 text

#3 Test it

Slide 48

Slide 48 text

”I wrote some code, please maintain it” ! — Every pull request ever

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Strategy Node.js AND the browser(s) ! Use two different setups ! Use the same test

Slide 51

Slide 51 text

mocha

Slide 52

Slide 52 text

#4

Slide 53

Slide 53 text

#4 Continous Integration

Slide 54

Slide 54 text

.travis.yml

Slide 55

Slide 55 text

But the browsers…? Bucketful ! Chalcogen ! Sauce Labs

Slide 56

Slide 56 text

Sauce Labs

Slide 57

Slide 57 text

Makefile release

Slide 58

Slide 58 text

So: Test all browsers in CI - not manually ! Don’t publish by hand ! Travis + Sauce Labs = *drewl*

Slide 59

Slide 59 text

#5

Slide 60

Slide 60 text

#5 Test Coverage

Slide 61

Slide 61 text

Instrumentation Mocha recommends JSCoverage ! It does the job, but is a little messy ! I implemented a drop-replacement: jscov

Slide 62

Slide 62 text

jscov

Slide 63

Slide 63 text

Improve the reports? Terminal reports: mocha-term-cov-reporter ! Running with CI: coveralls ! !

Slide 64

Slide 64 text

reporting

Slide 65

Slide 65 text

(#6)

Slide 66

Slide 66 text

Time’s running out… makefile ! bower.js ! And more details…

Slide 67

Slide 67 text

mocha chai sinon selenium sauce labs browserify bucketful chalcogen jscov mocha-term-cov-reporter npmedge coveralls travis ci jsontool

Slide 68

Slide 68 text

Creating JS modules jakobm.com @jakobmattsson bit.ly/spotifyjs + -