Skip to content

Instantly share code, notes, and snippets.

@nh2
Created June 13, 2012 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nh2/2924336 to your computer and use it in GitHub Desktop.
Save nh2/2924336 to your computer and use it in GitHub Desktop.
Sharing code between node and browser
Sharing code between node and browser
=====================================
This is incredibly messy.
Using require.js + r.js + amdefine
----------------------------------
* Cannot use cs.js for "cs!module" coffee imports
-> Need to compile CS to JS with coffee --watch
-> Breaks due to inotify in node 0.6 (new inotify resources even if other files like .html are saved)
-> fixed in node 0.7
* No good place to put shared files in
* Idea:
common/
server/
common/ -> ../common/ (symlink)
client
common/ -> ../common/ (symlink)
* Doesn't work:
* In a file in common, require 'amdefine' doesn't work:
* node_modules is (and should be) in the server directory
* node follows the symlink and doesn't see node_modules in common
So require.js doesn't really help us with this.
Simple method
-------------
* Use `@modulename = module?.exports = { ... functions ... }`
* Does not allow "imports": shared modules cannot properly depend on other shared methods
* only with `if`s and making sure that they are included in the right order in the browser
Conclusion
----------
Currently there is no really nice way.
Require.js would be it but it doesn't work and is complicated.
Treat it as an 'incident' that both client and server run on Javascript, so don't expect that sharing is easy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment