Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/vite-plugin-cloudflare/src/cloudflare-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ const cloudflareBuiltInModules = [
"cloudflare:workflows",
];

const nodeBuiltInModules = builtinModules.concat(
builtinModules.map((m) => `node:${m}`)
);

const defaultConditions = ["workerd", "module", "browser"];

export function createCloudflareEnvironmentOptions(
Expand All @@ -133,6 +137,10 @@ export function createCloudflareEnvironmentOptions(
noExternal: true,
// We want to use `workerd` package exports if available (e.g. for postgres).
conditions: [...defaultConditions, "development|production"],
// The Cloudflare ones are proper builtins in the environment
builtins: [...cloudflareBuiltInModules],
// The Node.js ones are no proper builtins in the environment since we also polyfill them using unenv
external: [...nodeBuiltInModules],
},
dev: {
createEnvironment(name, config) {
Expand All @@ -155,17 +163,15 @@ export function createCloudflareEnvironmentOptions(
// dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
// optimizeDeps.entries in the dev config)
input: workerConfig.main,
external: [...cloudflareBuiltInModules],
},
},
optimizeDeps: {
// Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false
noDiscovery: false,
entries: workerConfig.main,
exclude: [
...cloudflareBuiltInModules,
// we have to exclude all node modules to work in dev-mode not just the unenv externals...
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
...nodeBuiltInModules,
],
esbuildOptions: {
platform: "neutral",
Expand Down
25 changes: 12 additions & 13 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert";
import * as fs from "node:fs";
import * as fsp from "node:fs/promises";
import { builtinModules } from "node:module";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { Log, LogLevel, Response as MiniflareResponse } from "miniflare";
Expand All @@ -25,6 +26,10 @@ import type {
import type { MiniflareOptions, SharedOptions, WorkerOptions } from "miniflare";
import type { FetchFunctionOptions } from "vite/module-runner";

const nodeBuiltInModules = new Set(
builtinModules.concat(builtinModules.map((m) => `node:${m}`))
);

type PersistOptions = Pick<
SharedOptions,
| "cachePersist"
Expand Down Expand Up @@ -328,22 +333,16 @@ export function getDevMiniflareOptions(
const [moduleId] = invokePayloadData.data;
const moduleRE = new RegExp(MODULE_PATTERN);

// Externalize Worker modules (CompiledWasm, Text, Data)
if (moduleRE.test(moduleId)) {
const result = {
externalize: moduleId,
type: "module",
} satisfies vite.FetchResult;
const shouldExternalize =
// Worker modules (CompiledWasm, Text, Data)
moduleRE.test(moduleId) ||
// Node.js builtin node modules (they will be resolved to unenv aliases)
nodeBuiltInModules.has(moduleId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Know we've discussed it before but it still doesn't really make sense to me that we have to externalize things again here. Agree this is correct for now though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I 100% totally agree 👍

I'll go and investigate this 👍 (but yeah for this PR I would keep it as is since this is also effects the other modules)


return MiniflareResponse.json({ result });
}

// For some reason we need this here for cloudflare built-ins (e.g. `cloudflare:workers`) but not for node built-ins (e.g. `node:path`)
// See https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/46
if (moduleId.startsWith("cloudflare:")) {
if (shouldExternalize) {
const result = {
externalize: moduleId,
type: "builtin",
type: "module",
} satisfies vite.FetchResult;

return MiniflareResponse.json({ result });
Expand Down
Loading