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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ results in generating:
{"imports":{"htmx":"https://unpkg.com/browse/htmx.org@1.8.6/dist/htmx.min.js"}}
```

### jsdelivr ESM imports can be fetched using the jsdelivr provider:

```go
im := importmap.
NewDefaults().
WithProvider(cdnjs.New()).
AssetsDir(path.Join("assets")).
WithPackages([]library.Package{
{
Name: "htmx",
Version: "2.0.4",
Require: []library.Include{
{File: "htmx.esm.min.js"},
{File: "/ext/json-enc.js", As: "json-enc"},
},
},{
Provider: jsdelivr.NewESM(), // <!-- Use jsdelivr ESM provider -->
Name: "@simonwep/pickr",
Version: "1.9.1",
Require: []library.Include{
{File: "/esm-bundle.js", As: "pickr"}, // <!-- Get the ESM bundle -->
{File: "/dist/themes/nano.min.css", As: "nano.min.css"},
},
},
})
```

## Contributing

Contributions are welcome!
Expand Down
44 changes: 43 additions & 1 deletion client/jsdelivr/jsdelivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
Client struct {
apiBaseURL string
cdnBaseURL string
esm bool
}

SearchResponse struct {
Expand Down Expand Up @@ -58,9 +59,26 @@ func New() *Client {
return &Client{
apiBaseURL: defaultApiBaseURL,
cdnBaseURL: defaultCdnBaseURL,
esm: false,
}
}

// NewESM returns a new Client with ESM mode enabled
func NewESM() *Client {
return &Client{
apiBaseURL: defaultApiBaseURL,
cdnBaseURL: defaultCdnBaseURL,
esm: true,
}
}

// SetESM sets whether the client should use ESM mode
func (c *Client) SetESM(useESM bool) *Client {
c.esm = useESM
return c
}

// FetchPackageFiles retrieves package files from jsdelivr
func (c *Client) FetchPackageFiles(ctx context.Context, name, version string) (library.Files, string, error) {
url := defaultApiBaseURL + name

Expand Down Expand Up @@ -98,7 +116,7 @@ func (c *Client) FetchPackageFiles(ctx context.Context, name, version string) (l
}
}

// get all the files
// get all the files regardless of ESM mode - we need them for CSS and other file types
vUrl := fmt.Sprintf("%s%s@%s", defaultApiBaseURL, name, useVersion)

req, err = http.NewRequestWithContext(ctx, http.MethodGet, vUrl, nil)
Expand Down Expand Up @@ -131,6 +149,30 @@ func (c *Client) FetchPackageFiles(ctx context.Context, name, version string) (l

var files = walkFiles(pr.Files, basePath, "", hasDist)

// If ESM mode is enabled, we'll replace JS files with the ESM version
if c.esm {
// Create special ESM file with a unique LocalPath that will match any pattern
esmBasePath := c.cdnBaseURL + name + "@" + useVersion + "/+esm"

// Use a simpler approach - just add the ESM bundle with a standard path
esmFile := library.File{
Type: library.FileTypeJS,
Path: esmBasePath,
LocalPath: "/esm-bundle.js", // Simple filename without special characters
}

// Filter out JavaScript files, keeping only CSS and other files
var nonJSFiles library.Files
for _, file := range files {
if file.Type != library.FileTypeJS {
nonJSFiles = append(nonJSFiles, file)
}
}

// Combine the ESM file with non-JS files
files = append(library.Files{esmFile}, nonJSFiles...)
}

return files, useVersion, nil
}

Expand Down
Loading