How to Write an Open Source JavaScript Library
The purpose of this document is to serve as a reference for:
How to Write an Open Source JavaScript Library course by Kent C. Dodds
Watch the series at egghead.io, if you haven't.
Direct link to the video tutorial
micro libraries
npm install
objective of the course / learn to
Direct link to the video tutorial
Direct link to the video tutorial
$ npm set init-author-name "Sarbbottam Bandyopadhyay"
$ npm set init-author-url "https://sarbbottam.github.io/"
$ npm set init-author-email "sarbbottam@gmail.com"
$ npm set init-license "MIT"
npm init
$ cat ~/.npmrc
save-exact
property, it tells npm
to use the exact version of the packages, rather than a version range, while saving dependency to package.json.$ npm add-user
, to add your account
auth token
and add it to ~/.npmrc
$ npm init
will prompt for desired information and create package.json
at the end$ npm init --yes
will create a package.json
, with the defaults, with out promptingDirect link to the video tutorial
main
file-S
or --save
to save it as dependency
at package.json
-D
or --save-dev
to save it as devDependency
at package.json
Direct link to the video tutorial
.gitignore
at the root
of the project, to list all the ignored files and directories$ git add <file-name>
to stage the changes
$ git add --all
to stage all the changes$ git commit
to commit the changes$ git push origin <repo-name>
to push the changes to GitHub(origin
)
$ git remote -v
will display all the available remote
and their corresponding url
Direct link to the video tutorial
$ npm add-user
, if you have not alreadypackage.json/files
to whitelist the set of files to be published.npmignore
file to ignore files/directories, that might fall under from whitelist$ npm version <patch|minor|major>
, if you have already published to npm
patch
for bug fixminor
for new featuremajor
for breaking changesnpm pack
or npm link
to validate the module to be publish$ npm publish
npm.im/<package-name>
Direct link to the video tutorial
tag
in git, points to a specific commit$ git tag <version>
<version>
released to npm$ git push --tags
releases
tabDirect link to the video tutorial
package.json/version
$ npm version <patch|minor|major>
patch
for bug fixminor
for new featuremajor
for breaking changescommit
the changestag
the commit.$ npm publish
Direct link to the video tutorial
package.json
-beta.0
to the end of the version$ git checkout -b <branch-name>
for the beta version$ git tag <packge-version-beta.0>
$ git push origin <branch-name>
$ git push --tags
$ npm publish --tag beta
$ npm info
Direct link to the video tutorial
$ npm i -D mocha chai
, to install and add them to devDependencies
create a test file
require(chai)
require
the file to be testedvar expect = require('chai').expect;
var functionality = required('./path/to/index.js');
describe('functionality', function() {
it('should validate the functionality', function() {
expect(true).to.be.true;
});
});
update package.json/script.test
{"scripts": { "test": "mocha path/to/test/file" } }
-w
to watch for changes$ npm test
to run test
Direct link to the video tutorial
global
describe
function and it
function to describe the tests and what they should doexpect
Direct link to the video tutorial
semantic-release
automates the releasing and frees you from redundant manual steps.$ npm i -g semantic-release-cli
to install semantic-release-cli
globally$ semantic-release setup
travis.yml
if the CI system chosen, is travis.package.json/script
w.r.t release
version
from package.json
script
will be executed on success
travis.yml
to run tests prior releasingpackage.json/version
to 0.0.0-sematically-released
, to avoid npm
warningDirect link to the video tutorial
$ npm i -D commitizen cz-conventional-changelog
commitizen
globally or add ./node_modules/bin
to system PATH
to use git cz
instead of git commit
npm scripts
, {"scripts": { "commit": "git-cz" } }
commitizen
via {config": { "commitizen": { "path": "cz-conventional-changelog" } } }
Direct link to the video tutorial
source
and test
commitizen
to commit with conventional messageDirect link to the video tutorial
https://travis-ci.org/profile/<user-name>
semantic-release
semantic-release
would
Direct link to the video tutorial
$ npm i -D ghooks
to install and add it to package.json/devDependencies
ghooks
via the {"config": {"ghooks": { "hook-name": "command-to-execute" } } }
Direct link to the video tutorial
$ npm i istanbul
package.json/script.test
{"scripts": { "test": "istanbul cover -x test-file-name-pattern _mocha -- path/to/test/file -R spec" } }
$ npm test
will run the test and generate coverage information at coverage/
foldercoverage
to .gitignore
fileDirect link to the video tutorial
check-coverage
to verify coverage for statements, branches, functions, and lines.
{"scripts": { "check-coverage": "istanbul check-coverage --statement 100 --branches 100 --function 100 --lines 100" } }
npm run check-coverage
to travis/script
git hooks
{"config": {"ghooks": { "pre-commit": "npm test && npm run check-coverage" } } }
Direct link to the video tutorial
$ npm i codecov.io -D
report-coverage
to report coverage to codecov.io.
{"scripts": { "report-coverage": "cat ./coverage/lcov.info | codecov" } }
npm run report-coverage
to travis/after_success
codecov.io/github/<user-name/organization-name>/<repo-name>
Direct link to the video tutorial
[![alt text](badge-url)](link to the service)
[![build](https://img.shields.io/travis/<user-name>/<repo-name>.svg)](https://travis-ci.org/<user-name/organization-name>/<repo-name>)
style
query param to customize the style of the badge
https://img.shields.io/...svg?style=flat-square
Direct link to the video tutorial
$ npm i -D babel-cli
to install and add it to package.json/devDependencies
build
to transpile ES6/ES2015 code to ES5
{"scripts": { "build": "babel --out-dir dist src" } }
-d
instead of --out-dir
--copy-files
to copy dependencies
{"scripts": { "build": "babel --copy-files --out-dir dist src" } }
prebuild
to clean the dist
directory prior building
{"scripts": { "prebuild": "rimraf dist" } }
$ npm i -D rimraf
to install and add it to package.json/devDependencies
package.json/devDependencies
$ npm i -D babel-preset-es2015
$ npm i -D babel-preset-stage-2
.babelrc
file or in package.json/babel
.babelrc
- $ echo '{ "presets": ["es2015", "stage-2] }' > .babelrc
package.json
- { "babel" : { "presets": ["es2015", "stage-2] } }
$ npm run build
will create the transpiled code in dist
folderpackage.json/main
to refer dist
npm run build
to travis.yml/script
dist/
to .gitignore
Direct link to the video tutorial
npm i -D babel-register
to install and add it to package.json/devDependencies
babel-rgister
as the compiler to mocha
and update package.json/scripts.test
$ npm i -d nyc
to install and add it to package.json/devDependencies
cover
ghook
and travis/script
run npm run cover
instead of npm test
istanbul
with nyc
at package.json/scripts.check-coverage
{"scripts": { "check-coverage": "nyc check-coverage --statement 100 --branches 100 --function 100 --lines 100" } }
.nyc_output
to .gitignore
Direct link to the video tutorial
branches
to travis.yml
branches
- only
- master
only
for whitelisting and exclude
for blacklistingbranch-name
(master
) could also be a regex
Direct link to the video tutorial
$ npm i -D webpack
webpack.config.babel.js
libraryTarget: 'umd'
to output
library: <library-name>
to output
devtool: 'source-map'
to main configloaders
$ npm i -D babel-loader
package.json/script.build
to package.json/script.build:main
"build:umd": "webpack --output-filename <output-file-name>.umd.js"
to package.json/script
"build:umd:min": "webpack --output-filename <output-file-name>.umd.min.js" -p
to package.json/script
-p
for production build, minify the code"build": "build:main && build:umd && build:umd:min"
to package.json/script
$ npm i -D npm-run-all
"build": "npm-run-all build:*"
to package.json/script
readme
to point to https://unpkg.com/<package-name>@<version>/path/to/umd/file