hikkaku 0.1.0

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,77 @@
1
+ import type * as sb3 from '@pnsk-lab/sb3-types'
2
+ import { createServerModuleRunner, type PluginOption } from 'vite'
3
+
4
+ const BASE_URL = 'https://scratchfoundation.github.io/scratch-gui/'
5
+
6
+ export interface HikkakuViteInit {
7
+ entry: string
8
+ }
9
+ export default function hikkaku(init: HikkakuViteInit): PluginOption {
10
+ return {
11
+ name: 'vite-plugin-hikkaku',
12
+ config() {
13
+ return {
14
+ environments: {
15
+ hikkaku: {},
16
+ client: {},
17
+ },
18
+ }
19
+ },
20
+ resolveId(source) {
21
+ if (source === '/@virtual/hikkaku-client') {
22
+ return source
23
+ }
24
+ },
25
+ load(id) {
26
+ if (id === '/@virtual/hikkaku-client') {
27
+ return `
28
+ import 'hikkaku/client'
29
+ `
30
+ }
31
+ },
32
+ async configureServer(server) {
33
+ const hikkakuEnv = server.environments.hikkaku
34
+ if (!hikkakuEnv) {
35
+ throw new Error('Hikkaku environment is not configured.')
36
+ }
37
+ const runner = createServerModuleRunner(hikkakuEnv)
38
+ await runner.import(init.entry)
39
+ hikkakuEnv.hot.on('hikkaku:project', (project: sb3.ScratchProject) => {
40
+ server.environments.client.hot.send('hikkaku:project', project)
41
+ })
42
+
43
+ server.middlewares.use(async (req, res, next) => {
44
+ if (req.url === '/') {
45
+ const html = (await fetch(BASE_URL).then((res) => res.text()))
46
+ .replace(
47
+ 'gui.js',
48
+ 'https://scratchfoundation.github.io/scratch-gui/gui.js',
49
+ )
50
+ .replace(
51
+ '</head>',
52
+ '<script src="/@vite/client" type="module"></script><script type="module" src="/@virtual/hikkaku-client"></script></head>',
53
+ )
54
+ res.setHeader('Content-Type', 'text/html')
55
+ res.end(html)
56
+ return
57
+ }
58
+ if (req.url?.startsWith('/static/')) {
59
+ const url = new URL(req.url.slice(1), BASE_URL)
60
+ const response = await fetch(url.toString())
61
+ if (!response.ok) {
62
+ res.statusCode = response.status
63
+ res.end(`Error fetching ${url}: ${response.statusText}`)
64
+ return
65
+ }
66
+ res.setHeader(
67
+ 'Content-Type',
68
+ response.headers.get('content-type') || 'application/octet-stream',
69
+ )
70
+ res.end(new Uint8Array(await response.arrayBuffer()))
71
+ return
72
+ }
73
+ next()
74
+ })
75
+ },
76
+ }
77
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json"
3
+ }
File without changes