Skip to content

Commit 6404724

Browse files
authored
feat: Leverage endsWith instead of RegExp in matchers (#82)
1 parent 9847d11 commit 6404724

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

Diff for: index.js

+42-36
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11
var path = require('path');
22

3-
var endsInJsx = /\.jsx$/;
4-
var endsInTs = /\.ts$/;
5-
var endsInTsx = /\.tsx$/;
6-
var endsInBabelJs = /\.babel\.js$/;
7-
var endsInBabelJsx = /\.babel\.jsx$/;
8-
var endsInBabelTs = /\.babel\.ts$/;
9-
var endsInBabelTsx = /\.babel\.tsx$/;
10-
var endsInEsbuildJs = /\.esbuild\.js$/;
11-
var endsInEsbuildJsx = /\.esbuild\.jsx$/;
12-
var endsInEsbuildTs = /\.esbuild\.ts$/;
13-
var endsInEsbuildTsx = /\.esbuild\.tsx$/;
3+
// We only register on the final extension (like `.js`) due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
4+
// However, we use these matchers to apply the transform only if the full extension matches
5+
function endsInJsx(filename) {
6+
return filename.endsWith('.jsx');
7+
}
8+
function endsInTs(filename) {
9+
return filename.endsWith('.ts');
10+
}
11+
function endsInTsx(filename) {
12+
return filename.endsWith('.tsx');
13+
}
14+
function endsInBabelJs(filename) {
15+
return filename.endsWith('.babel.js');
16+
}
17+
function endsInBabelJsx(filename) {
18+
return filename.endsWith('.babel.jsx');
19+
}
20+
function endsInBabelTs(filename) {
21+
return filename.endsWith('.babel.ts');
22+
}
23+
function endsInBabelTsx(filename) {
24+
return filename.endsWith('.babel.tsx');
25+
}
26+
function endsInEsbuildJs(filename) {
27+
return filename.endsWith('.esbuild.js');
28+
}
29+
function endsInEsbuildJsx(filename) {
30+
return filename.endsWith('.esbuild.jsx');
31+
}
32+
function endsInEsbuildTs(filename) {
33+
return filename.endsWith('.esbuild.ts');
34+
}
35+
function endsInEsbuildTsx(filename) {
36+
return filename.endsWith('.esbuild.tsx');
37+
}
1438

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

17-
// Not part of the above check because it seems broken
1841
function isNodeModules(file) {
19-
return (
20-
path
21-
.relative(process.cwd(), file)
22-
.split(path.sep)
23-
.indexOf('node_modules') >= 0
24-
);
42+
return path.relative(process.cwd(), file).includes('node_modules');
2543
}
2644

2745
var extensions = {
@@ -75,9 +93,7 @@ var extensions = {
7593
mod.register({
7694
extensions: ['.js'],
7795
target: 'node' + process.version.slice(1),
78-
hookMatcher: function (file) {
79-
return endsInEsbuildJs.test(file);
80-
},
96+
hookMatcher: endsInEsbuildJs
8197
});
8298
},
8399
},
@@ -87,9 +103,7 @@ var extensions = {
87103
mod.register({
88104
extensions: ['.jsx'],
89105
target: 'node' + process.version.slice(1),
90-
hookMatcher: function (file) {
91-
return endsInEsbuildJsx.test(file);
92-
},
106+
hookMatcher: endsInEsbuildJsx
93107
});
94108
},
95109
},
@@ -99,9 +113,7 @@ var extensions = {
99113
mod.register({
100114
extensions: ['.ts'],
101115
target: 'node' + process.version.slice(1),
102-
hookMatcher: function (file) {
103-
return endsInEsbuildTs.test(file);
104-
},
116+
hookMatcher: endsInEsbuildTs
105117
});
106118
},
107119
},
@@ -111,9 +123,7 @@ var extensions = {
111123
mod.register({
112124
extensions: ['.tsx'],
113125
target: 'node' + process.version.slice(1),
114-
hookMatcher: function (file) {
115-
return endsInEsbuildTsx.test(file);
116-
},
126+
hookMatcher: endsInEsbuildTsx
117127
});
118128
},
119129
},
@@ -167,9 +177,7 @@ var extensions = {
167177
mod.register({
168178
extensions: ['.ts'],
169179
target: 'node' + process.version.slice(1),
170-
hookMatcher: function (file) {
171-
return endsInTs.test(file);
172-
},
180+
hookMatcher: endsInTs
173181
});
174182
},
175183
},
@@ -203,9 +211,7 @@ var extensions = {
203211
mod.register({
204212
extensions: ['.tsx'],
205213
target: 'node' + process.version.slice(1),
206-
hookMatcher: function (file) {
207-
return endsInTsx.test(file);
208-
},
214+
hookMatcher: endsInTsx
209215
});
210216
},
211217
},

0 commit comments

Comments
 (0)