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 installobjective 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 ~/.npmrcsave-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.jsonDirect 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 urlDirect 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 publishnpm.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 publishDirect 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 infoDirect 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 doexpectDirect 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 releaseversion from package.json
script will be executed on successtravis.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 commitnpm scripts, {"scripts": { "commit": "git-cz" } }commitizen via {config": { "commitizen": { "path": "cz-conventional-changelog" } } }Direct link to the video tutorial
source and testcommitizen to commit with conventional messageDirect link to the video tutorial
https://travis-ci.org/profile/<user-name>semantic-releasesemantic-release would
Direct link to the video tutorial
$ npm i -D ghooks to install and add it to package.json/devDependenciesghooks via the {"config": {"ghooks": { "hook-name": "command-to-execute" } } }Direct link to the video tutorial
$ npm i istanbulpackage.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/scriptgit hooks
{"config": {"ghooks": { "pre-commit": "npm test && npm run check-coverage" } } }Direct link to the video tutorial
$ npm i codecov.io -Dreport-coverage to report coverage to codecov.io.
{"scripts": { "report-coverage": "cat ./coverage/lcov.info | codecov" } }npm run report-coverage to travis/after_successcodecov.io/github/<user-name/organization-name>/<repo-name>Direct link to the video tutorial
[](link to the service)
[](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-squareDirect link to the video tutorial
$ npm i -D babel-cli to install and add it to package.json/devDependenciesbuild 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/devDependenciespackage.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] }' > .babelrcpackage.json - { "babel" : { "presets": ["es2015", "stage-2] } }$ npm run build will create the transpiled code in dist folderpackage.json/main to refer distnpm run build to travis.yml/scriptdist/ to .gitignoreDirect link to the video tutorial
npm i -D babel-register to install and add it to package.json/devDependenciesbabel-rgister as the compiler to mocha and update package.json/scripts.test
$ npm i -d nyc to install and add it to package.json/devDependenciescover
ghook and travis/script run npm run cover instead of npm testistanbul 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 .gitignoreDirect link to the video tutorial
branches to travis.ymlbranches
- only
- master
only for whitelisting and exclude for blacklistingbranch-name (master) could also be a regexDirect link to the video tutorial
$ npm i -D webpackwebpack.config.babel.js
libraryTarget: 'umd' to outputlibrary: <library-name> to outputdevtool: 'source-map' to main configloaders
$ npm i -D babel-loaderpackage.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/scriptreadme to point to https://unpkg.com/<package-name>@<version>/path/to/umd/file