3 ms·
Show HN: CommonJS module implementation for the browser
I built a really simple CommonJS module implementation for defining/requiring javascript modules in a browser environment that doesn't load scripts asynchronously (or at all, for that matter).
https://github.com/bgrohman/cardboard.js
I originally did this for fun and to learn about CommonJS, but I'm curious to see if anyone else thinks this might be useful.
- firefoxman1 15y agoSo are you saying the module loading is not asynchronous?
- bgrohman 15y agoWell, there isn't actually any module loading. The define function lets you define new modules with dependencies. The require function returns a reference to a module or throws an exception if it hasn't been loaded. The idea is that you get to use CommonJS-style module definition in conjunction with old-school script loading using script tags. This could be used during a transition of a large project to something more robust like RequireJS. Or it could be used if your application has to support high-latency connections where you need to minimize the number of HTTP requests.
- firefoxman1 15y agoAhh I see. That's really cool! No bloat and easy to use. I'll have to consider this for my next project. Honestly I think CommonJS gets too much flack. It's bounds better than ECMAScript Harmony's module draft.
- bgrohman 15y agoThanks! Yeah, ECMAScript Harmony's module proposal uses new syntax, which basically means most developers won't be able to use it for a while. Also, I'm not sure if Harmony supports loading/using multiple modules defined in a single file.