diff --git a/docs/core/usage.md b/docs/core/usage.md index c7e83cc..f4e7199 100644 --- a/docs/core/usage.md +++ b/docs/core/usage.md @@ -58,7 +58,7 @@ Where [options] are mostly optional configuration flags—see [Opti

Log various story statistics. Primarily, passage and word counts.

Note: Unsupported when watch mode (-w, --watch) is enabled.

-
-m SRC, --module=SRC
Module sources (repeatable); may consist of supported files and/or directories to recursively search for such files. Each file will be wrapped within the appropriate markup and bundled into the <head> element of the compiled HTML. Supported files: .css, .js, .otf, .ttf, .woff, .woff2.
+
-m SRC, --module=SRC
Module sources (repeatable); may consist of supported files and/or directories to recursively search for such files. Each file will be wrapped within the appropriate markup and bundled into the <head> element of the compiled HTML. Supported files: .css, .js, .mjs, .otf, .ttf, .woff, .woff2.
--no-trim

Do not trim whitespace surrounding passages—i.e., whitespace preceding and trailing the actual text of the passage. By default, such whitespace is removed when processing passages.

Note: It is recommended that you do not disable passage trimming.

@@ -86,7 +86,9 @@ The following extensions are supported:
.tw, .twee

Twee notation source files to process for passages.

-

Note: If any of these files are in the unofficial Twee2 notation, you must manually enable the Twee2 compatibility mode via its command line option (--twee2-compat).

+

Note: + If any of these files are in the unofficial Twee2 notation, you must manually enable the Twee2 compatibility mode via its command line option (--twee2-compat). +

.tw2, .twee2
Unofficial Twee2 notation source files to process for passages. Twee2 compatibility mode is automatically enabled for files with these extensions.
@@ -96,6 +98,13 @@ The following extensions are supported:
CSS source files to bundle.
.js
JavaScript source files to bundle.
+
.mjs
+
+

JavaScript module (ESM) source files to bundle.

+

Note: + May only be used with the module option (-m SRC, --module=SRC). +

+
.otf, .ttf, .woff, .woff2
Font files to bundle, as @font-face style rules. The generated name of the font family will be the font's base filename sans its extension—e.g., the family name for chinacat.tff will be chinacat.
.gif, .jpeg, .jpg, .png, .svg, .tif, .tiff, .webp
diff --git a/filesystem.go b/filesystem.go index 17c1f2a..4494845 100644 --- a/filesystem.go +++ b/filesystem.go @@ -244,7 +244,7 @@ func knownFileType(filename string) bool { "tw2", "twee2", "htm", "html", "css", - "js", + "js", "mjs", "otf", "ttf", "woff", "woff2", "gif", "jpeg", "jpg", "png", "svg", "tif", "tiff", "webp", "aac", "flac", "m4a", "mp3", "oga", "ogg", "opus", "wav", "wave", "weba", diff --git a/module.go b/module.go index a6b2876..8915a98 100644 --- a/module.go +++ b/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 can be found in the LICENSE file. */ @@ -33,9 +33,11 @@ func loadModules(filenames []string, encoding string) []byte { switch normalizedFileExt(filename) { // NOTE: The case values here should match those in `filesystem.go:knownFileType()`. case "css": - source, err = loadModuleTagged("style", filename, encoding) + source, err = loadModuleByType("text/css", filename, encoding) 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": source, err = loadModuleFont(filename) default: @@ -55,7 +57,7 @@ func loadModules(filenames []string, encoding string) []byte { 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) if err != nil { return nil, err @@ -65,24 +67,25 @@ func loadModuleTagged(tag, filename, encoding string) ([]byte, error) { return source, nil } - var ( - idSlug = tag + "-module-" + slugify(strings.Split(filepath.Base(filename), ".")[0]) - mimeType string - b bytes.Buffer - ) - switch tag { - case "script": - mimeType = "text/javascript" - case "style": - mimeType = "text/css" + var tag string + switch typeValue { + case "module", "text/javascript": + tag = "script" + case "text/css": + tag = "style" } + var ( + idSlug = tag + "-module-" + slugify(strings.Split(filepath.Base(filename), ".")[0]) + b bytes.Buffer + ) + if _, err := fmt.Fprintf( &b, `<%s id=%q type=%q>%s`, tag, idSlug, - mimeType, + typeValue, source, ); err != nil { return nil, err diff --git a/storyload.go b/storyload.go index c6ca2d4..b54bacb 100644 --- a/storyload.go +++ b/storyload.go @@ -52,6 +52,8 @@ func (s *story) load(filenames []string, c *config) { if err := s.loadTagged("script", filename, c.encoding); err != nil { log.Fatalf("error: load %s: %s", filename, err.Error()) } + case "mjs": + log.Fatalf("error: load %s: ESM files must be loaded via the module option", filename) case "otf", "ttf", "woff", "woff2": if err := s.loadFont(filename); err != nil { log.Fatalf("error: load %s: %s", filename, err.Error())