Discover local dependencies outside of immediate node_modules
I really hope that I'm just missing something obvious, so please close this issue if I just misread the documentation or something.
Let's say I have a very simple module with a single `index.js` that I want to test. If I have this in my `tachometer.json`:
```json
{
"benchmarks": [
{
"url": "./benchmark.html",
"expand": [
{
"name": "this-change"
},
{
"name": "tip-of-tree",
"packageVersions": {
"label": "tip-of-tree",
"dependencies": {
"my-package-name": {
"kind": "git",
"repo": "https://github.com/org/my-package-name.git",
"ref": "master"
}
}
}
}
]
}
]
}
```
And then I use this in my `benchmark.html`:
```js
import myPackage from 'my-package-name'
```
This actually works for the remote dependency, but it doesn't work for the local dependency. Instead, Tachometer doesn't transform the `import` statement, so the browser can't find the module.

Here is [a small repro](https://gist.github.com/nolanlawson/85bdba522731e3fd7d7f4e5d4cf2233f).
The only solution I've found is to manually add symlinks so that `node_modules/my-package-name` is linked locally:
ln -s .. node_modules/repro-tach-dependency
Then Tachometer works as expected - the local dependency is resolved locally, and the remote one is resolved by fetching it from npm.
I've also observed this issue in a monorepo, where the project structure is like this:
```
packages/my-package-name
packages/my-package-name/index.js
packages/benchmark
packages/benchmark/benchmark.html
```
In this case, if `benchmark.html` tries to do `import 'my-package-name'`, it won't resolve locally.
This might have something to do with subtleties of dependency hoisting. In a monorepo project _without_ dependency hoisting, `packages/benchmark/node_modules` would indeed contain the `my-package-name` package. But in the case I ran into, dependency hoisting moves all the dependencies to the top-level `node_modules`, so Tachometer can't find it, because it seems to check only the immediate `node_modules` directory.
The solution I've found is again to do the `node_modules` symlinking, e.g.:
ln -s ../../my-package-name ./packages/benchmark/node_modules/my-package-name
To solve both these issues (if this is indeed an issue and I didn't just misconfigure something), it seems to me that the Tachometer module resolver should:
- search for packages at the root level via `package.json` (for the simple single-package case)
- search recursively up the tree for `node_modules` (for the hoisted monorepo case)
Thanks for reading this far, and thanks for creating Tachometer! It's a great tool, and I can use it just fine with the symlinking workaround, but I thought I'd report the issue in case others ran into it or I missed something. :slightly_smiling_face:
关闭于 2024-08-03 3 条评论