Node.js require allows you
Thus, the modular system works in Node.js. But if you want to have the appropriate functionality to minimize, you can flip it yourself, for example,
var path = require("path"), glob = require("glob"); function requirer(pattern) { var modules = {}, files = glob.sync(pattern); files.forEach(function(currentFile) { var fileName = path.basename(currentFile); fileName = fileName.substring(0, fileName.lastIndexOf(".js")); modules[fileName] = require(currentFile); }); return modules; }
It depends on the glob , which allows you to use minimization templates to search for files, and then we require the found files, save them in the object and return the object. And it can be used like this
var modules = requirer('./packages/*/model/*.js'); console.log(modules.cities);
PS: I'm already working on making this a public module .
source share