diff --git a/README.md b/README.md index cc6074c..c2fb9b8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` -Result in the following `with` publish set to true: +Result in the following with `SetUseAssets` set to `true`: + ```html + ``` -Files generated will look like the following: +Output will look like the following: ``` - .importmap @@ -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. \ No newline at end of file + +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"}} +``` \ No newline at end of file diff --git a/example/main.go b/example/main.go index 6b40a92..cc00193 100644 --- a/example/main.go +++ b/example/main.go @@ -4,8 +4,6 @@ import ( "context" "fmt" "log" - "os" - "path/filepath" "github.com/donseba/go-importmap" "github.com/donseba/go-importmap/client/cdnjs" @@ -13,18 +11,11 @@ import ( ) 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{ { @@ -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) diff --git a/importmap.go b/importmap.go index 46cf5e1..e1e1ae7 100644 --- a/importmap.go +++ b/importmap.go @@ -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" ) @@ -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 { @@ -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), diff --git a/importmap_test.go b/importmap_test.go index 10f364a..f559db3 100644 --- a/importmap_test.go +++ b/importmap_test.go @@ -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 } @@ -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 }