diff --git a/.changeset/quiet-lizards-play.md b/.changeset/quiet-lizards-play.md new file mode 100644 index 000000000000..d61b99b0c51b --- /dev/null +++ b/.changeset/quiet-lizards-play.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +fix: prevent crash when running wrangler dev due to missing dependency diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 1ddb4018d3db..3034f5591874 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -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", @@ -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", diff --git a/packages/wrangler/src/dev/miniflare.ts b/packages/wrangler/src/dev/miniflare.ts index 674cc8fd5404..e52149c04124 100644 --- a/packages/wrangler/src/dev/miniflare.ts +++ b/packages/wrangler/src/dev/miniflare.ts @@ -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}` @@ -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 +): 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[]; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1dc7fa6be6ab..41cd79cad0f3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1509,6 +1509,9 @@ importers: '@cloudflare/kv-asset-handler': specifier: workspace:* version: link:../kv-asset-handler + '@cloudflare/workers-shared': + specifier: workspace:* + version: link:../workers-shared '@esbuild-plugins/node-globals-polyfill': specifier: ^0.2.3 version: 0.2.3(esbuild@0.17.19) @@ -1574,9 +1577,6 @@ importers: '@cloudflare/types': specifier: ^6.18.4 version: 6.18.4(react@18.2.0) - '@cloudflare/workers-shared': - specifier: workspace:* - version: link:../workers-shared '@cloudflare/workers-tsconfig': specifier: workspace:* version: link:../workers-tsconfig