Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion importmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ func (im *ImportMap) CacheOrFetch(ctx context.Context) error {
} else {
as = file.LocalPath
}
im.Structure.Imports[as] = file.Path

switch file.Type {
case library.FileTypeCSS:
im.Structure.Styles[as] = file.Path
case library.FileTypeJS:
im.Structure.Imports[as] = file.Path
}
}
}
return nil
Expand Down
48 changes: 32 additions & 16 deletions library/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,30 +153,46 @@ func (p *Package) Assets(assetsDir string, filePath string) (Files, string, erro
return nil, "", fmt.Errorf("asset file %s does not exist", fullPath)
}

// Use a recursive helper function to traverse directories
return p.getFilesRecursively(fullPath, baseDir, filePath)
}

// getFilesRecursively will scan a directory and all its subdirectories for files
func (p *Package) getFilesRecursively(fullPath, baseDir, relativePath string) (Files, string, error) {
files, err := os.ReadDir(fullPath)
if err != nil {
return nil, "", fmt.Errorf("failed to read assets directory %s: %w", fullPath, err)
return nil, "", fmt.Errorf("failed to read directory %s: %w", fullPath, err)
}

var assetFiles Files
for _, file := range files {
if file.IsDir() {
continue // Skip directories
}
// Create the current file/directory path
currentRelativePath := path.Join(relativePath, file.Name())
currentFullPath := path.Join(fullPath, file.Name())

assetPath := path.Join(baseDir, filePath, file.Name())
localPath := path.Join(filePath, file.Name())

// Ensure assetPath has a leading slash for URL usage
if !strings.HasPrefix(assetPath, "/") {
assetPath = "/" + assetPath
if file.IsDir() {
// Recursively process subdirectory
subFiles, _, err := p.getFilesRecursively(currentFullPath, baseDir, currentRelativePath)
if err != nil {
return nil, "", err
}
// Add files from subdirectory to our list
assetFiles = append(assetFiles, subFiles...)
} else {
// It's a file, add it to our list
assetPath := path.Join(baseDir, currentRelativePath)

// Ensure assetPath has a leading slash for URL usage
if !strings.HasPrefix(assetPath, "/") {
assetPath = "/" + assetPath
}

assetFiles = append(assetFiles, File{
Path: assetPath,
LocalPath: currentRelativePath,
Type: ExtractFileType(file.Name()),
})
}

assetFiles = append(assetFiles, File{
Path: assetPath,
LocalPath: localPath,
Type: ExtractFileType(file.Name()),
})
}

return assetFiles, baseDir, nil
Expand Down
Loading