lukeed / lukeed/tsm

Doesn't handle package.json `imports` field

Open
#45 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1.2k
Forks
18
PR merge metrics
No merged PRs in 30d

Description

the [`chalk`](https://www.npmjs.com/package/chalk) uses `imports` in the package.json. these start with a `#`. I'm not sure of the full semantics of how this "should" work. It might be as simple as switching [this](https://github.com/lukeed/tsm/blob/master/src/loader.ts#L87):

```diff
- if (/^\w+\:?/.test(ident)) return fallback(ident, context, fallback);
+ if (/^(\w+\:?|#)/.test(ident)) return fallback(ident, context, fallback);
```

but that might be too naive to work in every case.

It seems to be because the [`check`](https://github.com/lukeed/tsm/blob/master/src/loader.ts#L68-L71) function calls `fileURLToPath` from `url`, which strips the `#` and following text, meaning that the `check` function ends up just checking if the `parentURL` exists.

I think the _full_ solution to this would be to, if there's a `#` at the start of the import, call `fallback` and then process the result of `fallback`, as it might resolve to a typescript file. It _might_ be enough to just ignore any `#` files and assume that the package exposing them is not using typescript?

## Repro Steps

- `mkdir imports-report && cd imports-repro`
- `npm init -y`
- `npm add chalk tsm`
- `echo "import chalk from 'chalk';" > index.mjs`
- `node --loader tsm index.mjs`

you'll see

```
file:////imports-repro/node_modules/chalk/source/index.js#supports-color.mts:7
const { stdout: stdoutColor, stderr: stderrColor } = supportsColor;
^

TypeError: Cannot destructure property 'stdout' of 'supportsColor' as it is undefined.
at file:////imports-repro/node_modules/chalk/source/index.js#supports-color.mts:7:17
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:527:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/loader.ts around the check function at lines 68-71 and the fallback condition near line 87. Reproduce the issue with chalk's package.json imports using the listed npm and node --loader tsm commands, then verify that # imports resolve correctly without breaking TypeScript file loading.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.