]> git.codecow.com Git - nano-pow.git/commitdiff
Convert async dynamic imports to standard import map. Drop jsdelivr and just use...
authorChris Duncan <chris@codecow.com>
Fri, 10 Jul 2026 19:45:00 +0000 (12:45 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 10 Jul 2026 19:45:00 +0000 (12:45 -0700)
test/index.html

index 40b423f97410bb9b97f641ddb802f2407011542b..d3fbac9eb24abc42c19bf162fc1f3a2ca4d13bd9 100644 (file)
@@ -7,40 +7,25 @@ SPDX-License-Identifier: GPL-3.0-or-later
 
 <head>
        <link rel="icon" href="#">
-       <script type="module">
-               try {
-                       const { hostname, port } = new URL(window.location)
-                       let NanoPow, Cache, stats
-                       if (port && hostname === '127.0.0.1') {
-                               NanoPow = await import('../dist/index.js')
-                       } else {
-                               try {
-                                       NanoPow = await import('https://unpkg.com/nano-pow@5.2/dist/index.js')
-                               } catch (err) {
-                                       console.warn(err)
-                                       try {
-                                               NanoPow = await import('https://cdn.jsdelivr.net/npm/nano-pow@5.2/dist/index.js')
-                                       } catch (err) {
-                                               throw new Error(`Failed to load NanoPow ${err}`)
-                                       }
-                               }
-                       }
-
-                       if (port && hostname === '127.0.0.1') {
-                               ({ Cache, stats } = await import('../dist/utils/index.js'))
-                       } else {
-                               try {
-                                       ({ Cache, stats } = await import('https://unpkg.com/nano-pow@5.2/dist/utils/index.js'))
-                               } catch (err) {
-                                       console.warn(err)
-                                       try {
-                                               ({ Cache, stats } = await import('https://cdn.jsdelivr.net/npm/nano-pow@5.2/dist/utils/index.js'))
-                                       } catch (err) {
-                                               throw new Error(`Failed to load NanoPow utilities ${err}`)
-                                       }
+       <script type="importmap">
+               {
+                       "imports": {
+                               "nano-pow": "https://unpkg.com/nano-pow@5.2.0/dist/index.js",
+                               "nano-pow/utils": "https://unpkg.com/nano-pow@5.2.0/dist/utils/index.js"
+                       },
+                       "scopes": {
+                               "/test/": {
+                                       "nano-pow": "../dist/index.js",
+                                       "nano-pow/utils": "../dist/utils/index.js"
                                }
                        }
+               }
+       </script>
+       <script type="module">
+               import { NanoPow } from 'nano-pow'
+               import { Cache, stats } from 'nano-pow/utils'
 
+               try {
                        const glSize = (canvas => {
                                const gl = canvas.getContext('webgl2')
                                const MAX_VIEWPORT_DIMS = gl?.getParameter(gl.MAX_VIEWPORT_DIMS) ?? [0x1000, 0x1000]
@@ -49,7 +34,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                return gl?.drawingBufferHeight < gl?.drawingBufferWidth ? gl?.drawingBufferHeight : gl?.drawingBufferWidth
                        })(new OffscreenCanvas(0, 0))
 
-                       function random (size = 64) {
+                       function random(size = 64) {
                                let hex = ''
                                while (hex.length < size) {
                                        hex += crypto.randomUUID().replace(/-.*-/g, '')
@@ -57,7 +42,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                return hex.slice(0, size)
                        }
 
-                       function average (times, type, effort) {
+                       function average(times, type, effort) {
                                const averages = stats(times)
                                const title = type === 'WebGPU'
                                        ? `NanoPow (${type}) | Effort: ${effort} | Dispatch: ${(effort << 8) << 1} | Threads: ${64 * (effort << 8) << 1}`
@@ -69,7 +54,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                }
                        }
 
-                       async function run (size, difficulty, effort, api, isOutputShown, isDebug, isSelfCheck) {
+                       async function run(size, difficulty, effort, api, isOutputShown, isDebug, isSelfCheck) {
                                // Generate once on load to compile shaders and initialize buffers
                                await NanoPow.work_generate(random(), { api, difficulty: '0', debug: isDebug })
                                const type = api
@@ -192,7 +177,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                console.log('%cTESTING COMPLETE', 'color:orange;font-weight:bold')
                        }
 
-                       async function score (runs, size, difficulty, effort, api) {
+                       async function score(runs, size, difficulty, effort, api) {
                                console.log(`%cNanoPow ${api}`, 'color:green', `Calculate truncated harmonic mean of the truncated arithmetic rate across ${runs} runs of ${size} samples.`)
                                const rates = []
                                for (let i = 0; i < runs; i++) {
@@ -220,7 +205,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                console.log('%cSCORING COMPLETE', 'color:orange;font-weight:bold')
                        }
 
-                       async function execute (size, difficulty, effort, api, isOutputShown = false, debug = false) {
+                       async function execute(size, difficulty, effort, api, isOutputShown = false, debug = false) {
                                Cache.clear()
                                const output = document.getElementById('output')
                                const status = document.getElementById('status')
@@ -245,7 +230,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                return times
                        }
 
-                       function startValidation (event) {
+                       function startValidation(event) {
                                const difficulty = document.getElementById('difficulty')?.value
                                const work = document.getElementById('work')?.value
                                const hash = document.getElementById('hash')?.value
@@ -277,7 +262,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        document.getElementById('hash').addEventListener('input', startValidation)
                        document.getElementById('api').addEventListener('input', startValidation)
 
-                       function startTest (event) {
+                       function startTest(event) {
                                event.target.disabled = true
                                const size = document.getElementById('size')
                                const difficulty = document.getElementById('difficulty')
@@ -293,7 +278,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                        })
                        }
 
-                       function startScore (event) {
+                       function startScore(event) {
                                event.target.disabled = true
                                const runs = document.getElementById('runs')
                                const size = document.getElementById('size')