MCPcopy
hub / github.com/esm-dev/esm.sh / FormatJSON

Method FormatJSON

internal/importmap/importmap.go:482–567  ·  view source on GitHub ↗

FormatJSON formats the import map as a JSON string.

(indent int)

Source from the content-addressed store, hash-verified

480
481// FormatJSON formats the import map as a JSON string.
482func (im *ImportMap) FormatJSON(indent int) string {
483 buf := strings.Builder{}
484 indentStr := bytes.Repeat([]byte{' ', ' '}, indent+1)
485 buf.Write(indentStr[0 : 2*indent])
486 buf.WriteString("{\n")
487 if cf := im.config; cf.CDN != "" || cf.Target != "" {
488 buf.Write(indentStr)
489 buf.WriteString("\"config\": {\n")
490 if cf.CDN != "" {
491 buf.Write(indentStr)
492 buf.WriteString(" \"cdn\": \"")
493 buf.WriteString(cf.CDN)
494 buf.WriteString("\"")
495 if cf.Target != "" {
496 buf.WriteString(",\n")
497 } else {
498 buf.WriteByte('\n')
499 }
500 }
501 if cf.Target != "" {
502 buf.Write(indentStr)
503 buf.WriteString(" \"target\": \"")
504 buf.WriteString(cf.Target)
505 buf.WriteString("\"\n")
506 }
507 buf.Write(indentStr)
508 buf.WriteString("},\n")
509 }
510 buf.Write(indentStr)
511 buf.WriteString("\"imports\": {")
512 if im.Imports.Len() > 0 {
513 buf.WriteByte('\n')
514 formatImports(&buf, im.Imports, indent+2)
515 buf.Write(indentStr)
516 buf.WriteByte('}')
517 } else {
518 buf.WriteByte('}')
519 }
520 scopes := make([]string, 0, len(im.scopes))
521 if len(im.scopes) > 0 {
522 for key, imports := range im.scopes {
523 if imports.Len() > 0 {
524 scopes = append(scopes, key)
525 }
526 }
527 }
528 sort.Strings(scopes)
529 if len(scopes) > 0 {
530 buf.WriteString(",\n")
531 buf.Write(indentStr)
532 buf.WriteString("\"scopes\": {\n")
533 i := 0
534 for _, scope := range scopes {
535 imports, _ := im.GetScopeImports(scope)
536 if imports.Len() == 0 {
537 continue
538 }
539 buf.Write(indentStr)

Callers 4

updateImportMapFunction · 0.95
MarshalJSONMethod · 0.95
tidyFunction · 0.80
ServeHtmlMethod · 0.80

Calls 6

GetScopeImportsMethod · 0.95
formatImportsFunction · 0.85
formatMapFunction · 0.85
WriteMethod · 0.80
LenMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected