Skip to content

Commit

Permalink
merge external-package-resolution and internal-module-resolution exam…
Browse files Browse the repository at this point in the history
…ple with guidelines on readme
  • Loading branch information
edmundhung committed Feb 7, 2025
1 parent dc74937 commit b73d6cf
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 52 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 34 additions & 0 deletions fixtures/vitest-pool-workers-examples/module-resolution/README.md
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`.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Toucan } from "toucan-js";
// Test pre-bundling dependency relies on exports field without a default entrypoint
// Testing dependency without a main entrypoint
// @see https://github.com/cloudflare/workers-sdk/issues/6591
import "discord-api-types/v10";
// Test pre-bundling dependencies with browser field mapping
// Testing dependency with browser field mapping
// @see https://github.com/cloudflare/workers-sdk/issues/6581
import "@microlabs/otel-cf-workers";

Expand Down
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);
});
});
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"
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@microlabs/otel-cf-workers": "1.0.0-rc.45",
"@types/node": "catalog:default",
"discord-api-types": "0.37.98",
"ext-dep": "file:./internal-module-resolution/vendor/ext-dep",
"ext-dep": "file:./module-resolution/vendor/ext-dep",
"jose": "^5.2.2",
"miniflare": "workspace:*",
"run-script-os": "^1.1.6",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b73d6cf

Please sign in to comment.