the most convinient way to use sea.js in your node.js project
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
index.js
package.json
readme.mkd

readme.mkd

Freighter

Freighter is not and will never be another JavaScript loader and module dependency solution, because Sea.js just did a great job on this. Sea.js is the best script loader I've ever seen and used. I think you should try it, too.

Unfortunately, if you can't read Chinese, you will find it's difficult to use Sea.js because It don't even has a English document! Therefore Sea.js is barely used outside of China despite how popular it is in Chinese web development community.

So here I am. I'll write a connect middleware to show you my best practice of using Sea.js in node.js project and I'll talk about it on JSConfCN 2013, the 京JS conf which will be hold in Beijing this summer.

Sea.js In Short

Sea.js is a script loader implemented CMD ... Well, I'll add this section in the future.

Usage

It's recommended to use Freighter in a new project, so let's just start one:

npm install -g express # if you didn't install it before
express example
cd example
npm install
npm install --save freighter

add a file public/javascripts/string.js:

exports.eeoo = function (str) {
  return str.replace(/EE/g, 'OO');
}

and public/javascripts/beepboop.js:

var str = require('string');
console.log(str.eeoo('BEEP'));
exports.more = function () {
  console.log(str.eeoo('BEEP BOP BEEP'));
}

modify app.js to use Freighter:

// ...
var freighter = require('freighter');
var app = express();
// ...
app.use(app.router);
app.use(freighter());
app.use(express.static(path.join(__dirname, 'public')));

one more thing, add some lines in views/layout.jade:

!= js()
script
  seajs.use('beepboop', function (bb) {
    bb.more();
  });

now run node app.js and visit http://127.0.0.1:3000/ and open Developer Tools in your browser, you should see output like:

BOOP
BOOP BOP BOOP

Also, as you can see, string.js and beepboop.js is totally compatible with node.js, so you can use it in node.js directly, just try it yourself.

The RDD Strategy of Freighter

Right now I'm developing Freighter using a Readme-Driven Development strategy. So everything you read below, is my current focus and will be done in my next git push.

Planning

v0.0.1b

Need some tests and travis-ci.

v0.0.2

Serve coffee-script files.

It will also work with coffee script files, so if you change string.js to string.coffee:

exports.eeoo = (str) ->
  str.replace(/EE/g, 'OO')

and beepboop.js to beepboop.coffee:

{eeoo} = require('string')
console.log(eeoo('BEEP'))
exports.more = ->
  console.log(eeoo('BEEP BOP BEEP'))

It will also works.

How It Works

Sorry, not today. I'll write it down after v0.0.2 published, maybe.