diff --git a/pkg/service/soundtouchweb/static/js/app.js b/pkg/service/soundtouchweb/static/js/app.js index b4f24508..8b202ca2 100644 --- a/pkg/service/soundtouchweb/static/js/app.js +++ b/pkg/service/soundtouchweb/static/js/app.js @@ -1,6 +1,4 @@ -import { h, render } from '/app/static/lib/preact.module.js'; -import { useState, useEffect, useCallback } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, render, useCallback, useEffect, useState } from './dependencies.js'; import { DeviceList } from './components/DeviceList.js'; import { NowPlaying } from './components/NowPlaying.js'; import { Controls } from './components/Controls.js'; diff --git a/pkg/service/soundtouchweb/static/js/components/Announcements.js b/pkg/service/soundtouchweb/static/js/components/Announcements.js index da5a9d3b..58331945 100644 --- a/pkg/service/soundtouchweb/static/js/components/Announcements.js +++ b/pkg/service/soundtouchweb/static/js/components/Announcements.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/Controls.js b/pkg/service/soundtouchweb/static/js/components/Controls.js index 410e427f..f3f4c3b5 100644 --- a/pkg/service/soundtouchweb/static/js/components/Controls.js +++ b/pkg/service/soundtouchweb/static/js/components/Controls.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/DeviceList.js b/pkg/service/soundtouchweb/static/js/components/DeviceList.js index 48838a15..578931e0 100644 --- a/pkg/service/soundtouchweb/static/js/components/DeviceList.js +++ b/pkg/service/soundtouchweb/static/js/components/DeviceList.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useState } from '../dependencies.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/Library.js b/pkg/service/soundtouchweb/static/js/components/Library.js index 4af663ed..ae059e3e 100644 --- a/pkg/service/soundtouchweb/static/js/components/Library.js +++ b/pkg/service/soundtouchweb/static/js/components/Library.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/NowPlaying.js b/pkg/service/soundtouchweb/static/js/components/NowPlaying.js index ab665606..434d93bc 100644 --- a/pkg/service/soundtouchweb/static/js/components/NowPlaying.js +++ b/pkg/service/soundtouchweb/static/js/components/NowPlaying.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect, useRef } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useRef, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/PlayURL.js b/pkg/service/soundtouchweb/static/js/components/PlayURL.js index 0c815f73..544aaa38 100644 --- a/pkg/service/soundtouchweb/static/js/components/PlayURL.js +++ b/pkg/service/soundtouchweb/static/js/components/PlayURL.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/Presets.js b/pkg/service/soundtouchweb/static/js/components/Presets.js index 455e0ec1..c62fc935 100644 --- a/pkg/service/soundtouchweb/static/js/components/Presets.js +++ b/pkg/service/soundtouchweb/static/js/components/Presets.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/RadioBrowser.js b/pkg/service/soundtouchweb/static/js/components/RadioBrowser.js index cf43db0f..4995f29b 100644 --- a/pkg/service/soundtouchweb/static/js/components/RadioBrowser.js +++ b/pkg/service/soundtouchweb/static/js/components/RadioBrowser.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/Recents.js b/pkg/service/soundtouchweb/static/js/components/Recents.js index 2bf7be54..7f044ad7 100644 --- a/pkg/service/soundtouchweb/static/js/components/Recents.js +++ b/pkg/service/soundtouchweb/static/js/components/Recents.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/Sources.js b/pkg/service/soundtouchweb/static/js/components/Sources.js index e86e78c1..8eeb85ea 100644 --- a/pkg/service/soundtouchweb/static/js/components/Sources.js +++ b/pkg/service/soundtouchweb/static/js/components/Sources.js @@ -1,5 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/TTS.js b/pkg/service/soundtouchweb/static/js/components/TTS.js index 43a58cf0..b1bd5a90 100644 --- a/pkg/service/soundtouchweb/static/js/components/TTS.js +++ b/pkg/service/soundtouchweb/static/js/components/TTS.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/TuneInBrowser.js b/pkg/service/soundtouchweb/static/js/components/TuneInBrowser.js index 8b8033be..91194273 100644 --- a/pkg/service/soundtouchweb/static/js/components/TuneInBrowser.js +++ b/pkg/service/soundtouchweb/static/js/components/TuneInBrowser.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/components/Zone.js b/pkg/service/soundtouchweb/static/js/components/Zone.js index 10768e28..ce0c311b 100644 --- a/pkg/service/soundtouchweb/static/js/components/Zone.js +++ b/pkg/service/soundtouchweb/static/js/components/Zone.js @@ -1,6 +1,4 @@ -import { h } from '/app/static/lib/preact.module.js'; -import { useState, useEffect } from '/app/static/lib/preact-hooks.module.js'; -import htm from '/app/static/lib/htm.module.js'; +import { h, htm, useEffect, useState } from '../dependencies.js'; import { api } from '../api.js'; const html = htm.bind(h); diff --git a/pkg/service/soundtouchweb/static/js/dependencies.js b/pkg/service/soundtouchweb/static/js/dependencies.js new file mode 100644 index 00000000..86a87bf7 --- /dev/null +++ b/pkg/service/soundtouchweb/static/js/dependencies.js @@ -0,0 +1,5 @@ +import { h, render } from '../lib/preact.module.js'; +import { useCallback, useEffect, useRef, useState } from '../lib/preact-hooks.module.js'; +import htm from '../lib/htm.module.js'; + +export { h, htm, render, useCallback, useEffect, useRef, useState }; diff --git a/pkg/service/soundtouchweb/static_compatibility_test.go b/pkg/service/soundtouchweb/static_compatibility_test.go index 889b3e71..f5ea980c 100644 --- a/pkg/service/soundtouchweb/static_compatibility_test.go +++ b/pkg/service/soundtouchweb/static_compatibility_test.go @@ -17,19 +17,28 @@ func TestStaticModulesDoNotRequireImportMaps(t *testing.T) { t.Fatal("index.html must not require import map support") } - dependencies := map[string]string{ - "/app/static/lib/preact.module.js": "static/lib/preact.module.js", - "/app/static/lib/preact-hooks.module.js": "static/lib/preact-hooks.module.js", - "/app/static/lib/htm.module.js": "static/lib/htm.module.js", + const dependencyModulePath = "static/js/dependencies.js" + dependencyModule, err := fs.ReadFile(StaticFS, dependencyModulePath) + if err != nil { + t.Fatalf("read dependency module: %v", err) } - usedDependencies := make(map[string]bool, len(dependencies)) - for publicPath, embeddedPath := range dependencies { + + dependencies := map[string]string{ + "../lib/preact.module.js": "static/lib/preact.module.js", + "../lib/preact-hooks.module.js": "static/lib/preact-hooks.module.js", + "../lib/htm.module.js": "static/lib/htm.module.js", + } + for modulePath, embeddedPath := range dependencies { if _, err := fs.Stat(StaticFS, embeddedPath); err != nil { - t.Errorf("static dependency %q: %v", publicPath, err) + t.Errorf("static dependency %q: %v", modulePath, err) + } + if !bytes.Contains(dependencyModule, []byte(modulePath)) { + t.Errorf("dependency module does not import %q", modulePath) } } bareDependency := regexp.MustCompile(`\bfrom\s*['"](?:preact(?:/hooks)?|htm)['"]`) + directDependency := regexp.MustCompile(`\bfrom\s*['"][^'"]*lib/(?:preact(?:-hooks)?|htm)\.module\.js['"]`) err = fs.WalkDir(StaticFS, "static", func(path string, entry fs.DirEntry, walkErr error) error { if walkErr != nil { return walkErr @@ -45,10 +54,8 @@ func TestStaticModulesDoNotRequireImportMaps(t *testing.T) { if bareDependency.Match(source) { t.Errorf("%s contains a bare dependency import", path) } - for publicPath := range dependencies { - if bytes.Contains(source, []byte(publicPath)) { - usedDependencies[publicPath] = true - } + if path != dependencyModulePath && strings.HasPrefix(path, "static/js/") && directDependency.Match(source) { + t.Errorf("%s bypasses %s", path, dependencyModulePath) } return nil @@ -56,10 +63,4 @@ func TestStaticModulesDoNotRequireImportMaps(t *testing.T) { if err != nil { t.Fatalf("walk JavaScript modules: %v", err) } - - for publicPath := range dependencies { - if !usedDependencies[publicPath] { - t.Errorf("static dependency %q is not imported", publicPath) - } - } }