Skip to content

Commit

Permalink
feat: Add beforeRequire event
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored and phated committed Nov 22, 2021
1 parent 33a6286 commit 65f350d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -446,14 +446,25 @@ A function called after your application is executed. When invoked, `this` will

### events

#### beforeRequire(name)

Emitted before a module is pre-load. (But for only a module which is specified by `opts.require`.)

```js
var Hacker = new Liftoff({name:'hacker', require:'coffee-script'});
Hacker.on('beforeRequire', function (name) {
console.log('Requiring external module: '+name+'...');
});
```

#### require(name, module)

Emitted when a module is pre-loaded.
Emitted when a module has been pre-loaded.

```js
var Hacker = new Liftoff({name:'hacker'});
Hacker.on('require', function (name, module) {
console.log('Requiring external module: '+name+'...');
console.log('Required external module: '+name+'...');
// automatically register coffee-script extensions
if (name === 'coffee-script') {
module.register();
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -26,6 +26,7 @@ util.inherits(Liftoff, EE);

Liftoff.prototype.requireLocal = function(module, basedir) {
try {
this.emit('beforeRequire', module);
var result = require(resolve.sync(module, { basedir: basedir }));
this.emit('require', module, result);
return result;
Expand Down
16 changes: 14 additions & 2 deletions test/index.js
Expand Up @@ -347,20 +347,32 @@ describe('Liftoff', function() {
}
});

it('should emit `require` with the name of the module and the required module', function(done) {
it('should emit `beforeRequire` and `require` with the name of the module and the required module', function(done) {
var requireTest = new Liftoff({ name: 'require' });
var isEmittedBeforeRequired = false;
requireTest.on('beforeRequire', function(name) {
expect(name).to.equal('mocha');
isEmittedBeforeRequired = true;
});
requireTest.on('require', function(name, module) {
expect(name).to.equal('mocha');
expect(module).to.equal(require('mocha'));
expect(isEmittedBeforeRequired).to.equal(true);
done();
});
requireTest.requireLocal('mocha', __dirname);
});

it('should emit `requireFail` with an error if a module can\'t be found.', function(done) {
it('should emit `beforeRequire` and `requireFail` with an error if a module can\'t be found.', function(done) {
var requireFailTest = new Liftoff({ name: 'requireFail' });
var isEmittedBeforeRequired = false;
requireFailTest.on('beforeRequire', function(name) {
expect(name).to.equal('badmodule');
isEmittedBeforeRequired = true;
});
requireFailTest.on('requireFail', function(name) {
expect(name).to.equal('badmodule');
expect(isEmittedBeforeRequired).to.equal(true);
done();
});
requireFailTest.requireLocal('badmodule', __dirname);
Expand Down

0 comments on commit 65f350d

Please sign in to comment.