Skip to content

Commit f9d0c3e

Browse files
authoredJul 15, 2022
feat: support map cjs to cts, mjs to mts automatically (#88)
1 parent acd248e commit f9d0c3e

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed
 

‎.changeset/poor-fishes-attend.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"synckit": patch
3+
---
4+
5+
feat: support map `cjs` to `cts`, `mjs` to `mts` automatically

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ runAsWorker(async (...args) => {
6262
})
6363
```
6464

65-
You must make sure, the `result` is serialized by [`Structured Clone Algorithm`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
65+
You must make sure, the `result` is serializable by [`Structured Clone Algorithm`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
6666

6767
### Envs
6868

‎src/index.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,31 @@ const dataUrl = (code: string) =>
105105
const setupTsNode = (workerPath: string, execArgv: string[]) => {
106106
if (!/[/\\]node_modules[/\\]/.test(workerPath)) {
107107
const ext = path.extname(workerPath)
108-
// TODO: support `.cts` and `.mts` automatically
109-
if (!ext || ext === '.js') {
110-
const found = tryExtensions(
111-
ext ? workerPath.replace(/\.js$/, '') : workerPath,
112-
['.ts', '.js'],
113-
)
114-
if (found) {
108+
if (!ext || /\.[cm]?js$/.test(ext)) {
109+
const workPathWithoutExt = ext
110+
? workerPath.slice(0, -ext.length)
111+
: workerPath
112+
let extensions: string[]
113+
switch (ext) {
114+
case '.cjs':
115+
extensions = ['cts', 'cjs']
116+
break
117+
case '.mjs':
118+
extensions = ['mts', 'mjs']
119+
break
120+
default:
121+
extensions = ['.ts', '.js']
122+
break
123+
}
124+
const found = tryExtensions(workPathWithoutExt, extensions)
125+
if (found && (!ext || found !== workPathWithoutExt)) {
115126
workerPath = found
116127
}
117128
}
118129
}
119130

120131
const isTs = /\.[cm]?ts$/.test(workerPath)
121132

122-
// TODO: it does not work for `ts-node` for now
123133
let tsUseEsm = workerPath.endsWith('.mts')
124134

125135
if (isTs) {

0 commit comments

Comments
 (0)
Please sign in to comment.