packaton 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ import { execFileSync } from 'node:child_process'
2
+
3
+
4
+ export const openInBrowser = (async () => {
5
+ try {
6
+ return (await import('open')).default
7
+ }
8
+ catch (error) {
9
+ return _openInBrowser
10
+ }
11
+ })()
12
+
13
+ function _openInBrowser(address) {
14
+ switch (process.platform) {
15
+ case 'darwin':
16
+ execFileSync('open', [address])
17
+ break
18
+ case 'win32':
19
+ execFileSync('start', [address])
20
+ break
21
+ }
22
+ }
23
+
@@ -0,0 +1,23 @@
1
+ import { join } from 'node:path'
2
+ import { read, sizeOf, sha1, saveAsJSON, isFile } from './fs-utils.js'
3
+
4
+
5
+ export function reportSizes(reportFilename, baseDir, files) {
6
+ const oldReport = isFile(reportFilename)
7
+ ? JSON.parse(read(reportFilename))
8
+ : {}
9
+ const newReport = {}
10
+
11
+ for (const f of files) {
12
+ const fPath = join(baseDir, f)
13
+ const size = sizeOf(fPath)
14
+ newReport[f] = {
15
+ hash: sha1(fPath),
16
+ delta: size - (oldReport[f]?.size || 0),
17
+ size
18
+ }
19
+ }
20
+
21
+ console.table(newReport)
22
+ saveAsJSON(reportFilename, newReport)
23
+ }
@@ -0,0 +1,32 @@
1
+ const WATCH_API = '/packaton/watch-dev'
2
+
3
+ longPollDevChanges()
4
+ async function longPollDevChanges() {
5
+ try {
6
+ const response = await fetch(WATCH_API)
7
+ if (!response.ok)
8
+ throw response.statusText
9
+
10
+ const file = await response.json() || ''
11
+ if (file.endsWith('.css')) {
12
+ hotReloadCSS(file)
13
+ longPollDevChanges()
14
+ }
15
+ else if (file)
16
+ location.reload()
17
+ else // server timeout
18
+ longPollDevChanges()
19
+ }
20
+ catch (error) {
21
+ console.error('hot reload', error?.message || error)
22
+ setTimeout(longPollDevChanges, 3000)
23
+ }
24
+ }
25
+
26
+ function hotReloadCSS(file) {
27
+ const link = document.querySelector(`link[href*="${file}"]`)
28
+ if (link) {
29
+ const [url] = link.href.split('?')
30
+ link.href = url + '?' + Date.now()
31
+ }
32
+ }