RealmJs is a new dependency injection/module handling tool for Node.js and javascript projects. The library is universal (isomorphic). You can easily share modules between frontend and backend accordingly.
Real.js comes with an absolutely superb transpiler, which resembles es6 imports. It essentially has the same syntax with few improvements
"use realm";
import myModule from myapp;
import lodash as _ from myapp.utils;
class MySuperClass {
}
export MySuperClass;
Realm transpiler goes through files and converts, say MySuperClass.js file into
realm.module("test.MySuperClass", ["myapp.myModule", "myapp.utils.lodash"], function (myModule, _) {
class MySuperClass {
}
return MySuperClass;
});
Add babel7 and your are unstoppable!
npm install realm-js --save
Check a simple project and see what it compiles into test-backend.js (with a little help from babel es7)
If you want to serve realm.js you can just use express middleware
app.use('/ream.js', realm.serve.express());
To get contents (for build)
realm.serve.getContents()
Everything revolves around es6 promises:
realm.module("MyFirstModule", function() {
return new Promise(function(resolve, reject){
return resolve({hello : "world"})
});
});
realm.module("MySecondModule", function(MyFirstModule) {
console.log(MyFirstModule);
});
Code:
realm.require(function(MySecondModule){
console.log(MySecondModule)
});
Will resolve all required dependencies. The ouput:
{hello: "world"}
You can require a package if you like.
realm.requirePackage("app.components").then(function(components){
});
Clearly, if you don't use ec6, or any other transpilers, you need to annotate modules
realm.module("myModule", ["moduleA", "moduleB"], function(moduleA, moduleB){
})
Universal wrapper has a parameter called $isBackend. So, if you want to import lodash (my favorite) or any other libaries. you can register them like so:
domain.module("shared._", function() {
return $isBackend ? require("lodash") : window._;
});
domain.module("shared.realm", function() {
return $isBackend ? require("realm-js") : window.realm;
});
The current transpiler is a very simple regExp like script. (I am not sure if i can call transpiler though). I have been using this library for years, and decided to release just now. I've tried to create a babel plugin, but this thing is just ginormous and i simply don't have time for that. If you feel like, go ahead!
If a module does not belong to any package:
import Module
If a module belongs to a package:
import Module from app
Giving it alias
import Module as mod from app
realm.transpiler({
preffix: "test",
base : "test-app-backend",
target : "./test-backend.js"
})
Wrapping into a universal function
realm.transpiler({wrap : true})
You can use babel to transpile your code into anything you like. (RealmJs transpiler should come first)
Here is a sample build task;
gulp.task("build-backend", function() {
return gulp.src("test-app-backend/**/*.js").pipe(realm.transpiler({
preffix: "test",
base : "test-app-backend",
target : "./test-backend.js"
}))
.pipe(babel({
presets: ["es2016"],
plugins: ["transform-decorators-legacy"]
}))
.pipe(realm.transpiler({wrap : true}))
.pipe(gulp.dest("./"));
});
Please, contribute. The code isn't in its best shape but rocks!