Skip to content

Commit

Permalink
feat: Leverage endsWith instead of RegExp in matchers (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Apr 7, 2022
1 parent 9847d11 commit 6404724
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
var path = require('path');

var endsInJsx = /\.jsx$/;
var endsInTs = /\.ts$/;
var endsInTsx = /\.tsx$/;
var endsInBabelJs = /\.babel\.js$/;
var endsInBabelJsx = /\.babel\.jsx$/;
var endsInBabelTs = /\.babel\.ts$/;
var endsInBabelTsx = /\.babel\.tsx$/;
var endsInEsbuildJs = /\.esbuild\.js$/;
var endsInEsbuildJsx = /\.esbuild\.jsx$/;
var endsInEsbuildTs = /\.esbuild\.ts$/;
var endsInEsbuildTsx = /\.esbuild\.tsx$/;
// We only register on the final extension (like `.js`) due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
// However, we use these matchers to apply the transform only if the full extension matches
function endsInJsx(filename) {
return filename.endsWith('.jsx');
}
function endsInTs(filename) {
return filename.endsWith('.ts');
}
function endsInTsx(filename) {
return filename.endsWith('.tsx');
}
function endsInBabelJs(filename) {
return filename.endsWith('.babel.js');
}
function endsInBabelJsx(filename) {
return filename.endsWith('.babel.jsx');
}
function endsInBabelTs(filename) {
return filename.endsWith('.babel.ts');
}
function endsInBabelTsx(filename) {
return filename.endsWith('.babel.tsx');
}
function endsInEsbuildJs(filename) {
return filename.endsWith('.esbuild.js');
}
function endsInEsbuildJsx(filename) {
return filename.endsWith('.esbuild.jsx');
}
function endsInEsbuildTs(filename) {
return filename.endsWith('.esbuild.ts');
}
function endsInEsbuildTsx(filename) {
return filename.endsWith('.esbuild.tsx');
}

var mjsStub = path.join(__dirname, 'mjs-stub');

// Not part of the above check because it seems broken
function isNodeModules(file) {
return (
path
.relative(process.cwd(), file)
.split(path.sep)
.indexOf('node_modules') >= 0
);
return path.relative(process.cwd(), file).includes('node_modules');
}

var extensions = {
Expand Down Expand Up @@ -75,9 +93,7 @@ var extensions = {
mod.register({
extensions: ['.js'],
target: 'node' + process.version.slice(1),
hookMatcher: function (file) {
return endsInEsbuildJs.test(file);
},
hookMatcher: endsInEsbuildJs
});
},
},
Expand All @@ -87,9 +103,7 @@ var extensions = {
mod.register({
extensions: ['.jsx'],
target: 'node' + process.version.slice(1),
hookMatcher: function (file) {
return endsInEsbuildJsx.test(file);
},
hookMatcher: endsInEsbuildJsx
});
},
},
Expand All @@ -99,9 +113,7 @@ var extensions = {
mod.register({
extensions: ['.ts'],
target: 'node' + process.version.slice(1),
hookMatcher: function (file) {
return endsInEsbuildTs.test(file);
},
hookMatcher: endsInEsbuildTs
});
},
},
Expand All @@ -111,9 +123,7 @@ var extensions = {
mod.register({
extensions: ['.tsx'],
target: 'node' + process.version.slice(1),
hookMatcher: function (file) {
return endsInEsbuildTsx.test(file);
},
hookMatcher: endsInEsbuildTsx
});
},
},
Expand Down Expand Up @@ -167,9 +177,7 @@ var extensions = {
mod.register({
extensions: ['.ts'],
target: 'node' + process.version.slice(1),
hookMatcher: function (file) {
return endsInTs.test(file);
},
hookMatcher: endsInTs
});
},
},
Expand Down Expand Up @@ -203,9 +211,7 @@ var extensions = {
mod.register({
extensions: ['.tsx'],
target: 'node' + process.version.slice(1),
hookMatcher: function (file) {
return endsInTsx.test(file);
},
hookMatcher: endsInTsx
});
},
},
Expand Down

0 comments on commit 6404724

Please sign in to comment.