-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewriting importmap to support css files.
- Loading branch information
Showing
8 changed files
with
608 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,194 @@ | ||
# go-importmap | ||
|
||
Golang importmap generator and super lightweight javascript library and asset manager. | ||
ImportMap is a powerful Go package designed to simplify the management of JavaScript library dependencies for modern web applications. It facilitates the fetching, caching, and generation of import maps, allowing for seamless integration of JavaScript and CSS libraries directly into your projects. By abstracting the complexity of handling external library dependencies, ImportMap enables developers to focus on building feature-rich web applications without worrying about the underlying details of dependency management. | ||
Features | ||
|
||
In addition to generate the importmap section it can also cache external libraries and serve them from local storage. | ||
- **Flexible Provider Interface**: Easily extendable to support different sources for fetching library packages, with a default implementation for cdnjs. | ||
- **Automatic Caching**: Libraries are fetched and cached locally to improve load times and reduce external requests. | ||
- **Import Map Generation**: Automatically generates import maps for included JavaScript and CSS files, adhering to the latest web standards. | ||
- **Customizable Directories**: Configurable cache and assets directories to fit the structure of your project. | ||
- **Shim Management**: Supports the inclusion of ES module shims for backward compatibility with non-module browsers. | ||
- **Dynamic Package Management**: Add individual or multiple library packages with optional versioning and dependency requirements. | ||
|
||
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. | ||
## Getting Started | ||
### Installation | ||
|
||
## Example | ||
To use ImportMap in your Go project, install it as a module: | ||
|
||
```bash | ||
go get github.com/donseba/go-importmap | ||
``` | ||
### Usage Example | ||
|
||
Here's a quick example to get you started with ImportMap: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"context" | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
|
||
"github.com/donseba/go-importmap" | ||
"github.com/donseba/go-importmap/client/cdnjs" | ||
"github.com/donseba/go-importmap/library" | ||
"github.com/donseba/go-importmap" | ||
"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/[email protected]/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) | ||
ctx := context.TODO() | ||
|
||
im := importmap. | ||
NewDefaults(). | ||
WithLogger(slog.Default()). | ||
ShimPath("https://ga.jspm.io/npm:[email protected]") | ||
|
||
im.WithPackages([]library.Package{ | ||
{ | ||
Name: "htmx", | ||
Version: "1.8.5", // locking a specific version | ||
Require: []library.Include{ | ||
{ | ||
File: "htmx.min.js", | ||
}, | ||
{ | ||
File: "/ext/json-enc.js", | ||
As: "json-enc", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "bootstrap", | ||
Require: []library.Include{ | ||
{ | ||
File: "css/bootstrap.min.css", | ||
As: "bootstrap", | ||
}, | ||
{ | ||
File: "js/bootstrap.min.js", | ||
As: "bootstrap", | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// 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`: | ||
|
||
Resulting in the following output: | ||
```html | ||
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js"></script> | ||
<link rel="stylesheet" href="/assets/css/bootstrap.min.css" as="bootstrap"/> | ||
<script async src="https://ga.jspm.io/npm:[email protected]"></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/[email protected]/dist/htmx.min.js", | ||
"json-enc": "https://cdnjs.cloudflare.com/ajax/libs/htmx/1.8.4/ext/json-enc.min.js" | ||
} | ||
} | ||
{ | ||
"bootstrap": "/assets/js/bootstrap.min.js", | ||
"htmx": "/assets/htmx.min.js", | ||
"json-enc": "/assets/ext/json-enc.js" | ||
} | ||
</script> | ||
``` | ||
This above example initializes ImportMap with default settings and fetches the specified library packages from cdnjs. | ||
It then generates an import map with the required JavaScript and CSS files, including the ES module shim | ||
for compatibility with older browsers. | ||
|
||
Result in the following with `SetUseAssets` set to `true`: | ||
Finally, it renders the HTML block with the necessary script tags for the libraries. | ||
|
||
```html | ||
<script async src="https://ga.jspm.io/npm:[email protected]/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> | ||
## Configuration | ||
|
||
ImportMap offers several methods to customize its behavior according to your project's needs: | ||
|
||
- **WithDefaults()**: Initializes ImportMap with default settings for cache and assets directories, and the ES module shim. | ||
- **WithProvider(provider Provider)**: Sets a custom provider for fetching library files. | ||
- **WithPackages(packages []library.Package)**: Sets multiple library packages to the import map. | ||
- **WithPackage(package library.Package)**: Adds a single library package to the import map. | ||
- **AssetsDir(dir string)**: Sets the directory path for assets, default is `assets`. | ||
- **CacheDir(dir string)**: Sets the directory path for the cache, default is `.importmap`. | ||
- **RootDir(dir string)**: Sets the directory paths for assets, cache, and root directories, respectively. | ||
- **ShimPath(sp string)**: Sets the path to the ES module shim, default is `https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js`. | ||
|
||
``` | ||
|
||
Output will look like the following: | ||
## file structure | ||
|
||
``` | ||
- .importmap | ||
- bootstrap | ||
- 5.1.3 | ||
- css | ||
- bootstrap.css | ||
- bootstrap.min.css | ||
- bootstrap-grid.css | ||
- bootstrap-grid.min.css | ||
- js | ||
- bootstrap.js | ||
- bootstrap.bundle.js | ||
- bootstrap.min.js | ||
- htmx | ||
- 1.8.4 | ||
- ext | ||
- json-enc.min.js | ||
- 1.8.5 | ||
- htmx.min.js | ||
- htmx-latest | ||
- 1.8.6 | ||
- htmx.min.js | ||
- htmx.min.js | ||
- ext | ||
- json-enc.js | ||
- ajax-header.js | ||
- apline-morphd.js | ||
- ... etc | ||
- assets | ||
- js | ||
- bootstrap | ||
-5.1.3 | ||
- css | ||
- bootstrap.min.css | ||
- js | ||
- bootstrap.min.js | ||
- htmx | ||
- ext | ||
- json-enc.min.js | ||
- htmx.min.js | ||
- htmx-latest | ||
- htmx.min.js | ||
- 1.8.5 | ||
- htmx.min.js | ||
- ext | ||
- json-enc.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. | ||
As you can see the `.importmap` contains all the files fetched from the cdnjs, while the `assets` contains the files that are used in the importmap. | ||
|
||
## Variations | ||
## RAW Imports | ||
|
||
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", | ||
}, | ||
} | ||
im.WithPackages([]library.Package{ | ||
{ | ||
Name: "htmx", | ||
Version: "1.8.6", | ||
Require: []library.Include{ | ||
{ | ||
Raw: "https://unpkg.com/browse/[email protected]/dist/htmx.min.js", | ||
As: "htmx", | ||
}, | ||
}, | ||
}, | ||
}) | ||
``` | ||
|
||
This wil generate: | ||
```json | ||
{"imports":{"htmx":"https://some.url.to/repo/with.js"}} | ||
``` | ||
{"imports":{"htmx":"https://unpkg.com/browse/[email protected]/dist/htmx.min.js"}} | ||
``` | ||
|
||
## Contributing | ||
|
||
Contributions are welcome! | ||
Whether it's bug reports, feature requests, or code contributions, | ||
please feel free to reach out or submit a pull request. | ||
## License | ||
|
||
Distributed under the MIT License. See `LICENSE` for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.