Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/webpack/enhanced-require
[CURRENTLY UNMAINTAINED] Enhance the require function in node.js with support for loaders which preprocess files. This is a standalone polyfill for features of webpack.
https://github.com/webpack/enhanced-require
Last synced: about 1 month ago
JSON representation
[CURRENTLY UNMAINTAINED] Enhance the require function in node.js with support for loaders which preprocess files. This is a standalone polyfill for features of webpack.
- Host: GitHub
- URL: https://github.com/webpack/enhanced-require
- Owner: webpack
- Created: 2012-07-01T13:41:28.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2015-05-04T06:56:18.000Z (over 9 years ago)
- Last Synced: 2024-03-28T17:14:33.429Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 432 KB
- Stars: 64
- Watchers: 6
- Forks: 13
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# enhanced-require
More features for node.js require.
* [loader support](https://github.com/sokra/modules-webpack/wiki/Loader-Specification)
* `require.ensure`
* AMD `require`, `define` (from require.js)
* `require.context`
* [Hot Code Replacement](https://github.com/webpack/enhanced-require/wiki/HCR-Spec)
* module substitutions for mockingAsynchron require functions are **really** async. They do not use the sync node.js require, but use a async resolving and async readFile.
## Create a enhanced require function
``` javascript
var myRequire = require("enhanced-require")(module, {
// options
recursive: true // enable for all modules recursivly
// This replaces the original require function in loaded modules
});// startup your application
myRequire("./startup");
```## Usage
Than you can use them:
``` javascript
// use loaders
var fileContent = require("raw!"+__filename);// use loaders automatically
var template = require("./my-template.jade");var html = template({content: fileContent});
// use require.context
var directoryRequire = require.context("raw!./subdir");
var txtFile = directoryRequire("./aFile.txt");// use require.ensure
require.ensure(["./someFile.js"], function(require) {
var someFile = require("./someFile.js");
});// use AMD define
require.define(["./aDep"], function(aDep) {
aDep.run();
});// use AMD require
require(["./bDep"], function(bDep) {
bDep.doSomething();
});
```## Hot Code Replacement
``` javascript
require("enhanced-require")(module, {
recursive: true, // enable for all modules
hot: true, // enable hot code replacement
watch: true // watch for changes
})("./startup");
```For hot code reloading you need to follow the [hot code reloading spec](https://github.com/webpack/enhanced-require/wiki/HCR-Spec).
## Testing/Mocking
``` javascript
var er = require("enhanced-require");
it("should read the config option", function(done) {
var subject = er(module, {
recursive: true,
substitutions: {
// specify the exports of a module directly
"../lib/config.json": {
"test-option": { value: 1234 }
}
},
substitutionFactories: {
// specify lazy generated exports of a module
"../lib/otherConfig.json": function(require) {
// export the same object as "config.json"
return require("../lib/config.json");
}
}
})("../lib/subject");var result = subject.getConfigOption("test-option");
should.exist(result);
result.should.be.eql({ value: 1234 });
});
```## Options
``` javascript
{
recursive: false,
// replace require function in required modules with enhanced require methodresolve: {
// ...
// see enhanced-resolve
// https://github.com/webpack/enhanced-resolve
},
substitutions: {},
substitutionFactories: {},
// See above
// Replace modules with mocks
// keys are resolved and have to existamd: {},
// The require.amd objectenhanced: {},
// The require.enhanced objectloader: {},
// additional stuff in the loaderContexthot: false,
// enable hot code replacementwatch: false,
// Watch for file changes and issue hot replacementwatchDelay: 400,
// Time to summarize changes for watching
}
```## Future Plans
* cli tool
## License
Copyright (c) 2012 Tobias Koppers
MIT (http://www.opensource.org/licenses/mit-license.php)