enhanced-require
More features for node.js require.
- loader support
require.ensure
- AMD
require
, define
(from require.js) require.context
- Hot Code Replacement
Asynchron require functions are really async. They do not use the sync node.js require, but use a async resolving and async readFile.
Single module
Add this line to a node.js module to activate the enhanced features in that module:
require = require("enhanced-require")(module);
if(typeof define != "function") var define = require.define;
Than you can use them:
var fileContent = require("raw!"+__filename);
var template = require("./my-template.jade");
var html = template({content: fileContent});
var directoryRequire = require.context("raw!./subdir");
var txtFile = directoryRequire("./aFile.txt");
require.ensure(["./someFile.js"], function(require) {
var someFile = require("./someFile.js");
});
require.define(["./aDep"], function(aDep) {
aDep.run();
});
require(["./bDep"], function(bDep) {
bDep.doSomething();
});
Complete application
Create a seperate entry module and require your original entry module:
require = require("enhanced-require")(module, {
recursive: true,
hot: true,
watch: true
});
require("./startup");
You can use all features in startup.js
and all required modules.
For hot code reloading you need to follow the hot code reloading spec.
Future Plans
License
Copyright (c) 2012 Tobias Koppers
MIT (http://www.opensource.org/licenses/mit-license.php)