• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JavaScript library template.

JavaScript Library Template

This is meant to serve as a foundation to build JavaScript libraries on top of. Exactly how a JavaScript library is structured isn't standardized, so this project aims to define an opinionated pattern for organizing, building, and testing code. If want to write a JavaScript library and need a starting point, this is a good option.

Here's a video explaining the ideas and motivation behind lib-tmpl.

Don't be afraid to change any code or conventions in this project, it's simply an educational tool meant to give direction when writing JavaScript libraries.

I created this skeleton project out of what I learned with my projects Rekapi and Shifty. It is also heavily inspired by other projects such as jQuery. The module pattern and AMD compatibility code is an abstraction of the work of Franck Lecollinet.

Setting it up

This project uses NodeJS and Grunt for the build process. Assuming you have Node and npm set up (npm is installed when Node in installed), you can just do this at the CLI in the lib-tmpl directory:

$: npm install

What this project gives you

A scalable, unobtrusive pattern

There are a lot of small things you can do maximize compatibility and minimize headaches. This template has a lot of this baked in. Only one global symbol is exposed (none are exposed if it is loaded as an AMD module), a clear convention for managing private and public APIs is provided, and JavaScript Strict Mode is enabled by default.

Module templates

As your library grows, it helps to separate code into files. This serves to keep large amounts of code manageable, and it also makes collaboration easier by mitigating merge conflicts. A lib-tmpl library must have one core module, and any number of additional modules. Modules should be organized by the task they perform. For instance, if you have a set of methods that do DOM manipulation, those should be isolated into a single module.

A directory structure

Each directory has a README that explains what its purpose is.

A build process (via Grunt)

Compiled code loads and runs faster than uncompiled code. lib-tmpl provides concatenation and minification via Grunt (see Gruntfile.js). Feel free to modify this configuration to suit your needs. The default configuration creates the production-ready dist/library.min.js and debug-friendly dist/library.js files. Building this project via the CLI is really easy:

$: grunt build

Conditional compilation for customizing binaries

UglifyJS has a great feature: Conditional compilation. Conditional compilation allows you to mark sections of code that should not be included in the compiled binary. This has a number of benefits, not least of which is exposing private functions and variables only during development and testing. This template provides sections to place code that get removed during compilation. The flag DEBUG is included and set up out of the box. See the notes in src/library.const.js for more info on how to add more.

Testing bootstrap

A good library is thoroughly unit tested. There are number of ways to unit test your code, and you are free to use whatever framework or approach you prefer. lib-tmpl provides basic test skeletons for API and AMD tests via QUnit.

To run the tests, you can either open each of the test/qunit.*.html pages in your browser, or run them all in the CLI (requires PhantomJS to be installed). The qunit Grunt task automates this for you:

$: grunt qunit

JSHint boilerplate

JSHint is a tool that helps ensure code quality. lib-tmpl provides some default settings in .jshintrc - feel free to customize it. To lint all of the source files, run this:

$: grunt jshint

AMD compatibility

AMD (Asynchronous Module Definition) defines a way by which to load code. You don't have to use it, but it's a great development tool and helps prevent pollution of the global scope. This template is compatible with AMD loaders, such as RequireJS (which is included by default). You can also safely load the code with a standard <script> element with no side effects.

Building on top of this template

If you choose to use this template to build your libraries on top of, you should first change the name of the library. It is called "Library" by default. You'll need to change the name of the constructor (in src/library.core.js) and all references to that constructor in the code and comments. It's also a good idea to change the file names to reference the actual name of your library, rather than the generic "library." In other words, you should change src/library.core.js to something like src/my-awesome-tool.core.js.

The naming convention for the files in src/ is significant:

<LIBRARY_NAME>.<MODULE_NAME>.js

When you change the name of the library and the prefix of the files in src/, you will also need to update the value of LIBRARY_NAME in Guntfile.js. As you add module files in src/, you will also need to add them to MODULE_LIST, also in Gruntfile.js. The areas you need to modify (LIBRARY_NAME and MODULE_LIST) are called out in the comments.

Don't forget to update package.json (for npm) and component.json (for Bower) to reflect you and your project!

More on adding module files

You can add modules to this library. You can think of src/library.module.js as a generic module template. Simply copy that file and modify as needed. To initialize the module, add its init wrapper function to src/library.init.js (there's a note in the comments where to do this).

Roadmap

Admittedly, building on top of and extending lib-tmpl is somewhat labor-intensive. Eventually I'd like to write generators for creating and linking modules automatically.

More Repositories

1

shifty

The fastest TypeScript animation engine on the web
TypeScript
1,509
star
2

chitchatter

Secure peer-to-peer chat that is serverless, decentralized, and ephemeral
TypeScript
1,292
star
3

rekapi

A keyframe animation library for JavaScript
JavaScript
641
star
4

stylie

A graphical CSS3 animation tool
JavaScript
526
star
5

keydrown

A JavaScript key state handler for web apps
JavaScript
206
star
6

mantra

A professional web animation tool for everyone
JavaScript
181
star
7

farmhand

A resource management game that puts a farm in your hand
JavaScript
100
star
8

kapi

A keyframe API for the HTML5 Canvas.
JavaScript
62
star
9

dotfiles

This is what I use to get things done!
Vim Script
57
star
10

dragon

Another fn.draggable plugin for jQuery.
JavaScript
49
star
11

merkaba

An SVG editor component written in React
JavaScript
21
star
12

rekapi-timeline

A visual tool for modifying Rekapi animations
JavaScript
21
star
13

secure-file-transfer

A library to encrypt and transfer files P2P in the browser
TypeScript
19
star
14

vim-docker-env

A bare Vim environment, plus Pathogen, powered by Docker
Vim Script
15
star
15

rekapi-controls

Graphical controls for Rekapi animations.
JavaScript
10
star
16

props-editor

A UI for modifying React component props
JavaScript
8
star
17

misfit

A CLI UI for working with npm
JavaScript
8
star
18

Infinitely-Draggable-Content

Drag stuff around! For forever and ever!
JavaScript
8
star
19

js-project-starter

My personal starting point for JavaScript projects
JavaScript
6
star
20

Cachey

A caching plugin for jQuery
JavaScript
6
star
21

requirejs-lazy-load-example

An example of a lazy-loaded web app powered by RequireJS with a build process
JavaScript
6
star
22

require-and-backbone-views

An example of using Require.js and Backbone Views.
JavaScript
6
star
23

cubelet

A jQuery widget for graphically rotating a cube and working with its coordinates
JavaScript
5
star
24

tools

Some handy development tools
Vim Script
5
star
25

bezierizer

A graphical widget for defining a Bezier curve
JavaScript
4
star
26

storytime-with-github

Tell a story!
3
star
27

spinneroo

Take the web for a spin!
JavaScript
3
star
28

keyhandler_demo

A demonstration for a simple key handler in JavaScript
JavaScript
3
star
29

node-cli-boilerplate

Minimal, practical boilerplate for writing Node-powered CLI tools
JavaScript
3
star
30

rekapi-export-test

A test harness for experimenting with Rekapi exportTimeline data
JavaScript
3
star
31

tweetree

A fun project that lets you view random Twitter tweets in a visual tree.
JavaScript
3
star
32

jambi

A game development platform
2
star
33

transfornimate

A CSS transform animation tool
JavaScript
2
star
34

jumpjump

A game prototype for an idea I have.
JavaScript
2
star
35

fun-animal-names

Hash strings into fun animal names
TypeScript
2
star
36

jeremyckahn.com

My personal site.
CSS
2
star
37

wipe

An old jQuery plugin I made to make blur/wipe transitions
JavaScript
2
star
38

dumbdiff

It's a dumb, but fast and simple string differ for JavaScript.
JavaScript
2
star
39

hackday

We are hacking.
2
star
40

shifty-actors

A bridge library for Tweeny and Kapi actors.
JavaScript
2
star
41

jquery-canvas-kit

An old project, just throwing it on here because why not.
JavaScript
2
star
42

doodles

Sometimes I make art with code for fun.
JavaScript
2
star
43

farmhand-shuffle

A card game for farmers
TypeScript
2
star
44

jeremyckahn

It's a-me! jeremyckahn!
1
star
45

wrecked

An HTML5 game about rectangles. Hopefully it will be fun someday!
JavaScript
1
star
46

generator-web-project

This is the generator I use to start web projects.
JavaScript
1
star
47

trystero-node-sandbox

Sandbox repo for https://github.com/dmotz/trystero/issues/24
TypeScript
1
star
48

hackapi

Rekapi Art!
JavaScript
1
star
49

rekapi-benchmark

A simple performance benchmark for Rekapi, Underscore and Lo-Dash
JavaScript
1
star
50

rekapi-getting-started-demo

A runnable set of files for a Rekapi "Hello World"
1
star
51

vertigo

A jQuery plugin that makes a 3D UI for presenting content
JavaScript
1
star
52

petriEvolution

An old Processing project of mine.
Java
1
star
53

suggest-o-tron

An app that makes event suggestions for you
Ruby
1
star
54

rekapi-ios

An iOS-inspired Rekapi animation
JavaScript
1
star
55

3d-cards

A UI experiment with cards in 3D space
JavaScript
1
star
56

workflow-dispatch-test

A repo for experimenting with GitHub Actions
1
star
57

react-starter

My starting point for React apps
JavaScript
1
star
58

pwa-demo

TypeScript
1
star
59

jck-library-extensions

A set of extensions for open source libraries
JavaScript
1
star
60

iframe-test

HTML
1
star
61

jquerycanvaskit

Automatically exported from code.google.com/p/jquerycanvaskit
JavaScript
1
star