Domanda

I'm a Node n00b starting a couple web app projects using Express, and I've got some common client-side libraries I'd like to share between the two projects. This seems like a very common problem, so there must be several solutions available already.

I come from a java background, and in java, I'd create a separate "common" project and "overlay" common WAR over my project during packaging. This would also allow for r.js optimization during the build process.

My best guess in Node is that I need to create a private NPM module, and map those common files into express via a use() middleware plugin. Is that right?

How, then, can I package both my common and project specific javascript into a minified file using r.js?

Or is source control the answer? Checking out my "common" repository inside each project?

Any help would be most appreciated. Thanks.

È stato utile?

Soluzione

This seems like a very common problem, so there must be several solutions available already.

Good news: Yes, this is a common problem. Yes, there are several "solutions".

Bad News: All of the "solutions" are at least partially terrible.

Here's my advice:

1) Individual .js files should be coded as CommonJS modules

2) Groups of related .js files should be made into npm packages

3A) Use them in node via the regular node.js/CommonJS require function and use browserify to use them in the browser

3B) OR use a built tool like grunt to wrap commonjs into AMD format for use with requireJS in the browser

3C) OR consider something like component.io components

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top