From 41d72b0ddedebc3d5e9c5a838b43ac12cde6e8ce Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 6 Apr 2022 19:21:55 -0700 Subject: [PATCH 1/2] feat: Leverage endsWith instead of RegExp in matchers --- index.js | 79 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 3492e61..00ff7ba 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,46 @@ 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 +// which only captures the final extension (.esm.js -> .js) +// 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 = { @@ -75,9 +94,7 @@ var extensions = { mod.register({ extensions: ['.js'], target: 'node' + process.version.slice(1), - hookMatcher: function (file) { - return endsInEsbuildJs.test(file); - }, + hookMatcher: endsInEsbuildJs }); }, }, @@ -87,9 +104,7 @@ var extensions = { mod.register({ extensions: ['.jsx'], target: 'node' + process.version.slice(1), - hookMatcher: function (file) { - return endsInEsbuildJsx.test(file); - }, + hookMatcher: endsInEsbuildJsx }); }, }, @@ -99,9 +114,7 @@ var extensions = { mod.register({ extensions: ['.ts'], target: 'node' + process.version.slice(1), - hookMatcher: function (file) { - return endsInEsbuildTs.test(file); - }, + hookMatcher: endsInEsbuildTs }); }, }, @@ -111,9 +124,7 @@ var extensions = { mod.register({ extensions: ['.tsx'], target: 'node' + process.version.slice(1), - hookMatcher: function (file) { - return endsInEsbuildTsx.test(file); - }, + hookMatcher: endsInEsbuildTsx }); }, }, @@ -167,9 +178,7 @@ var extensions = { mod.register({ extensions: ['.ts'], target: 'node' + process.version.slice(1), - hookMatcher: function (file) { - return endsInTs.test(file); - }, + hookMatcher: endsInTs }); }, }, @@ -203,9 +212,7 @@ var extensions = { mod.register({ extensions: ['.tsx'], target: 'node' + process.version.slice(1), - hookMatcher: function (file) { - return endsInTsx.test(file); - }, + hookMatcher: endsInTsx }); }, }, From c12b3dbf3bcd8d8d2ad8342371c13d3b82bf5101 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 6 Apr 2022 19:25:14 -0700 Subject: [PATCH 2/2] improve comment --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 00ff7ba..8969d0a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ var path = require('path'); // We only register on the final extension (like `.js`) due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353 -// which only captures the final extension (.esm.js -> .js) // However, we use these matchers to apply the transform only if the full extension matches function endsInJsx(filename) { return filename.endsWith('.jsx');