Skip to content

Commit

Permalink
fix bug and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
donseba committed Mar 8, 2023
1 parent 403de57 commit 1402684
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
73 changes: 52 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# go-importmap
Golang importmap generator.

disclaimer : There is still plenty of room for optimization. and the API might change during the early stages of development
Golang importmap generator and super lightweight javascript library and asset manager.

For now only cdnjs has been implemented because it provides a great api to interact with.
There is a `Raw` client as well that mimics the process and returns the Raw field of the package struct.
In addition to generate the importmap section it can also cache external libraries and serve them from local storage.

For now only cdnjs has been implemented because it provides a great api to interact with. There is a `Raw` client as well that mimics the process and returns the Raw field of the package struct.

## Example

Usage:
```go
package main

Expand Down Expand Up @@ -62,33 +63,39 @@ func main() {
fmt.Println(tmpl)
}
```
Result in the following `without` publish set to true:

Result in the following with `SetUseAssets` set to `false`:

```html
<script async src="https://ga.jspm.io/npm:es-module-shims@1.7.0/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"htmx": "https://cdnjs.cloudflare.com/ajax/libs/htmx/1.8.6/htmx.min.js",
"json-enc": "https://cdnjs.cloudflare.com/ajax/libs/htmx/1.8.6/ext/json-enc.min.js"
}
}
{
"imports": {
"htmx": "https://cdnjs.cloudflare.com/ajax/libs/htmx/1.8.5/htmx.min.js",
"htmx-latest": "https://unpkg.com/browse/htmx.org@1.8.6/dist/htmx.min.js",
"json-enc": "https://cdnjs.cloudflare.com/ajax/libs/htmx/1.8.4/ext/json-enc.min.js"
}
}
</script>
```

Result in the following `with` publish set to true:
Result in the following with `SetUseAssets` set to `true`:

```html
<script async src="https://ga.jspm.io/npm:es-module-shims@1.7.0/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"htmx": "assets/js/htmx/htmx.min.js",
"json-enc": "assets/js/htmx/ext/json-enc.min.js"
}
}
{
"imports": {
"htmx": "/assets/js/htmx/htmx.min.js",
"htmx-latest": "/assets/js/htmx-latest/htmx.min.js",
"json-enc": "/assets/js/htmx/ext/json-enc.min.js"
}
}
</script>

```

Files generated will look like the following:
Output will look like the following:

```
- .importmap
Expand All @@ -98,11 +105,35 @@ Files generated will look like the following:
- json-enc.min.js
- 1.8.5
- htmx.min.js
- htmx-latest
- 1.8.6
- htmx.min.js
- assets
- js
- htmx
- ext
- json-enc.min.js
- htmx.min.js
- htmx-latest
- htmx.min.js
```
as you can see the `.importmap` contains the files per version and we create the assets without a version this will allow you to `update` the file without having to update the snippet.

as you can see the `.importmap` contains the files per version and the assets are created without a version a version folder. This has been done by design so you don't have to update the snippet while doing an update.

## Variations

it is possible to bypass the cdnjs by using the `Raw` param on the package.

```go
im.Packages = []library.Package{
{
Name: "htmx",
Raw: "https://some.url.to/repo/with.js",
},
}
```

This wil generate:
```json
{"imports":{"htmx":"https://some.url.to/repo/with.js"}}
```
13 changes: 3 additions & 10 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@ import (
"context"
"fmt"
"log"
"os"
"path/filepath"

"github.com/donseba/go-importmap"
"github.com/donseba/go-importmap/client/cdnjs"
"github.com/donseba/go-importmap/library"
)

func main() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)

ctx := context.TODO()
pr := cdnjs.New()

im := importmap.New(pr)
im.SetUseAssets(true)
im.SetRootDir(exPath)

im.Packages = []library.Package{
{
Expand All @@ -44,12 +35,14 @@ func main() {
},
}

err = im.Fetch(ctx)
// retrieve all libraries
err := im.Fetch(ctx)
if err != nil {
log.Fatal(err)
return
}

// render the html block including script tags.
tmpl, err := im.Render()
if err != nil {
log.Fatal(err)
Expand Down
29 changes: 14 additions & 15 deletions importmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

var (
defaultAssetsDir = path.Join("useAssets", "js")
defaultAssetsPath = "/" + strings.Join([]string{"useAssets", "js"}, "/")
defaultAssetsDir = path.Join("assets", "js")
defaultAssetsPath = "/" + strings.Join([]string{"assets", "js"}, "/")
defaultCacheDir = ".importmap"
defaultShimSrc = "https://ga.jspm.io/npm:es-module-shims@1.7.0/dist/es-module-shims.js"
)
Expand All @@ -26,19 +26,17 @@ type (
}

ImportMap struct {
Provider Provider
Packages []library.Package
Structure structure

clean bool
useAssets bool
includeShim bool

assetsDir string
assetsPath string
cacheDir string
rootDir string
shimSrc string
Provider Provider // the js library provider
Packages []library.Package // the library packages we want to include
Structure structure // the output structure
clean bool // whether to clean cache and assets
useAssets bool // use local assets or not
includeShim bool // include shim to support older browsers
assetsDir string // assets directory
assetsPath string // path to assets in the URL
cacheDir string // cache directory
rootDir string // application root directory
shimSrc string // shim source in case the default one does not meet requirements.
}

structure struct {
Expand All @@ -56,6 +54,7 @@ func New(p Provider) *ImportMap {
shimSrc: defaultShimSrc,
includeShim: true,
clean: false,
useAssets: false,
Provider: p,
Structure: structure{
Imports: make(map[string]string),
Expand Down
4 changes: 2 additions & 2 deletions importmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestImportMapCache(t *testing.T) {
return
}

if string(out) != `{"imports":{"htmx":"/useAssets/js/htmx/htmx.min.js","json-enc":"/useAssets/js/htmx/htmx.min.js"}}` {
if string(out) != `{"imports":{"htmx":"/assets/js/htmx/htmx.min.js","json-enc":"/assets/js/htmx/htmx.min.js"}}` {
t.Error("json output mismatch")
return
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestImportMapRawPublish(t *testing.T) {
return
}

if string(out) != `{"imports":{"htmx":"/useAssets/js/htmx/htmx.min.js"}}` {
if string(out) != `{"imports":{"htmx":"/assets/js/htmx/htmx.min.js"}}` {
t.Error("json output mismatch")
return
}
Expand Down

0 comments on commit 1402684

Please sign in to comment.