Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Leverage endsWith instead of RegExp in matchers #82

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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