Repository: un-ts/synckit (ts)

Issue:
Support `import.meta.resolve()` urls 
Currently docs tell you to use `require.resolve('./worker')`

I don't use cjs in new projects and would prefer to use `import.meta.resolve('./worker.ts')` instead



Pull request description:
feat: support file url worker path
close #187

Patch:
diff --git a/.changeset/rotten-squids-protect.md b/.changeset/rotten-squids-protect.md
new file mode 100644
index 0000000..d07b93c
--- /dev/null
+++ b/.changeset/rotten-squids-protect.md
@@ -0,0 +1,5 @@
+---
+"synckit": minor
+---
+
+feat: support file url worker path
diff --git a/src/index.ts b/src/index.ts
index 86b4874..6268450 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -113,11 +113,15 @@ export function extractProperties<T>(object?: T) {
 }
 
 export function createSyncFn<T extends AnyFn>(
-  workerPath: string,
+  workerPath: URL | string,
   timeoutOrOptions?: SynckitOptions | number,
 ): Syncify<T> {
   syncFnCache ??= new Map()
 
+  if (typeof workerPath !== 'string' || workerPath.startsWith('file://')) {
+    workerPath = fileURLToPath(workerPath)
+  }
+
   const cachedSyncFn = syncFnCache.get(workerPath)
 
   if (cachedSyncFn) {
@@ -159,7 +163,7 @@ const setupTsRunner = (
   let ext = path.extname(workerPath)
 
   if (
-    !/[/\\]node_modules[/\\]/.test(workerPath) &&
+    !/([/\\])node_modules\1/.test(workerPath) &&
     (!ext || /^\.[cm]?js$/.test(ext))
   ) {
     const workPathWithoutExt = ext


Test patch:
diff --git a/test/fn.spec.ts b/test/fn.spec.ts
index e1c6bd9..9280a3d 100644
--- a/test/fn.spec.ts
+++ b/test/fn.spec.ts
@@ -1,5 +1,6 @@
 import { createRequire } from 'node:module'
 import path from 'node:path'
+import { pathToFileURL } from 'node:url'
 
 import { jest } from '@jest/globals'
 
@@ -244,3 +245,30 @@ test('globalShims options', async () => {
   expect(syncFn(2)).toBe(2)
   expect(syncFn(5, 0)).toBe(5)
 })
+
+test('support file url', async () => {
+  const { createSyncFn } = await import('synckit')
+
+  const syncFn = createSyncFn<AsyncWorkerFn>(pathToFileURL(workerCjsPath), {})
+
+  expect(syncFn(1)).toBe(1)
+  expect(syncFn(2)).toBe(2)
+  expect(syncFn(5, 0)).toBe(5)
+
+  expect(() => createSyncFn(new URL('https://example.com'))).toThrow(
+    'The URL must be of scheme file',
+  )
+})
+
+test('support file url protocol', async () => {
+  const { createSyncFn } = await import('synckit')
+
+  const syncFn = createSyncFn<AsyncWorkerFn>(
+    pathToFileURL(workerCjsPath).href,
+    {},
+  )
+
+  expect(syncFn(1)).toBe(1)
+  expect(syncFn(2)).toBe(2)
+  expect(syncFn(5, 0)).toBe(5)
+})