11import assert from "node:assert" ;
22import * as fsp from "node:fs/promises" ;
33import * as path from "node:path" ;
4- import { prepareContainerImagesForDev } from "@cloudflare/containers-shared/src/images" ;
5- import { getDevContainerImageName } from "@cloudflare/containers-shared/src/knobs" ;
64import {
75 generateContainerBuildId ,
86 getContainerIdsByImageTags ,
9- isDockerfile ,
107} from "@cloudflare/containers-shared/src/utils" ;
118import { generateStaticRoutingRuleMatcher } from "@cloudflare/workers-shared/asset-worker/src/utils/rules-engine" ;
129import replace from "@rollup/plugin-replace" ;
@@ -29,7 +26,11 @@ import {
2926 kRequestType ,
3027 ROUTER_WORKER_NAME ,
3128} from "./constants" ;
32- import { getDockerPath , removeContainersByIds } from "./containers" ;
29+ import {
30+ getDockerPath ,
31+ prepareContainerImages ,
32+ removeContainersByIds ,
33+ } from "./containers" ;
3334import {
3435 addDebugToVitePrintUrls ,
3536 getDebugPathHtml ,
@@ -75,7 +76,6 @@ import type {
7576 ResolvedPluginConfig ,
7677 WorkerConfig ,
7778} from "./plugin-config" ;
78- import type { ContainerDevOptions } from "@cloudflare/containers-shared" ;
7979import type { StaticRouting } from "@cloudflare/workers-shared/utils/types" ;
8080import type { Unstable_RawConfig } from "wrangler" ;
8181
@@ -99,7 +99,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
9999
100100 const additionalModulePaths = new Set < string > ( ) ;
101101 const nodeJsCompatWarningsMap = new Map < WorkerConfig , NodeJsCompatWarnings > ( ) ;
102- const containerImageTagsSeen = new Set < string > ( ) ;
102+ let containerImageTagsSeen : Set < string > | undefined ;
103103 let runningContainerIds : Array < string > ;
104104
105105 return [
@@ -445,38 +445,25 @@ if (import.meta.hot) {
445445 }
446446
447447 if ( hasDevContainers ) {
448- assert (
449- containerBuildId ,
450- "Build ID should be set if containers are enabled and defined"
451- ) ;
452- // Assemble container options and build if necessary
453- const containerOptions = getContainerOptions (
454- entryWorkerConfig ,
455- containerBuildId
456- ) ;
457448 const dockerPath = getDockerPath ( ) ;
458449
459- if ( containerOptions ) {
460- // keep track of them so we can clean up later
461- for ( const container of containerOptions ) {
462- containerImageTagsSeen . add ( container . image_tag ) ;
463- }
464-
465- await prepareContainerImagesForDev ( {
466- dockerPath,
467- containerOptions,
468- onContainerImagePreparationStart : ( ) => { } ,
469- onContainerImagePreparationEnd : ( ) => { } ,
470- } ) ;
471- }
450+ containerImageTagsSeen = await prepareContainerImages ( {
451+ containersConfig : entryWorkerConfig . containers ,
452+ containerBuildId,
453+ isContainersEnabled : entryWorkerConfig . dev . enable_containers ,
454+ dockerPath,
455+ configPath : entryWorkerConfig . configPath ,
456+ } ) ;
472457
473458 // poll Docker every two seconds and update the list of ids of all
474459 // running containers
475460 const dockerPollIntervalId = setInterval ( async ( ) => {
476- runningContainerIds = await getContainerIdsByImageTags (
477- dockerPath ,
478- containerImageTagsSeen
479- ) ;
461+ if ( containerImageTagsSeen ?. size ) {
462+ runningContainerIds = await getContainerIdsByImageTags (
463+ dockerPath ,
464+ containerImageTagsSeen
465+ ) ;
466+ }
480467 } , 2000 ) ;
481468
482469 /*
@@ -563,14 +550,52 @@ if (import.meta.hot) {
563550 vitePreviewServer
564551 ) ;
565552
553+ // first Worker in the Array is always the entry Worker
554+ const entryWorkerConfig = resolvedPluginConfig . workers [ 0 ] ;
555+ const hasDevContainers =
556+ entryWorkerConfig ?. containers ?. length &&
557+ entryWorkerConfig . dev . enable_containers ;
558+ let containerBuildId : string | undefined ;
559+
560+ if ( hasDevContainers ) {
561+ containerBuildId = generateContainerBuildId ( ) ;
562+ }
563+
566564 miniflare = new Miniflare (
567- await getPreviewMiniflareOptions (
565+ await getPreviewMiniflareOptions ( {
568566 resolvedPluginConfig,
569567 vitePreviewServer,
570- inputInspectorPort
571- )
568+ inspectorPort : inputInspectorPort ,
569+ containerBuildId,
570+ } )
572571 ) ;
573572
573+ if ( hasDevContainers ) {
574+ const dockerPath = getDockerPath ( ) ;
575+
576+ containerImageTagsSeen = await prepareContainerImages ( {
577+ containersConfig : entryWorkerConfig . containers ,
578+ containerBuildId,
579+ isContainersEnabled : entryWorkerConfig . dev . enable_containers ,
580+ dockerPath,
581+ configPath : entryWorkerConfig . configPath ,
582+ } ) ;
583+
584+ const dockerPollIntervalId = setInterval ( async ( ) => {
585+ if ( containerImageTagsSeen ?. size ) {
586+ runningContainerIds = await getContainerIdsByImageTags (
587+ dockerPath ,
588+ containerImageTagsSeen
589+ ) ;
590+ }
591+ } , 2000 ) ;
592+
593+ process . on ( "exit" , ( ) => {
594+ clearInterval ( dockerPollIntervalId ) ;
595+ removeContainersByIds ( dockerPath , runningContainerIds ) ;
596+ } ) ;
597+ }
598+
574599 handleWebSocket ( vitePreviewServer . httpServer , ( ) => {
575600 assert ( miniflare , `Miniflare not defined` ) ;
576601
@@ -587,7 +612,10 @@ if (import.meta.hot) {
587612 ) ;
588613 } ,
589614 async buildEnd ( ) {
590- if ( resolvedViteConfig . command === "serve" ) {
615+ if (
616+ resolvedViteConfig . command === "serve" &&
617+ containerImageTagsSeen ?. size
618+ ) {
591619 const dockerPath = getDockerPath ( ) ;
592620 runningContainerIds = await getContainerIdsByImageTags (
593621 dockerPath ,
@@ -1037,42 +1065,4 @@ if (import.meta.hot) {
10371065 ? resolvedPluginConfig . workers [ environmentName ]
10381066 : undefined ;
10391067 }
1040-
1041- /**
1042- * @returns Container options suitable for building or pulling images,
1043- * with image tag set to well-known dev format, or undefined if
1044- * containers are not enabled or not configured.
1045- */
1046- function getContainerOptions (
1047- config : WorkerConfig ,
1048- containerBuildId : string
1049- ) : ContainerDevOptions [ ] | undefined {
1050- if ( ! config . containers ?. length || config . dev . enable_containers === false ) {
1051- return undefined ;
1052- }
1053- return config . containers . map ( ( container ) => {
1054- if ( isDockerfile ( container . image , config . configPath ) ) {
1055- return {
1056- dockerfile : container . image ,
1057- image_build_context :
1058- container . image_build_context ?? path . dirname ( container . image ) ,
1059- image_vars : container . image_vars ,
1060- class_name : container . class_name ,
1061- image_tag : getDevContainerImageName (
1062- container . class_name ,
1063- containerBuildId
1064- ) ,
1065- } ;
1066- } else {
1067- return {
1068- image_uri : container . image ,
1069- class_name : container . class_name ,
1070- image_tag : getDevContainerImageName (
1071- container . class_name ,
1072- containerBuildId
1073- ) ,
1074- } ;
1075- }
1076- } ) ;
1077- }
10781068}
0 commit comments