Creating a Library

25 views
Skip to first unread message

Secretmapper

unread,
Mar 29, 2015, 9:33:17 PM3/29/15
to nod...@googlegroups.com
I'm on the way of creating my first very own javascript library (a small client side library, say a stereotypical jquery library (but standalone) like tooltips)

However, I'm curios us to how developers normally create like this (and how it's tested).

For example, how can I create the library and be able to test it out easily? Should I act it out as a generic webapp (with gulp and tasks like livereload?) and then just copy paste the relevant files when isolating the library?

Aria Stewart

unread,
Mar 29, 2015, 10:32:45 PM3/29/15
to nod...@googlegroups.com
I'd generally go "as small as possible".

If you're doing DOM things, that means you're going to need to test against a DOM implementation -- a browser, even headless, like phantom, or a non-browser DOM like JSDOM. I'd generally make a package (especially to publish on the npm registry) like so:

package.json
{
"name": "mylibrary",
....
"main": "mylibrary.js",
"devDependencies": {
"jsdom": "^1",
"tape": "^2"
}
"scripts": { "test": "tape test.js" }
}

mylibrary.js
do things with the DOM

test.js
var dom = require('jsdom');
var test = require('tape');
...
set up dom here, loading your mylibrary.js into it -- maybe you can require it, if you've made a commonjs library, maybe you have to tell jsdom to load it like a script, or load and eval it yourself

test('test my library', function (t) {
t.ok(dom.stuff.mylibrary.whatever);
t.end();
})


Super minimal. I only add build tools when needed, and if libraries are small, you don't need very much.

Aria
Reply all
Reply to author
Forward
0 new messages