luanglopes / ts-paths-esm-loader

Node JS loader for Typescript that supports tsconfig paths
MIT License
23 stars 5 forks source link

ESM support in extension .js #12

Closed v0ldemar01 closed 1 year ago

v0ldemar01 commented 1 year ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch ts-paths-esm-loader@1.3.2 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/ts-paths-esm-loader/resolverFactory.js b/node_modules/ts-paths-esm-loader/resolverFactory.js
index 33f3420..959b978 100644
--- a/node_modules/ts-paths-esm-loader/resolverFactory.js
+++ b/node_modules/ts-paths-esm-loader/resolverFactory.js
@@ -3,10 +3,12 @@ import { pathToFileURL } from 'url'
 import { matchPath } from './matchPath.js'

 export const resolverFactory = (resolveTs) => {
-    return function (specifier, ctx, defaultResolve) {
-        const match = matchPath && matchPath(specifier)
-        return match
-          ? resolveTs(pathToFileURL(`${match}`).href, ctx, defaultResolve)
-          : resolveTs(specifier, ctx, defaultResolve)
-      }
-}
+  return function (specifier, ctx, defaultResolve) {
+    if (specifier.endsWith('.js')) {
+      const trimmed = specifier.substring(0, specifier.length - 3)
+      const match = matchPath(trimmed)
+      if (match) return resolveTs(pathToFileURL(`${match}.js`).href, ctx, defaultResolve)
+    }
+    return resolveTs(specifier, ctx, defaultResolve)
+  }
+};

This issue body was partially generated by patch-package.

luanglopes commented 1 year ago

@v0ldemar01 thanks for your issue and solution, please, take a look at the PR I created and try it on your project, it is supposed to add the support for js extension and keep the same behavior for other files, if you find that everything is ok, I will merge and publish it