diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 index 81bc4e895..ff8263105 100644 --- a/ci/release/template/man/d2.1 +++ b/ci/release/template/man/d2.1 @@ -109,6 +109,9 @@ An appendix for tooltips and links is added to PNG exports since they are not in .Ns . .It Fl d , -debug Print debug logs. +.It Fl -img-cache Ar true +In watch mode, images used in icons are cached for subsequent compilations. This should be disabled if images might change +.Ns . .It Fl h , -help Print usage information and exit. .It Fl v , -version diff --git a/d2cli/main.go b/d2cli/main.go index 0db873bc6..9b5f1cc8a 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -67,6 +67,10 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { if err != nil { return err } + imgCacheFlag, err := ms.Opts.Bool("IMG_CACHE", "img-cache", "", true, "in watch mode, images used in icons are cached for subsequent compilations. This should be disabled if images might change.") + if err != nil { + return err + } layoutFlag := ms.Opts.String("D2_LAYOUT", "layout", "l", "dagre", `the layout engine used`) themeFlag, err := ms.Opts.Int64("D2_THEME", "theme", "t", 0, "the diagram theme ID") if err != nil { @@ -150,6 +154,9 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { if *debugFlag { ms.Env.Setenv("DEBUG", "1") } + if *imgCacheFlag { + ms.Env.Setenv("IMG_CACHE", "1") + } if *browserFlag != "" { ms.Env.Setenv("BROWSER", *browserFlag) } diff --git a/lib/imgbundler/imgbundler.go b/lib/imgbundler/imgbundler.go index d7df77851..526f3bf8d 100644 --- a/lib/imgbundler/imgbundler.go +++ b/lib/imgbundler/imgbundler.go @@ -151,8 +151,10 @@ func runWorkers(ctx context.Context, ms *xmain.State, svg []byte, imgs [][][]byt } func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([]byte, error) { - if hit, ok := imgCache.Load(string(href)); ok { - return hit.([]byte), nil + if ms.Env.Getenv("IMG_CACHE") == "1" { + if hit, ok := imgCache.Load(string(href)); ok { + return hit.([]byte), nil + } } var buf []byte var mimeType string @@ -175,7 +177,9 @@ func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([ b64 := base64.StdEncoding.EncodeToString(buf) out := []byte(fmt.Sprintf(`