11import assert from "node:assert" ;
22import * as util from "node:util" ;
3- import {
4- cleanupContainers ,
5- generateContainerBuildId ,
6- resolveDockerHost ,
7- } from "@cloudflare/containers-shared/src/utils" ;
3+ import { prepareContainerImagesForDev } from "@cloudflare/containers-shared" ;
4+ import { cleanupContainers } from "@cloudflare/containers-shared/src/utils" ;
85import { generateStaticRoutingRuleMatcher } from "@cloudflare/workers-shared/asset-worker/src/utils/rules-engine" ;
96import { CoreHeaders , Miniflare } from "miniflare" ;
107import colors from "picocolors" ;
@@ -21,7 +18,7 @@ import {
2118 kRequestType ,
2219 ROUTER_WORKER_NAME ,
2320} from "./constants" ;
24- import { getDockerPath , prepareContainerImages } from "./containers" ;
21+ import { getDockerPath } from "./containers" ;
2522import {
2623 addDebugToVitePrintUrls ,
2724 DEBUG_PATH ,
@@ -234,23 +231,14 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
234231 const entryWorkerConfig = getEntryWorkerConfig (
235232 ctx . resolvedPluginConfig
236233 ) ;
237- const hasDevContainers =
238- entryWorkerConfig ?. containers ?. length &&
239- entryWorkerConfig . dev . enable_containers ;
240- const dockerPath = getDockerPath ( ) ;
241-
242- if ( hasDevContainers ) {
243- containerBuildId = generateContainerBuildId ( ) ;
244- entryWorkerConfig . dev . container_engine =
245- resolveDockerHost ( dockerPath ) ;
246- }
247234
248- const miniflareDevOptions = await getDevMiniflareOptions ( {
249- resolvedPluginConfig : ctx . resolvedPluginConfig ,
250- viteDevServer,
251- inspectorPort : inputInspectorPort ,
252- containerBuildId,
253- } ) ;
235+ const { config : miniflareDevOptions , allContainerOptions } =
236+ await getDevMiniflareOptions ( {
237+ resolvedPluginConfig : ctx . resolvedPluginConfig ,
238+ viteDevServer,
239+ inspectorPort : inputInspectorPort ,
240+ containerBuildId,
241+ } ) ;
254242
255243 if ( ! miniflare ) {
256244 debuglog ( "Creating new Miniflare instance" ) ;
@@ -318,21 +306,22 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
318306 } ;
319307 }
320308
321- if ( hasDevContainers ) {
309+ if ( allContainerOptions . size > 0 ) {
322310 viteDevServer . config . logger . info (
323311 colors . dim (
324312 colors . yellow (
325313 "∷ Building container images for local development...\n"
326314 )
327315 )
328316 ) ;
329- containerImageTagsSeen = await prepareContainerImages ( {
330- containersConfig : entryWorkerConfig . containers ,
331- containerBuildId,
332- isContainersEnabled : entryWorkerConfig . dev . enable_containers ,
333- dockerPath,
334- configPath : entryWorkerConfig . configPath ,
317+ await prepareContainerImagesForDev ( {
318+ dockerPath : getDockerPath ( ) ,
319+ containerOptions : [ ...allContainerOptions . values ( ) ] ,
320+ onContainerImagePreparationStart : ( ) => { } ,
321+ onContainerImagePreparationEnd : ( ) => { } ,
335322 } ) ;
323+
324+ containerImageTagsSeen = new Set ( allContainerOptions . keys ( ) ) ;
336325 viteDevServer . config . logger . info (
337326 colors . dim (
338327 colors . yellow (
@@ -356,8 +345,8 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
356345 *
357346 */
358347 process . on ( "exit" , async ( ) => {
359- if ( containerImageTagsSeen . size ) {
360- cleanupContainers ( dockerPath , containerImageTagsSeen ) ;
348+ if ( allContainerOptions . size > 0 ) {
349+ cleanupContainers ( getDockerPath ( ) , containerImageTagsSeen ) ;
361350 }
362351 } ) ;
363352 }
@@ -415,27 +404,15 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
415404 vitePreviewServer
416405 ) ;
417406
418- // first Worker in the Array is always the entry Worker
419- const entryWorkerConfig = ctx . resolvedPluginConfig . workers [ 0 ] ;
420- const hasDevContainers =
421- entryWorkerConfig ?. containers ?. length &&
422- entryWorkerConfig . dev . enable_containers ;
423- let containerBuildId : string | undefined ;
424-
425- if ( hasDevContainers ) {
426- containerBuildId = generateContainerBuildId ( ) ;
427- }
428-
429- miniflare = new Miniflare (
407+ const { config : miniflareOptions , allContainerOptions } =
430408 await getPreviewMiniflareOptions ( {
431409 resolvedPluginConfig : ctx . resolvedPluginConfig ,
432410 vitePreviewServer,
433411 inspectorPort : inputInspectorPort ,
434- containerBuildId,
435- } )
436- ) ;
412+ } ) ;
413+ miniflare = new Miniflare ( miniflareOptions ) ;
437414
438- if ( hasDevContainers ) {
415+ if ( allContainerOptions . size > 0 ) {
439416 const dockerPath = getDockerPath ( ) ;
440417
441418 vitePreviewServer . config . logger . info (
@@ -445,13 +422,14 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
445422 )
446423 )
447424 ) ;
448- containerImageTagsSeen = await prepareContainerImages ( {
449- containersConfig : entryWorkerConfig . containers ,
450- containerBuildId,
451- isContainersEnabled : entryWorkerConfig . dev . enable_containers ,
452- dockerPath,
453- configPath : entryWorkerConfig . configPath ,
425+ await prepareContainerImagesForDev ( {
426+ dockerPath : getDockerPath ( ) ,
427+ containerOptions : [ ...allContainerOptions . values ( ) ] ,
428+ onContainerImagePreparationStart : ( ) => { } ,
429+ onContainerImagePreparationEnd : ( ) => { } ,
454430 } ) ;
431+ containerImageTagsSeen = new Set ( allContainerOptions . keys ( ) ) ;
432+
455433 vitePreviewServer . config . logger . info (
456434 colors . dim ( colors . yellow ( "\n⚡️ Containers successfully built.\n" ) )
457435 ) ;
0 commit comments