Skip to content

Commit

Permalink
fix: do not try to load in @cloudflare/workers-shared if not using ex…
Browse files Browse the repository at this point in the history
…perimental-assets (#6432)
  • Loading branch information
petebacondarwin authored Aug 6, 2024
1 parent 362bf34 commit cba2e25
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-lizards-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: prevent crash when running wrangler dev due to missing dependency
2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"dependencies": {
"@cloudflare/kv-asset-handler": "workspace:*",
"@cloudflare/workers-shared": "workspace:*",
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
"blake3-wasm": "^2.1.5",
Expand All @@ -92,7 +93,6 @@
"@cloudflare/eslint-config-worker": "workspace:*",
"@cloudflare/pages-shared": "workspace:^",
"@cloudflare/types": "^6.18.4",
"@cloudflare/workers-shared": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20240725.0",
"@cspotcode/source-map-support": "0.8.1",
Expand Down
85 changes: 45 additions & 40 deletions packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,43 +842,6 @@ export async function buildMiniflareOptions(
}
}

const assetServerModulePath = require.resolve(
"@cloudflare/workers-shared/dist/asset-server-worker.mjs"
);
const assetServerConfigPath = require.resolve(
"@cloudflare/workers-shared/asset-server-worker/wrangler.toml"
);
let assetServerConfig: Config | undefined;

try {
assetServerConfig = readConfig(assetServerConfigPath, {});
} catch (err) {
throw new UserError(
"Failed to read the Asset Server Worker configuration file.\n" + `${err}`
);
}

const assetServerWorker: WorkerOptions | undefined = config.experimentalAssets
? {
name: assetServerConfig?.name,
compatibilityDate: assetServerConfig?.compatibility_date,
compatibilityFlags: assetServerConfig?.compatibility_flags,
modulesRoot: dirname(assetServerModulePath),
modules: [
{
type: "ESModule",
path: assetServerModulePath,
},
],
unsafeDirectSockets: [
{
host: "127.0.0.1",
port: 0,
},
],
}
: undefined;

const upstream =
typeof config.localUpstream === "string"
? `${config.upstreamProtocol}://${config.localUpstream}`
Expand Down Expand Up @@ -921,15 +884,57 @@ export async function buildMiniflareOptions(
proxy: true,
})),
},
...(config.experimentalAssets
? [assetServerWorker as WorkerOptions]
: []),
...getAssetServerWorker(config),
...externalWorkers,
],
};
return { options, internalObjects, entrypointNames };
}

function getAssetServerWorker(
config: Omit<ConfigBundle, "rules">
): WorkerOptions[] {
if (!config.experimentalAssets) {
return [];
}
const assetServerModulePath = require.resolve(
"@cloudflare/workers-shared/dist/asset-server-worker.mjs"
);
const assetServerConfigPath = require.resolve(
"@cloudflare/workers-shared/asset-server-worker/wrangler.toml"
);
let assetServerConfig: Config | undefined;

try {
assetServerConfig = readConfig(assetServerConfigPath, {});
} catch (err) {
throw new UserError(
"Failed to read the Asset Server Worker configuration file.\n" + `${err}`
);
}

return [
{
name: assetServerConfig?.name,
compatibilityDate: assetServerConfig?.compatibility_date,
compatibilityFlags: assetServerConfig?.compatibility_flags,
modulesRoot: dirname(assetServerModulePath),
modules: [
{
type: "ESModule",
path: assetServerModulePath,
},
],
unsafeDirectSockets: [
{
host: "127.0.0.1",
port: 0,
},
],
},
];
}

export interface ReloadedEventOptions {
url: URL;
internalDurableObjects: CfDurableObject[];
Expand Down
6 changes: 3 additions & 3 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 cba2e25

Please sign in to comment.