News for hackers. Stuff that matters.
Re:Reusing require.js modules in Node.js with AMDrequire by wildpeaks on Wednesday May 21, 2014 @01:45PM (#7779613)
Interesting, it might solve my dilemna regarding RequireJS vs Browserify given the npm requirejs has one troublesome issue with the paths aliases config getting erased (& can't be restored either) once .optimize has been called to make an Almond build. That's why I switched to Browserify in most recent projects, however I have one issue with it and it feels I'm missing something obvious:

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:

  var fs = require('fs');
  var Handlebars = require('handlebars');
  require.extensions['.handlebars'] = function (module, filename){
    var code = fs.readFileSync(filename, 'utf8');
    module.exports = Handlebars.compile(code);
  };

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.

Re:Reusing require.js modules in Node.js with AMDrequire by couchand on Wednesday May 21, 2014 @10:21AM (#7778367)
I disagree with the assertion that the browserify transformation implies it's "not the same code". If you're (following best practice and) concatenating and minifying your assets, is that not the same thing?

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.

Re:Reusing require.js modules in Node.js with AMDrequire by jgillich on Wednesday May 21, 2014 @11:10AM (#7778629)
Exactly. RequireJS promotes having lots of files, but for production, everyone runs their code through r.js anyway.

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).

Re:Reusing require.js modules in Node.js with AMDrequire by chr1 on Wednesday May 21, 2014 @11:41AM (#7778828)
Browserify compile step on a large project is painfully slow. Using https://www.npmjs.org/package/amd-loader with commonjs style modules http://requirejs.org/docs/commonjs.html was much easier to use.

> 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.

Re:Reusing require.js modules in Node.js with AMDrequire by couchand on Wednesday May 21, 2014 @11:54AM (#7778893)
Browserify compile step on a large project is painfully slow.

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?

Re:Reusing require.js modules in Node.js with AMDrequire by chr1 on Wednesday May 21, 2014 @12:02PM (#7778941)
The speed of r.js and browserify compile is about the same, indeed. The difference is that with browserify i had to wait for it on every reload, and r.js runs only for release, on buildbot.
Re:Reusing require.js modules in Node.js with AMDrequire by greggman on Wednesday May 21, 2014 @11:01AM (#7778591)
I don't know browserify well but I'd assume having a compile step is the part that sucks. If you can use require.js style until shipping time you can debug the actual code directly instead of the compiled code. You see each module in the debugger as its original file.
Re:Reusing require.js modules in Node.js with AMDrequire by jgillich on Wednesday May 21, 2014 @11:12AM (#7778640)
Source maps solve that issue, even though browser support isn't perfect yet.
Re:Reusing require.js modules in Node.js with AMDrequire by alistairjcbrown on Wednesday May 21, 2014 @11:31AM (#7778769)
+1 - this is why I love requirejs - lazy load modules in development, compile for QA and deployment.

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.

Re:Reusing require.js modules in Node.js with AMDrequire by danabramov on Wednesday May 21, 2014 @12:51PM (#7779257)
I tried porting a project from Require to Browserify and I gave up because of relative paths issue.
Re:Reusing require.js modules in Node.js with AMDrequire by alistairjcbrown on Wednesday May 21, 2014 @11:28AM (#7778744)
How does this differ from the requirejs module for Node?

https://www.npmjs.org/package/requirejs

Re:Reusing require.js modules in Node.js with AMDrequire by spencera on Wednesday May 21, 2014 @01:27PM (#7779475)
Looks like it offers a subset of the features by allowing you to use the same require() function for both amd and commonjs modules.

https://gist.github.com/spenceralger/946ddc6c55d997d4a028

Re:Reusing require.js modules in Node.js with AMDrequire by alistairjcbrown on Thursday May 22, 2014 @07:22AM (#7783229)
Ah, thank you - I misunderstood the crucial piece "load AMD modules like ... they were native Node modules".

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.

Re:Reusing require.js modules in Node.js with AMDrequire by wildpeaks on Friday May 23, 2014 @03:05PM (#7790817)
The requirejs module has both the server-side loading modules and the optimizer that compiles to a single file whereas this new one only does the server-side loading of modules as far as I can tell.