liuyanghejerry
2014-06-22 22:52:12 +08:00
// require_x.js
var fs = require('fs');
module.exports = {
require_x: function (name) {
var mod = fs.readFileSync(name);
mod = decode(mod);
var real_name = 'hack here as you wish';
var mod_path = save_file_into_one_dir(real_name, mod);
// now you have the original js module
// but be sure all deps are installed in that path.
return require(mod_path);
},
transform_x: function (file) {
// use async read if you want to
var mod = fs.readFileSync(file);
mod = encode(mod);
var transformed_name = 'hack here as you wish';
var mod_path = save_file_into_one_dir(transformed_name, mod);
// now you have your transformed js module file
return true;
}
}
function encode (content) {
// do your hack here
return content;
}
function decode (content) {
// do your hack here
return content;
}
function save_file_into_one_dir(name, content) {
// do your hack here
return real_path;
}
// app.js
var require_x = require('./require_x.js');
var my_mod = require_x('./my_mod_x.js');
var other_mod = require('other_mod');
my_mod.use.what.you.have();