5 ms·
Since I compile all my CoffeeScripts into JavaScripts at node program start, code sharing is a breeze. For each file, I make a client version and a server versi
by dualogy 15y ago
Since I compile all my CoffeeScripts into JavaScripts at node program start, code sharing is a breeze. For each file, I make a client version and a server version and use simple preprocessor lines:
#if client
#if server
#endif
for the pieces that only apply to the client side or the server side, with shared pieces staying outside these "tags". I won't go back. This is especially cool as I have a templating system that can run on the server or on the client. So for JS-enabled browsers, all HTML template rendering is done on the client by building all HTML tags out of very small simple JSON structs. For non-JS clients (spiders, bots, paranoid non-JS enterprise legacy browsers) the "pages" get fully rendered old-school by the server-side -- both sharing the same rather simple custom-made JSON-driven templating functions and producing essentially almost the same HTML output. Of course there are some added complexities going on with things like scripted onclicks, but you get the general idea. (This is mostly done for indexing spiders and human non-JS clients just simply have to put up with a limited, degraded, basic experience.)
- Andi 15y agoI do it similarly. And it's the way to go ...