Reusing require.js modules in Node.js with AMDrequire
AMDrequire is a npm package that makes Node.js able to load require.js modules like if they were native Node ones, as well as continue to load its own modules as usual, allowing to reuse browser code in the server.
(
14 comments
| 13 points
| on HN
)
What is the right way to use Browserify transforms server-side?
------------------
Because Browerify assumes you use Node's own require() server-side while RequireJS (and now also AMDRequire) have their own module to provide an enhanced require function. The closest I could come up with (besides giving up and precompiling to a single file before calling Node) is using the deprecated "require.extensions" to preprocess the file, similar to requirejs loaders, e.g. for handlebars you could add in your Node start:
Then require('./something.handlebars') in modules you share client and server-side gives you the already-compiled template regardless if you're running in Node or in the browser (if you also use the handlebar transforms for Browserify).I'm sure I'm missing something very obvious here.
The compilation step that browserify uses is little more than concatenate + a little bit of sugar. Practically speaking why does this matter?
I'd much prefer to deliver less code to the client if possible, so I don't see the rationale for adding a dependency on a dependency manager.
I used RequireJS for a while and it isn't bad, but Browserify has a lot of advantages like compatibility with Node.js and npm (while the only disadvantage I see is that the compile step is non-optional).
> I'd much prefer to deliver less code to the client if possible
requirejs shim isn't signifcantly larger than what browserify adds, it's only 200 lines.
I'd be very interested in seeing a set of benchmarks comparing browserify to r.js. I'd be very surprised if the difference was meaningful, since they do basically the same thing. In fact, doesn't r.js support CommonJS-style requires?
Update: My other love for requirejs is that you don't need to use relative paths when requiring a module. Once you have a relative path, you can't change the location of either dependant or dependency. The require config lets you separate what you call the module from the location of the file.
https://www.npmjs.org/package/requirejs
https://gist.github.com/spenceralger/946ddc6c55d997d4a028
I guess I can see the value in this, if you're retrofitting a new module in the AMD style to an existing node application which does not use that style.