importmap

package module
v0.1.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.

disclaimer : There is still plenty of room for optimization. and the API might change during the early stages of development

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.

Usage:

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 without publish 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": "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"
        }
    }
</script>

Result in the following with publish 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",
            "json-enc": "assets/js/htmx/ext/json-enc.min.js"
        }
    }
</script>

Files generated will look like the following:

- .importmap
  - htmx
    - 1.8.4
      - ext
        - json-enc.min.js
    - 1.8.5
      - htmx.min.js
- assets
  - js
    - htmx
      - ext
        - json-enc.min.js
      - 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.

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
	Packages  []library.Package
	Structure 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