Skip to content

Commit

Permalink
feat: support map cjs to cts, mjs to mts automatically (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 15, 2022
1 parent acd248e commit f9d0c3e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-fishes-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": patch
---

feat: support map `cjs` to `cts`, `mjs` to `mts` automatically
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ runAsWorker(async (...args) => {
})
```

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)
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)

### Envs

Expand Down
26 changes: 18 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,31 @@ const dataUrl = (code: string) =>
const setupTsNode = (workerPath: string, execArgv: string[]) => {
if (!/[/\\]node_modules[/\\]/.test(workerPath)) {
const ext = path.extname(workerPath)
// TODO: support `.cts` and `.mts` automatically
if (!ext || ext === '.js') {
const found = tryExtensions(
ext ? workerPath.replace(/\.js$/, '') : workerPath,
['.ts', '.js'],
)
if (found) {
if (!ext || /\.[cm]?js$/.test(ext)) {
const workPathWithoutExt = ext
? workerPath.slice(0, -ext.length)
: workerPath
let extensions: string[]
switch (ext) {
case '.cjs':
extensions = ['cts', 'cjs']
break
case '.mjs':
extensions = ['mts', 'mjs']
break
default:
extensions = ['.ts', '.js']
break
}
const found = tryExtensions(workPathWithoutExt, extensions)
if (found && (!ext || found !== workPathWithoutExt)) {
workerPath = found
}
}
}

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

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

if (isTs) {
Expand Down

0 comments on commit f9d0c3e

Please sign in to comment.