Skip to content

Commit ac4f30b

Browse files
authored
fix(vitest-pool-workers): enable dependency pre-bundling (#7810)
1 parent c8719b1 commit ac4f30b

File tree

31 files changed

+645
-593
lines changed

31 files changed

+645
-593
lines changed

.changeset/gold-frogs-rest.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
Added [Vite dependency pre-bundling](https://vite.dev/guide/dep-pre-bundling) support. If you encounter module resolution issues—such as: `Error: Cannot use require() to import an ES Module` or `Error: No such module`—you can now bundle these dependencies using the [deps.optimizer](https://vitest.dev/config/#deps-optimizer) option:
6+
7+
```tsx
8+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
9+
10+
export default defineWorkersConfig({
11+
test: {
12+
deps: {
13+
optimizer: {
14+
ssr: {
15+
enabled: true,
16+
include: ["your-package-name"],
17+
},
18+
},
19+
},
20+
poolOptions: {
21+
workers: {
22+
// ...
23+
},
24+
},
25+
},
26+
});
27+
```
28+
29+
Fixed #6591, #6581, #6405.

fixtures/vitest-pool-workers-examples/external-package-resolution/test/index.spec.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/external-package-resolution/test/tsconfig.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/external-package-resolution/vitest.config.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/internal-module-resolution/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/internal-module-resolution/test/index.spec.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/internal-module-resolution/tsconfig.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/internal-module-resolution/wrangler.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ⚡ module-resolution
2+
3+
This fixture demonstrates that the Vitest integration correctly resolves modules, including:
4+
5+
- A CommonJS package that requires a directory rather than a specific file.
6+
- A package without a main entrypoint or with browser field mapping, handled via [Dependency Pre-Bundling](#dependency-pre-bundling).
7+
8+
## Dependency Pre-Bundling
9+
10+
[Dependency Pre-Bundling](https://vite.dev/guide/dep-pre-bundling) is a Vite feature that converts dependencies shipped as CommonJS or UMD into ESM. If you encounter module resolution issues—such as: `Error: Cannot use require() to import an ES Module` or `Error: No such module`—you can pre-bundle these dependencies using the [deps.optimizer](https://vitest.dev/config/#deps-optimizer) option:
11+
12+
```ts
13+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
14+
15+
export default defineWorkersConfig({
16+
test: {
17+
deps: {
18+
optimizer: {
19+
ssr: {
20+
enabled: true,
21+
include: ["your-package-name"],
22+
},
23+
},
24+
},
25+
poolOptions: {
26+
workers: {
27+
// ...
28+
},
29+
},
30+
},
31+
});
32+
```
33+
34+
See our [vitest config](./vitest.config.ts) for an example of how we pre-bundled `discord-api-types/v10` and `@microlabs/otel-cf-workers`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import { Toucan } from "toucan-js";
2+
// Testing dependency without a main entrypoint
3+
// @see https://github.com/cloudflare/workers-sdk/issues/6591
4+
import "discord-api-types/v10";
5+
// Testing dependency with browser field mapping
6+
// @see https://github.com/cloudflare/workers-sdk/issues/6581
7+
import "@microlabs/otel-cf-workers";
28

39
export default {
410
async fetch(): Promise<Response> {

0 commit comments

Comments
 (0)