Skip to content

Commit a05a2f9

Browse files
committed
css: make source index on compile results optional
1 parent bb16fb7 commit a05a2f9

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

internal/linker/linker.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5835,7 +5835,11 @@ type compileResultCSS struct {
58355835
// or the start of the file if this is the first CSS string.
58365836
generatedOffset sourcemap.LineColumnOffset
58375837

5838-
sourceIndex uint32
5838+
// The source index can be invalid for short snippets that aren't necessarily
5839+
// tied to any one file and/or that don't really need source mappings. The
5840+
// source index is really only valid for the compile result that contains the
5841+
// main contents of a file, which we try to only ever write out once.
5842+
sourceIndex ast.Index32
58395843
hasCharset bool
58405844
}
58415845

@@ -6007,7 +6011,7 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
60076011
LocalNames: c.mangledProps,
60086012
}
60096013
compileResult.PrintResult = css_printer.Print(asts[i], c.graph.Symbols, cssOptions)
6010-
compileResult.sourceIndex = sourceIndex
6014+
compileResult.sourceIndex = ast.MakeIndex32(sourceIndex)
60116015
waitGroup.Done()
60126016
}(i, entry.sourceIndex, &compileResults[i])
60136017
}
@@ -6149,19 +6153,19 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
61496153
var compileResultsForSourceMap []compileResultForSourceMap
61506154
var legalCommentList []legalCommentEntry
61516155
for _, compileResult := range compileResults {
6152-
if len(compileResult.ExtractedLegalComments) > 0 {
6156+
if len(compileResult.ExtractedLegalComments) > 0 && compileResult.sourceIndex.IsValid() {
61536157
legalCommentList = append(legalCommentList, legalCommentEntry{
6154-
sourceIndex: compileResult.sourceIndex,
6158+
sourceIndex: compileResult.sourceIndex.GetIndex(),
61556159
comments: compileResult.ExtractedLegalComments,
61566160
})
61576161
}
61586162

6159-
if c.options.Mode == config.ModeBundle && !c.options.MinifyWhitespace {
6163+
if c.options.Mode == config.ModeBundle && !c.options.MinifyWhitespace && compileResult.sourceIndex.IsValid() {
61606164
var newline string
61616165
if newlineBeforeComment {
61626166
newline = "\n"
61636167
}
6164-
comment := fmt.Sprintf("%s/* %s */\n", newline, c.graph.Files[compileResult.sourceIndex].InputFile.Source.PrettyPath)
6168+
comment := fmt.Sprintf("%s/* %s */\n", newline, c.graph.Files[compileResult.sourceIndex.GetIndex()].InputFile.Source.PrettyPath)
61656169
prevOffset.AdvanceString(comment)
61666170
j.AddString(comment)
61676171
}
@@ -6180,11 +6184,11 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
61806184
prevOffset = sourcemap.LineColumnOffset{}
61816185

61826186
// Include this file in the source map
6183-
if c.options.SourceMap != config.SourceMapNone {
6187+
if c.options.SourceMap != config.SourceMapNone && compileResult.sourceIndex.IsValid() {
61846188
compileResultsForSourceMap = append(compileResultsForSourceMap, compileResultForSourceMap{
61856189
sourceMapChunk: compileResult.SourceMapChunk,
61866190
generatedOffset: compileResult.generatedOffset,
6187-
sourceIndex: compileResult.sourceIndex,
6191+
sourceIndex: compileResult.sourceIndex.GetIndex(),
61886192
})
61896193
}
61906194
}
@@ -6223,12 +6227,18 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
62236227
}
62246228
chunk.jsonMetadataChunkCallback = func(finalOutputSize int) helpers.Joiner {
62256229
finalRelDir := c.fs.Dir(chunk.finalRelPath)
6230+
isFirst := true
62266231
for i, compileResult := range compileResults {
6227-
if i > 0 {
6232+
if !compileResult.sourceIndex.IsValid() {
6233+
continue
6234+
}
6235+
if isFirst {
6236+
isFirst = false
6237+
} else {
62286238
jMeta.AddString(",")
62296239
}
62306240
jMeta.AddString(fmt.Sprintf("\n %s: {\n \"bytesInOutput\": %d\n }",
6231-
helpers.QuoteForJSON(c.graph.Files[compileResult.sourceIndex].InputFile.Source.PrettyPath, c.options.ASCIIOnly),
6241+
helpers.QuoteForJSON(c.graph.Files[compileResult.sourceIndex.GetIndex()].InputFile.Source.PrettyPath, c.options.ASCIIOnly),
62326242
c.accurateFinalByteCount(pieces[i], finalRelDir)))
62336243
}
62346244
if len(compileResults) > 0 {

0 commit comments

Comments
 (0)