-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vitest-pool-workers): enable dependency pre-bundling (#7810)
- Loading branch information
1 parent
c8719b1
commit ac4f30b
Showing
31 changed files
with
645 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
"@cloudflare/vitest-pool-workers": patch | ||
--- | ||
|
||
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: | ||
|
||
```tsx | ||
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; | ||
|
||
export default defineWorkersConfig({ | ||
test: { | ||
deps: { | ||
optimizer: { | ||
ssr: { | ||
enabled: true, | ||
include: ["your-package-name"], | ||
}, | ||
}, | ||
}, | ||
poolOptions: { | ||
workers: { | ||
// ... | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
Fixed #6591, #6581, #6405. |
9 changes: 0 additions & 9 deletions
9
fixtures/vitest-pool-workers-examples/external-package-resolution/test/index.spec.ts
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
fixtures/vitest-pool-workers-examples/external-package-resolution/test/tsconfig.json
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
fixtures/vitest-pool-workers-examples/external-package-resolution/vitest.config.ts
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
fixtures/vitest-pool-workers-examples/internal-module-resolution/README.md
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
fixtures/vitest-pool-workers-examples/internal-module-resolution/test/index.spec.ts
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
fixtures/vitest-pool-workers-examples/internal-module-resolution/tsconfig.json
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
fixtures/vitest-pool-workers-examples/internal-module-resolution/wrangler.toml
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
fixtures/vitest-pool-workers-examples/module-resolution/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# ⚡ module-resolution | ||
|
||
This fixture demonstrates that the Vitest integration correctly resolves modules, including: | ||
|
||
- A CommonJS package that requires a directory rather than a specific file. | ||
- A package without a main entrypoint or with browser field mapping, handled via [Dependency Pre-Bundling](#dependency-pre-bundling). | ||
|
||
## Dependency Pre-Bundling | ||
|
||
[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: | ||
|
||
```ts | ||
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; | ||
|
||
export default defineWorkersConfig({ | ||
test: { | ||
deps: { | ||
optimizer: { | ||
ssr: { | ||
enabled: true, | ||
include: ["your-package-name"], | ||
}, | ||
}, | ||
}, | ||
poolOptions: { | ||
workers: { | ||
// ... | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
See our [vitest config](./vitest.config.ts) for an example of how we pre-bundled `discord-api-types/v10` and `@microlabs/otel-cf-workers`. |
6 changes: 6 additions & 0 deletions
6
.../external-package-resolution/src/index.ts → ...s-examples/module-resolution/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
fixtures/vitest-pool-workers-examples/module-resolution/test/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { instrument } from "@microlabs/otel-cf-workers"; | ||
import { Utils } from "discord-api-types/v10"; | ||
import dep from "ext-dep"; | ||
import { assert, describe, test } from "vitest"; | ||
|
||
describe("test", () => { | ||
test("resolves commonjs directory dependencies correctly", async () => { | ||
assert.equal(dep, 123); | ||
}); | ||
|
||
// This requires the `deps.optimizer` option to be set in the vitest config | ||
test("resolves dependency without a default entrypoint", async () => { | ||
assert.isFunction(Utils.isDMInteraction); | ||
}); | ||
|
||
// This requires the `deps.optimizer` option to be set in the vitest config | ||
test("resolves dependency with mapping on the browser field", async () => { | ||
assert.isFunction(instrument); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...external-package-resolution/wrangler.toml → ...-examples/module-resolution/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
name = "external-package-resolution" | ||
name = "module-resolution" | ||
main = "src/index.ts" | ||
compatibility_date = "2024-04-05" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ | |
"tree-kill": "^1.2.2", | ||
"turbo": "^2.2.3", | ||
"typescript": "catalog:default", | ||
"vite": "^5.0.12", | ||
"vite": "catalog:default", | ||
"vitest": "catalog:default" | ||
}, | ||
"packageManager": "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Node.js built-in modules provided by `workerd` | ||
export const workerdBuiltinModules = new Set([ | ||
...VITEST_POOL_WORKERS_DEFINE_BUILTIN_MODULES, | ||
"__STATIC_CONTENT_MANIFEST", | ||
]); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.