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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
.idea/
example/assets/
example/.importmap/
.importmap
assets
8 changes: 8 additions & 0 deletions client/cdnjs/cdnjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,13 @@ func (c *Client) FetchPackageFiles(ctx context.Context, name, version string) (l
}
}

if len(files) == 0 && sr.Filename != "" {
files = append(files, library.File{
Type: library.ExtractFileType(sr.Filename),
Path: basePath + sr.Filename,
LocalPath: sr.Filename,
})
}

return files, useVersion, nil
}
7 changes: 5 additions & 2 deletions importmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ func (im *ImportMap) Fetch(ctx context.Context) error {
}

if im.cacheDir != nil && !pkg.HasCache(im.rootDir, *im.cacheDir) {
fmt.Println("building cache")
if im.logger != nil {
im.logger.InfoContext(ctx, "building cache", "package", pkg.Name, "version", pkg.Version)
}

for _, file := range allFiles {
err = pkg.MakeCache(im.rootDir, *im.cacheDir, file.LocalPath, file.Path)
if err != nil {
Expand Down Expand Up @@ -183,7 +186,7 @@ func (im *ImportMap) Fetch(ctx context.Context) error {
}

assetFiles = append(assetFiles, library.Include{
File: path.Join(im.rootDir, *im.assetsDir, file.LocalPath),
File: path.Join(im.rootDir, pkg.AssetsDir(*im.assetsDir), file.LocalPath),
As: as,
})
} else {
Expand Down
17 changes: 10 additions & 7 deletions importmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestImportMapWithLocalAssets(t *testing.T) {
return
}

if string(out) != `{"imports":{"bootstrap":"/assets/js/bootstrap.min.js","htmx":"/assets/htmx.min.js","json-enc":"/assets/ext/json-enc.js"}}` {
if string(out) != `{"imports":{"bootstrap":"/assets/bootstrap/js/bootstrap.min.js","htmx":"/assets/htmx/htmx.min.js","json-enc":"/assets/htmx/ext/json-enc.js"}}` {
t.Log(out)
t.Error("json output mismatch")
return
}
Expand All @@ -67,7 +68,8 @@ func TestImportMapWithLocalAssets(t *testing.T) {
return
}

if string(outStyles) != `<link rel="stylesheet" href="/assets/css/bootstrap.min.css" as="bootstrap">` {
if string(outStyles) != `<link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css" as="bootstrap">` {
t.Log(outStyles)
t.Error("json output mismatch")
return
}
Expand All @@ -78,14 +80,15 @@ func TestImportMapWithLocalAssets(t *testing.T) {
return
}

t.Log(full)
if string(full) != `<link rel="stylesheet" href="/assets/css/bootstrap.min.css" as="bootstrap"/>
if string(full) != `<link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css" as="bootstrap"/>
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"bootstrap": "/assets/js/bootstrap.min.js",
"htmx": "/assets/htmx.min.js",
"json-enc": "/assets/ext/json-enc.js"
"imports": {
"bootstrap": "/assets/bootstrap/js/bootstrap.min.js",
"htmx": "/assets/htmx/htmx.min.js",
"json-enc": "/assets/htmx/ext/json-enc.js"
}
}
</script>` {
t.Error("json output mismatch")
Expand Down
12 changes: 4 additions & 8 deletions library/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,13 @@ func (p *Package) MakeCache(rootDir string, cacheDir string, filePath string, sr
}

// AssetsDir returns the assets dir for the current package, we will store all files in here
func (p *Package) AssetsDir(cacheDir string) string {
version := "latest"
if p.Version != "" {
version = p.Version
}
return path.Join(cacheDir, p.Name, version)
func (p *Package) AssetsDir(assets string) string {
return path.Join(assets, p.Name)
}

// HasAssets checks if the package has assets on disk
func (p *Package) HasAssets(rootDir string, assetsDir string) bool {
fullPath := path.Join(rootDir, p.CacheDir(assetsDir))
fullPath := path.Join(rootDir, p.AssetsDir(assetsDir))

if _, err := os.Stat(fullPath); errors.Is(err, os.ErrNotExist) {
return false
Expand All @@ -116,7 +112,7 @@ func (p *Package) HasAssets(rootDir string, assetsDir string) bool {
}

func (p *Package) HasAssetFile(rootDir string, assetsDir string, filePath string) bool {
fullPath := path.Join(rootDir, p.CacheDir(assetsDir), filePath)
fullPath := path.Join(rootDir, p.AssetsDir(assetsDir), filePath)

if _, err := os.Stat(fullPath); errors.Is(err, os.ErrNotExist) {
return false
Expand Down