Search code examples
node.jstypescriptdurandalamdcommonjs

Is it possible to mix AMD and CommonJS modules within same Typescript project


I'm trying to integrate Durandal with node.js server using Typescript for defining modules both on server and client side.

The problem I've encountered is that Durandal is strongly dependent on RequireJS and AMD style of defining modules that I would like not to introduce on the server side, and since it uses RequireJS I don't have any chance to run CommonJS-ish modules on the client (default for node.js).

The final nail in the coffin is that I found no way of defining which files should be compiled as AMD modules and which one as CommonJS by tsc - it seems like a simplest solution.

I don't think separating client part and server part is an option, since a lot of code will be common to both parts.

So, my question is threefold:

  • is there a way to mix AMD and CommonJS modules in the same Typescript project (preferably using NodejsTools)

  • if not, is there a way to force Durandal to work with CommonJS files to load views/viewmodels and so on

  • if none of this is possible is it possible (and wise) to use AMD modules on node.js server

Any ideas are highly appreciated


Solution

  • is there a way to mix AMD and CommonJS modules in the same Typescript project (preferably using NodejsTools)

    Yes. Use grunt-ts. See https://github.com/basarat/demo-fullstack/tree/master/src specifically the gruntfile common files : https://github.com/basarat/demo-fullstack/blob/master/src/Gruntfile.js#L4-L6 commonjs : https://github.com/basarat/demo-fullstack/blob/master/src/Gruntfile.js#L26 amd : https://github.com/basarat/demo-fullstack/blob/master/src/Gruntfile.js#L37