diff --git a/internal/linker/debug.go b/internal/linker/debug.go index af7a45e0b94..04d0a394236 100644 --- a/internal/linker/debug.go +++ b/internal/linker/debug.go @@ -26,6 +26,7 @@ func (c *linkerContext) generateExtraDataForFileJS(sourceIndex uint32) string { file := &c.graph.Files[sourceIndex] repr := file.InputFile.Repr.(*graph.JSRepr) sb := strings.Builder{} + isFirstPartWithStmts := true quoteSym := func(ref ast.Ref) string { name := fmt.Sprintf("%d:%d [%s]", ref.SourceIndex, ref.InnerIndex, c.graph.Symbols.Get(ref).OriginalName) @@ -48,16 +49,23 @@ func (c *linkerContext) generateExtraDataForFileJS(sourceIndex uint32) string { } else if ast.MakeIndex32(uint32(partIndex)) == repr.Meta.WrapperPartIndex { sb.WriteString(`,"wrapperPartIndex":true`) } else if len(part.Stmts) > 0 { - start := part.Stmts[0].Loc.Start - end := len(file.InputFile.Source.Contents) + contents := file.InputFile.Source.Contents + start := int(part.Stmts[0].Loc.Start) + if isFirstPartWithStmts { + start = 0 + isFirstPartWithStmts = false + } + end := len(contents) if partIndex+1 < len(repr.AST.Parts) { if nextStmts := repr.AST.Parts[partIndex+1].Stmts; len(nextStmts) > 0 { - if nextStart := nextStmts[0].Loc.Start; nextStart >= start { + if nextStart := int(nextStmts[0].Loc.Start); nextStart >= start { end = int(nextStart) } } } - code = file.InputFile.Source.Contents[start:end] + start = moveBeforeExport(contents, start) + end = moveBeforeExport(contents, end) + code = contents[start:end] } // importRecords @@ -122,7 +130,7 @@ func (c *linkerContext) generateExtraDataForFileJS(sourceIndex uint32) string { // code sb.WriteString(`,"code":`) - sb.Write(helpers.QuoteForJSON(strings.TrimRight(code, "\n"), c.options.ASCIIOnly)) + sb.Write(helpers.QuoteForJSON(code, c.options.ASCIIOnly)) sb.WriteByte('}') } @@ -130,3 +138,11 @@ func (c *linkerContext) generateExtraDataForFileJS(sourceIndex uint32) string { return sb.String() } + +func moveBeforeExport(contents string, i int) int { + contents = strings.TrimRight(contents[:i], " \t\r\n") + if strings.HasSuffix(contents, "export") { + return len(contents) - 6 + } + return i +} diff --git a/internal/linker/linker.go b/internal/linker/linker.go index 2385735e835..5b06b8f7537 100644 --- a/internal/linker/linker.go +++ b/internal/linker/linker.go @@ -5891,9 +5891,9 @@ func (c *linkerContext) generateChunkJS(chunkIndex int, chunkWaitGroup *sync.Wai for _, output := range pieces[i] { count += c.accurateFinalByteCount(output, finalRelDir) } - jMeta.AddString(fmt.Sprintf("\n %s: {\n \"bytesInOutput\": %d\n }", + jMeta.AddString(fmt.Sprintf("\n %s: {\n \"bytesInOutput\": %d\n %s}", helpers.QuoteForJSON(c.graph.Files[sourceIndex].InputFile.Source.PrettyPath, c.options.ASCIIOnly), - count)) + count, c.generateExtraDataForFileJS(sourceIndex))) } if len(metaOrder) > 0 { jMeta.AddString("\n ") diff --git a/scripts/graph-debugger.html b/scripts/graph-debugger.html index 48f1cf84958..8e0f8409671 100644 --- a/scripts/graph-debugger.html +++ b/scripts/graph-debugger.html @@ -1,9 +1,4 @@ - + @@ -13,6 +8,16 @@ body { margin: 0; overflow: hidden; + font: 14px/20px sans-serif; + background: #fff; + color: #000; + } + + @media (prefers-color-scheme: dark) { + body { + background: #222; + color: #ddd; + } } canvas { @@ -22,10 +27,23 @@ width: 100%; height: 100%; } + + #instructions { + padding: 20px; + } + +
+ This is a debugger for some of esbuild's internals. To use: +
    +
  1. Set "debugVerboseMetafile = true"
  2. +
  3. Serve the esbuild repo over localhost
  4. +
  5. Visit this page with "#metafile=path/to/metafile.json"
  6. +
+