vite-plugin-hyperapp 1.0.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.
- package/LICENSE.md +7 -0
- package/README.md +3 -0
- package/index.js +62 -0
- package/jsx-runtime.d.ts +11 -0
- package/jsx.js +11 -0
- package/package.json +16 -0
- package/ssr.js +17 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2024 Zacharias Enochsson
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
hmr: true,
|
|
3
|
+
dispatchExport: "dispatch",
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const rxIsEntry = /import {.*app[^}]*} from "hyperapp"/
|
|
7
|
+
const rxIsSSREntry = /import .+ from "vite-plugin-hyperapp\/ssr"/
|
|
8
|
+
|
|
9
|
+
const HMRAppendage = dispatch => `
|
|
10
|
+
if (import.meta.hot) {
|
|
11
|
+
import.meta.hot.accept(m => {
|
|
12
|
+
if (!${dispatch}) return
|
|
13
|
+
let prevState = null
|
|
14
|
+
${dispatch}(s => (prevState = s))
|
|
15
|
+
${dispatch}()
|
|
16
|
+
m.${dispatch}(prevState)
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
`
|
|
20
|
+
|
|
21
|
+
const SSRHMRAppendage = `
|
|
22
|
+
if (import.meta.hot) {
|
|
23
|
+
import.meta.hot.accept()
|
|
24
|
+
}
|
|
25
|
+
`
|
|
26
|
+
|
|
27
|
+
export default function injectConsoleLog(options = {}) {
|
|
28
|
+
options = { ...defaultOptions, ...options }
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
name: "vite-plugin-hyperapp",
|
|
32
|
+
|
|
33
|
+
config: () => ({
|
|
34
|
+
esbuild: {
|
|
35
|
+
jsxFactory: "jsxNode",
|
|
36
|
+
jsxFragment: "jsxFragment",
|
|
37
|
+
jsxInject:
|
|
38
|
+
"import {jsxNode, jsxFragment} from 'vite-plugin-hyperapp/jsx.js'",
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
|
|
42
|
+
async transform(code, id) {
|
|
43
|
+
if (!options.hmr) return null
|
|
44
|
+
|
|
45
|
+
if (rxIsEntry.test(code)) {
|
|
46
|
+
return {
|
|
47
|
+
code: code + HMRAppendage(options.dispatchExport),
|
|
48
|
+
map: null,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (rxIsSSREntry.test(code)) {
|
|
53
|
+
return {
|
|
54
|
+
code: code + SSRHMRAppendage,
|
|
55
|
+
map: null,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return null
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}
|
package/jsx-runtime.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This type declaration is named 'jsx-runtime.d.tx' even though
|
|
3
|
+
jsx is compiled in the classic way. It's only named this to make
|
|
4
|
+
it easier for tsconfig.json to be ok with jsx in .tsx-files.
|
|
5
|
+
*/
|
|
6
|
+
import { Props } from "hyperapp"
|
|
7
|
+
declare global {
|
|
8
|
+
module JSX {
|
|
9
|
+
type IntrinsicElements = Record<string, Props<any>>
|
|
10
|
+
}
|
|
11
|
+
}
|
package/jsx.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { h, text } from "hyperapp"
|
|
2
|
+
|
|
3
|
+
const nodeify = x =>
|
|
4
|
+
(typeof x === "string") | (typeof x === "number") ? text(x) : x
|
|
5
|
+
|
|
6
|
+
export const jsxNode = (tag, props, ...children) =>
|
|
7
|
+
typeof tag === "function"
|
|
8
|
+
? tag(props, children)
|
|
9
|
+
: h(tag, props || {}, children.flat().map(nodeify))
|
|
10
|
+
|
|
11
|
+
export const jsxFragment = (...children) => children.flat().map(nodeify)
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-hyperapp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Plugin and helpers for using Hyperapp with Vite",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"author": "Zacharias Enochsson",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"jsdom": "^24.1.0",
|
|
14
|
+
"hyperapp": "^2.0.22"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/ssr.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { JSDOM } from "jsdom"
|
|
2
|
+
import { app } from "hyperapp"
|
|
3
|
+
|
|
4
|
+
export default function (defs) {
|
|
5
|
+
const dom = new JSDOM("")
|
|
6
|
+
global.document = dom.window.document
|
|
7
|
+
const parent = document.createElement("div")
|
|
8
|
+
const node = document.createElement("div")
|
|
9
|
+
parent.appendChild(node)
|
|
10
|
+
return new Promise(resolve => {
|
|
11
|
+
const stop = app({ ...defs, node })
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
stop() //prevents any eventual callbacks from calling back
|
|
14
|
+
resolve(parent.firstChild.outerHTML)
|
|
15
|
+
}, 0)
|
|
16
|
+
})
|
|
17
|
+
}
|