Skip to content

Commit

Permalink
fix: Update rechoir to support dots in config name
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored and phated committed Nov 22, 2021
1 parent dea6860 commit 33a6286
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
"flagged-respawn": "^1.0.0",
"is-plain-object": "^2.0.4",
"object.map": "^1.0.0",
"rechoir": "^0.6.2",
"rechoir": "^0.7.0",
"resolve": "^1.1.7"
},
"devDependencies": {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-file-b.js
@@ -0,0 +1,10 @@
(function() {

var path = require('path');

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

}());
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-file-bc.js
@@ -0,0 +1,10 @@
(function() {

var path = require('path');

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

}());
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-file-cd.js
@@ -0,0 +1,10 @@
(function() {

var path = require('path');

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

}());
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-file-d.js
@@ -0,0 +1,10 @@
(function() {

var path = require('path');

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

}());
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-file-ecd.js
@@ -0,0 +1,10 @@
(function() {

var path = require('path');

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

}());
10 changes: 10 additions & 0 deletions test/fixtures/register_loader/require-file-fcd.js
@@ -0,0 +1,10 @@
(function() {

var path = require('path');

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

}());
83 changes: 83 additions & 0 deletions test/lib/register_loader.js
Expand Up @@ -231,5 +231,88 @@ describe('registerLoader', function() {
});
});

describe('Multiple extensions', function() {
it('should detect the shortest extension in extension candidates', function(done) {
var loaderPath = path.join(testDir, 'require-file-b.js');
var configPath = path.join(testDir, 'file.a.b');
var extensions = { '.b': loaderPath };

var app = new App();
app.on('requireFail', handlerNotEmit);
app.on('require', function(moduleName /* , module */) {
expect(moduleName).to.be.equal(loaderPath);
expect(require(configPath)).to.equal('Load file.a.b by require-file-b');
done();
});

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

it('should detect not shortest extension in extension candidates', function(done) {
var loaderPath = path.join(testDir, 'require-file-bc.js');
var configPath = path.join(testDir, 'file.a.b.c');
var extensions = { '.b.c': loaderPath };

var app = new App();
app.on('requireFail', handlerNotEmit);
app.on('require', function(moduleName /* , module */) {
expect(moduleName).to.be.equal(loaderPath);
expect(require(configPath)).to.equal('Load file.a.b.c by require-file-bc');
done();
});

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

it('Should update a loader of a longer extension but not update a loader of a shorter extension', function(done) {
var loaderPathD = path.join(testDir, 'require-file-d.js');
var loaderPathCD = path.join(testDir, 'require-file-cd.js');
var loaderPathECD = path.join(testDir, 'require-file-ecd.js');
var loaderPathFCD = path.join(testDir, 'require-file-fcd.js');

var configPathD = path.join(testDir, 'file.a.b.d');
var configPathCD = path.join(testDir, 'file.a.b.c.d');
var configPathECD = path.join(testDir, 'file.a.e.c.d');
var configPathFCD = path.join(testDir, 'file.a.f.c.d');

var extensions = {
'.d': loaderPathD,
'.c.d': loaderPathCD,
'.e.c.d': loaderPathECD,
'.f.c.d': loaderPathFCD,
};

var count = 0;
var app = new App();
app.on('requireFail', handlerNotEmit);
app.on('require', function(moduleName /* , module */) {
switch (count) {
case 0: {
expect(moduleName).to.be.equal(loaderPathCD);
expect(require(configPathCD)).to.equal('Load file.a.b.c.d by require-file-cd');
break;
}
case 1: {
expect(moduleName).to.be.equal(loaderPathECD);
expect(require(configPathECD)).to.equal('Load file.a.e.c.d by require-file-ecd');
break;
}
case 2: {
expect(moduleName).to.be.equal(loaderPathFCD);
expect(require(configPathFCD)).to.equal('Load file.a.f.c.d by require-file-fcd');
done();
break;
}
}
count++;
});

registerLoader(app, extensions, configPathCD);
registerLoader(app, extensions, configPathECD);
registerLoader(app, extensions, configPathD); // Don't register loader.
registerLoader(app, extensions, configPathFCD);
});
});

});

0 comments on commit 33a6286

Please sign in to comment.