Skip to content

Commit

Permalink
fix!: Avoid emitting requireFail events if any loader succeeds
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored and phated committed Nov 22, 2021
1 parent 4405663 commit a2477d3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
20 changes: 10 additions & 10 deletions lib/register_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ module.exports = function(eventEmitter, extensions, configPath, cwd) {
}

var autoloads = rechoir.prepare(extensions, configPath, cwd, true);
if (autoloads instanceof Error) {
autoloads = autoloads.failures;
if (autoloads instanceof Error) { // Only errors
autoloads.failures.forEach(function(failed) {
eventEmitter.emit('requireFail', failed.moduleName, failed.error);
});
return;
}

if (Array.isArray(autoloads)) {
autoloads.forEach(function (attempt) {
if (attempt.error) {
eventEmitter.emit('requireFail', attempt.moduleName, attempt.error);
} else {
eventEmitter.emit('require', attempt.moduleName, attempt.module);
}
});
if (!Array.isArray(autoloads)) { // Already required or no config.
return;
}

var succeeded = autoloads[autoloads.length - 1];
eventEmitter.emit('require', succeeded.moduleName, succeeded.module);
};
Empty file.
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {

const path = require('path');

require.extensions['.conf'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-conf';
};

}());
39 changes: 34 additions & 5 deletions test/lib/register_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,48 @@ describe('registerLoader', function() {
registerLoader(app, extensions, configPath);
});

it('Should emit only a "require" event when registering loader ' +
'failed and succeeds', function(done) {

var loaderPath = path.join(testDir, 'require-conf.js');
var configPath = path.join(testDir, 'app.conf');
var extensions = { '.conf': ['xxx', loaderPath] };

var app = new App();
app.on('require', function(moduleName, module) {
console.log('require', moduleName);
expect(moduleName).to.be.equal(loaderPath);
expect(require(configPath)).to.equal('Load app.conf by require-conf');
done();
});
app.on('requireFail', handlerNotEmit);

registerLoader(app, extensions, configPath);
});

it('Should emit a "requireFail" event when loader is not found',
function(done) {

var loaderPath = path.join(testDir, 'require-tmp.js');
var configPath = path.join(testDir, 'app.tmp');
var extensions = { '.tmp': loaderPath };
var extensions = { '.tmp': ['xxx', loaderPath] };

var app = new App();
var index = 0;
app.on('requireFail', function(moduleName, error) {
expect(moduleName).to.be.equal(loaderPath);
expect(error).to.be.an('error');
expect(error.message).to.contain('Cannot find module');
done();
if (index === 0) {
expect(moduleName).to.be.equal('xxx');
expect(error).to.be.an('error');
expect(error.message).to.contain('Cannot find module');
} else if (index === 1) {
expect(moduleName).to.be.equal(loaderPath);
expect(error).to.be.an('error');
expect(error.message).to.contain('Cannot find module');
done();
} else {
fail();
}
index ++;
});
app.on('require', handlerNotEmit);

Expand Down

0 comments on commit a2477d3

Please sign in to comment.