importmap

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 9 Imported by: 0

README

go-importmap

Golang importmap generator and super lightweight javascript library and asset manager.

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

package main

import (
	"context"
	"fmt"
	"log"

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

func main() {
	ctx := context.TODO()
	pr := cdnjs.New()

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

	im.Packages = []library.Package{
		{
			Name:    "htmx",
			Version: "1.8.5",
		},
		{
			Name:     "htmx",
			Version:  "1.8.4",
			As:       "json-enc",
			FileName: "ext/json-enc.min.js",
		},
		{
			Name:    "htmx-latest",
			Version: "1.8.6",
			Raw:     "https://unpkg.com/browse/htmx.org@1.8.6/dist/htmx.min.js",
		},
	}

	// 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)
		return
	}

	fmt.Println(tmpl)
}

Result in the following with SetUseAssets set to false:

<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.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 SetUseAssets set to true:

<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",
    "htmx-latest": "/assets/js/htmx-latest/htmx.min.js",
    "json-enc": "/assets/js/htmx/ext/json-enc.min.js"
  }
}
</script>

Output will look like the following:

- .importmap
  - htmx
    - 1.8.4
      - ext
        - 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 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.

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

This wil generate:

	{"imports":{"htmx":"https://some.url.to/repo/with.js"}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ImportMap

type ImportMap struct {
	Provider  Provider          // the js library provider
	Packages  []library.Package // the library packages we want to include
	Structure structure         // the output structure
	// contains filtered or unexported fields
}

func New

func New(p Provider) *ImportMap

New returns a new instance of the ImportMap

func (*ImportMap) Fetch

func (im *ImportMap) Fetch(ctx context.Context) error

Fetch retrieves all packages locally or remotely

func (*ImportMap) GetShim

func (im *ImportMap) GetShim() string

GetShim gets the path to the shim

func (*ImportMap) Imports

func (im *ImportMap) Imports() (template.HTML, error)

Imports returns the structure in HTML.

func (*ImportMap) IncludeShim

func (im *ImportMap) IncludeShim() bool

IncludeShim returns includeShim

func (*ImportMap) Marshal

func (im *ImportMap) Marshal() ([]byte, error)

Marshal returns the Structure as JSON.

func (*ImportMap) MarshalIndent

func (im *ImportMap) MarshalIndent() ([]byte, error)

MarshalIndent returns the Structure as JSON in a pretty form.

func (*ImportMap) Render

func (im *ImportMap) Render() (template.HTML, error)

Render returns an HTML snippet to use in a template

func (*ImportMap) RootDir

func (im *ImportMap) RootDir() string

RootDir retrieves the root dir

func (*ImportMap) SetAssetsDir

func (im *ImportMap) SetAssetsDir(d string)

SetAssetsDir sets assetsDir to the desired value

func (*ImportMap) SetAssetsPath

func (im *ImportMap) SetAssetsPath(d string)

SetAssetsPath sets assetsPath to the desired value

func (*ImportMap) SetCacheDir

func (im *ImportMap) SetCacheDir(d string)

SetCacheDir sets the path used for cache

func (*ImportMap) SetClean

func (im *ImportMap) SetClean(b bool)

SetClean sets the flag to do a clean run

func (*ImportMap) SetIncludeShim

func (im *ImportMap) SetIncludeShim(b bool)

SetIncludeShim sets the flag to include the shim

func (*ImportMap) SetRootDir

func (im *ImportMap) SetRootDir(d string)

SetRootDir sets the root dir to use ase base for both cache and useAssets directory

func (*ImportMap) SetShimSrc

func (im *ImportMap) SetShimSrc(s string)

SetShimSrc sets the source of the shim

func (*ImportMap) SetUseAssets

func (im *ImportMap) SetUseAssets(b bool)

SetUseAssets sets the flag to useAssets the files locally

type Provider

type Provider interface {
	Package(ctx context.Context, p *library.Package) (string, error)
}

Directories

Path Synopsis
client
raw

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL