mirror of
https://github.com/tmedwards/tweego.git
synced 2025-07-05 14:10:27 -04:00
chore: add ESM file support
This commit is contained in:
parent
9f43ea0976
commit
835507d95c
1 changed files with 19 additions and 15 deletions
34
module.go
34
module.go
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2014–2021 Thomas Michael Edwards. All rights reserved.
|
Copyright © 2014–2023 Thomas Michael Edwards. All rights reserved.
|
||||||
Use of this source code is governed by a Simplified BSD License which
|
Use of this source code is governed by a Simplified BSD License which
|
||||||
can be found in the LICENSE file.
|
can be found in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
@ -33,9 +33,11 @@ func loadModules(filenames []string, encoding string) []byte {
|
||||||
switch normalizedFileExt(filename) {
|
switch normalizedFileExt(filename) {
|
||||||
// NOTE: The case values here should match those in `filesystem.go:knownFileType()`.
|
// NOTE: The case values here should match those in `filesystem.go:knownFileType()`.
|
||||||
case "css":
|
case "css":
|
||||||
source, err = loadModuleTagged("style", filename, encoding)
|
source, err = loadModuleByType("text/css", filename, encoding)
|
||||||
case "js":
|
case "js":
|
||||||
source, err = loadModuleTagged("script", filename, encoding)
|
source, err = loadModuleByType("text/javascript", filename, encoding)
|
||||||
|
case "mjs":
|
||||||
|
source, err = loadModuleByType("module", filename, encoding)
|
||||||
case "otf", "ttf", "woff", "woff2":
|
case "otf", "ttf", "woff", "woff2":
|
||||||
source, err = loadModuleFont(filename)
|
source, err = loadModuleFont(filename)
|
||||||
default:
|
default:
|
||||||
|
@ -55,7 +57,7 @@ func loadModules(filenames []string, encoding string) []byte {
|
||||||
return bytes.Join(headTags, []byte("\n"))
|
return bytes.Join(headTags, []byte("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadModuleTagged(tag, filename, encoding string) ([]byte, error) {
|
func loadModuleByType(typeValue, filename, encoding string) ([]byte, error) {
|
||||||
source, err := fileReadAllWithEncoding(filename, encoding)
|
source, err := fileReadAllWithEncoding(filename, encoding)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -65,24 +67,26 @@ func loadModuleTagged(tag, filename, encoding string) ([]byte, error) {
|
||||||
return source, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var tag string
|
||||||
idSlug = tag + "-module-" + slugify(strings.Split(filepath.Base(filename), ".")[0])
|
switch typeValue {
|
||||||
mimeType string
|
case "module", "text/javascript":
|
||||||
b bytes.Buffer
|
tag = "script"
|
||||||
)
|
case "text/css":
|
||||||
switch tag {
|
tag = "style"
|
||||||
case "script":
|
|
||||||
mimeType = "text/javascript"
|
|
||||||
case "style":
|
|
||||||
mimeType = "text/css"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
idSlug = tag + "-module-" + slugify(strings.Split(filepath.Base(filename), ".")[0])
|
||||||
|
tag string
|
||||||
|
b bytes.Buffer
|
||||||
|
)
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(
|
if _, err := fmt.Fprintf(
|
||||||
&b,
|
&b,
|
||||||
`<%s id=%q type=%q>%s</%[1]s>`,
|
`<%s id=%q type=%q>%s</%[1]s>`,
|
||||||
tag,
|
tag,
|
||||||
idSlug,
|
idSlug,
|
||||||
mimeType,
|
typeValue,
|
||||||
source,
|
source,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue