Skip to content

Commit

Permalink
avoid embedding additional files in scan phase
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 16, 2020
1 parent efb3350 commit 5bc08f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
10 changes: 1 addition & 9 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,7 @@ func ScanBundle(log logger.Log, fs fs.FS, res resolver.Resolver, entryPaths []st
fmt.Sprintf("Cannot use %q as a URL", otherFile.source.PrettyPath))

case *reprJS:
if otherRepr.ast.URLForCSS != "" {
// Inline the URL if the loader supports URLs
record.Path.Text = otherRepr.ast.URLForCSS
record.Path.Namespace = ""
record.SourceIndex = nil

// Copy the additional files to the output directory
result.file.additionalFiles = append(result.file.additionalFiles, otherFile.additionalFiles...)
} else {
if otherRepr.ast.URLForCSS == "" {
log.AddRangeError(&result.file.source, record.Range,
fmt.Sprintf("Cannot use %q as a URL", otherFile.source.PrettyPath))
}
Expand Down
26 changes: 24 additions & 2 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,14 +1008,36 @@ func (c *linkerContext) sortedCrossChunkExportItems(exportRefs map[js_ast.Ref]bo
func (c *linkerContext) scanImportsAndExports() {
// Step 1: Figure out what modules must be CommonJS
for _, sourceIndex := range c.reachableFiles {
if repr, ok := c.files[sourceIndex].repr.(*reprJS); ok {
file := &c.files[sourceIndex]
switch repr := file.repr.(type) {
case *reprCSS:
// We shouldn't need to clone this because it should be empty for CSS files
if file.additionalFiles != nil {
panic("Internal error")
}

// Inline URLs for non-CSS files into the CSS file
for importRecordIndex := range repr.ast.ImportRecords {
if record := &repr.ast.ImportRecords[importRecordIndex]; record.SourceIndex != nil {
otherFile := &c.files[*record.SourceIndex]
if otherRepr, ok := otherFile.repr.(*reprJS); ok {
record.Path.Text = otherRepr.ast.URLForCSS
record.Path.Namespace = ""
record.SourceIndex = nil

// Copy the additional files to the output directory
file.additionalFiles = append(file.additionalFiles, otherFile.additionalFiles...)
}
}
}

case *reprJS:
for importRecordIndex := range repr.ast.ImportRecords {
record := &repr.ast.ImportRecords[importRecordIndex]
if record.SourceIndex == nil {
continue
}

// Make sure the printer can require() CommonJS modules
otherFile := &c.files[*record.SourceIndex]
otherRepr := otherFile.repr.(*reprJS)

Expand Down

0 comments on commit 5bc08f5

Please sign in to comment.