Can I write a CoffeeScript lib the same way for the server and client? - node.js

I'm trying to write a modular library in CoffeeScript. I want to write the code once, and use it in the same way on the server (running Node in this case) and on the browser. How can I do this?
My code structure is like this...
src/a.coffee
src/b.coffee
src/c.coffee
And my dependencies are like this...
a depends on b, c depends on a and b.
I've tried Browserify and requirejs, but I couldn't quite get there with it. I've also looked into Traceur but that's no use since I'm using CoffeeScript. I'm really at a loss, the only thing I can think of, is bundling it all up in one file and doing it traditionally as explained in this blog... https://alicoding.com/write-javascript-modules-that-works-both-in-nodejs-and-browser-with-requirejs/. I really don't want to do that, I'd probably just write it in Dart sooner than I would bundle it up like that. CoffeeScript is really letting me down with this particular issue.

I forgot to resolve this. I ended up using https://github.com/jrburke/amdefine.

Related

Recommendations for preventing RequireJS from interfering with legacy code

I'm developing a 'widget', for lack of a better word, that will be loaded in many different sites that I don't control.
We're using RequireJS to keep things easy, but this has the side effect of breaking A LOT of sites that don't already use/support it.
The be clear - we don't control the sites, and the cause is that many of the sites existing libraries are loading into RequireJS instead of globally, and the code on these sites expects them to be loaded globally.
The only practical solution I can think of so far is to rename RequireJS' require() and define() (and perhaps others), then edit every library we rely on (using sed, of course) to load using the 'new' functions.
Has anyone else dealt with this? Is there a better method I'm missing?
Michael
For anyone who stumbles upon this, here's what I ended up doing...
There isn't a good solution for this at the moment as:
1) All libraries that load into RequireJS need define() to exist in their scope at execution time
and
2) There isn't any mechanism for asynchronously loading scripts that would allow define to be defined (pun not intended) and undefined before/after execution, aside from eval(), and that's just not a good option.
This means that, it's not really possible to have some type of scoped RequireJS without it possibly interfering with other scripts on-page that CAN use RequireJS, but are intended to load globally on that particular site.
So... here's the hacky solution I did...
Instead of loading the JS libraries myself, I bundled them on the fly, along with RequireJS, and wrapped in an immediately executing function.
The reason for doing this on-the-fly, is that some site specific data is necessary for the program to function, and it saves an HTTP request to obtain it (at the expense of a larger file download).
This allowed me to:
1) Use libraries that need to run under RequireJS (or similar) to work property
2) Avoid cluttering up the global namespace for stuff like jQuery
3) Avoid editing library source (eg. changing define() to my_special_define() or similar)
I hope this helps someone if they're trying to do the same thing as me :)

Server-side node.js templating and merging for text files

I am trying to see if there is a library that will allow me to do merging of a JSON object with a template (txt file) on the server side. Ideally, I would like it to be able to handle some conditional statements (e.g. if, greater than, equals etc.) and looping (e.g. for).
I know there are binding libraries (e.g. angularjs), and one option might be to hack it to extract the code required to do this. Alternatively, I could create my own solution, but would rather not re-invent the wheel.
I am new to Nodejs, so I'm thinking this seems like a problem that might have been solved already.
Any ideas?
All good.
Ended up going with doT.js. Great library for what I'm doing.

Should Node.js module developers use Underscore.js?

I'm working on my 1st Node.js module, and having to do common utility stuff like check types, looping etc.
The native JS for some of this stuff is pretty ugly. Underscore.js makes it more readable and adds a lot of new features too. But if I don't need the new stuff, should I use Underscore or just do it the hard way?
Thanks!
In node.is you can rely on having some ES5 stuff, array iteration functions and utility functions like isArray. In my node modules I never used underscore and had, due to array iteration functions like map, forEach never the need to use underscore or lodash.
I would not avoid a underscore dependency in case I'd really need it. The node.js platform relies on small modules depending on a couple of small modules itself. So why not depend on underscore.
I see no reason to avoid using a module that makes your life easier. And, it just so happens, that underscore.js is the most depended upon package in the npm registry (as of the time of this answer, according to https://npmjs.org/). So yea, no reason to avoid it.
I've never used underscore nor async on real projects. Once you know how to code good javascript it's not necessary to use any helper library. For example, functions that should execute in serie and are asynchronous it's pretty easy to do with a simple "recursive while loop", you don't need to load any library.
But at the end this is a personal preference. Use external libraries if you feel comfortable with them.
Advice: Don't look at the github starts or npm installations to decide which module to use. Being popular doesn't mean being good. I've tried a lot of popular modules and about a 40% of them are just bad/bugged/not really useful. There are a lot of modules that are not popular that are really good. Being popular helps to take a decision but you should not install and use a module just because it's popular.
Underscore does the right thing, which is check for all the native es5 methods first, meaning you won't have much in the way of performance loss on native methods getting replaced with slower non-native versions that basically do the same thing (code here):
var
nativeForEach = ArrayProto.forEach,
nativeMap = ArrayProto.map,
nativeReduce = ArrayProto.reduce,
nativeReduceRight = ArrayProto.reduceRight,
nativeFilter = ArrayProto.filter,
nativeEvery = ArrayProto.every,
nativeSome = ArrayProto.some,
nativeIndexOf = ArrayProto.indexOf,
nativeLastIndexOf = ArrayProto.lastIndexOf,
nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind;
Note: prototypes assigned to "Proto" vars earlier.
That said, I'm pretty sure V8 has most if not all of these. Being of client-side dev origins I'd be delighted simply to use the raw naked thing without having to think about how or what library is best for dragging IE kicking and screaming out of the stone age this time, providing the built-in methods aren't as ugly as the DOM API and I would say these aren't.
If underscore does more for you than the above then by all means use it. If it doesn't, I'd consider it a waste of space. All it really does on the browser is give you fallback methods for the older browsers which aren't a going concern in Node. It's light though. I wouldn't object either way if you were on my team and didn't want to write your own versions of something uniquely handled by underscore but would prefer the direct native method names/args, etc. in my own code on the principle of disliking dependencies anywhere I don't need them.
I use underscore in modules that are shared with the browser, not to depend on ES5. Also Underscore has quite a few very useful methods that are not available in ES5, so it would make sense to read their manual page.

SPA using BundleConfig and Require.js

I would appreciate any reasonable explanation of the use of BundleConfig and Require.js together in a SPA. How do you choose which scripts should be loaded by BundleConfig when starting the app? Which scripts should NOT be called in BundleConfig and selectively loaded via AMD? Once loaded by BundleConfig, do they have to be called again in require.js' DEFINE statement?
Search as I might, I simply can't find a simple explanation for all this.
Thanks!
BundleConfig will help you get all the script sin the bundle to the client together, in 1 trip. This takes the burden or job off of require.js for that. I often do this for 3rd party scripts, since it is highly likely i need them all client side right away anyway.
Then I can choose to either use require.js to load my scripts as needed (thus AMD) or bundle them too, up front.
Require does not just load things asyncly, though. It also handles the dependency resolution. To me, this is the more important part. It makes sure before I use module A that if it depends on B, which depends on C, which depends on D is resolved first. So they get loaded and run in the proper order.
So whether you use bundling or not, the require.js dependency aspects as super valuable.

Using the uinput kernel module in Lisp/SBCL

is there currently any way to use the uinput kernel module in SBCL? I have not been able to find any library myself (except some Japanese[?] guy's github: https://github.com/quek/info.read-eval-print.cl-mayu/blob/master/mayu.lisp ) so I was wondering if anyone knew anything about that.
Am I supposed to use some kind of SBCL-specific package? Am I supposed to generate my own bindings for this using something like CFFI?
I think it's safe to say there is no finished library you can use. However, that github repo seems to be a good place to look for ideas for making your own.

Resources