-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(vitest-pool-workers): enable dependency pre-bundling #7810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6495797
feat(vitest-pool-workers): add vite pre-bundling support
edmundhung b6f02d7
add changeset
edmundhung 25a2dfa
merge external-package-resolution and internal-module-resolution exam…
edmundhung 641c544
improve error message
edmundhung 8382495
update error message
edmundhung 4d4a05c
share workerdBuiltinModules
edmundhung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.