feat(core): expose McFly global to app server scripts
This commit is contained in:
parent
d46bf2255b
commit
abaab2b73f
3 changed files with 45 additions and 5 deletions
|
@ -75,7 +75,7 @@ export default eventHandler(async (event) => {
|
||||||
const transforms = [
|
const transforms = [
|
||||||
{
|
{
|
||||||
fn: evaluateServerScripts,
|
fn: evaluateServerScripts,
|
||||||
args: [''],
|
args: [event],
|
||||||
hook: mcflyHooks.serverScriptsEvaluated,
|
hook: mcflyHooks.serverScriptsEvaluated,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,6 +2,15 @@ import { ELEMENT_NODE, parse, renderSync, walkSync } from 'ultrahtml'
|
||||||
import { parseScript } from 'esprima'
|
import { parseScript } from 'esprima'
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import type { BaseNode as JsNode } from 'estree'
|
import type { BaseNode as JsNode } from 'estree'
|
||||||
|
import type { H3Event } from 'h3'
|
||||||
|
|
||||||
|
const McFlyGlobal: {
|
||||||
|
hello: string
|
||||||
|
event: any
|
||||||
|
} = {
|
||||||
|
hello: 'world',
|
||||||
|
event: undefined,
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('estree').BaseNode} JsNode
|
* @typedef {import('estree').BaseNode} JsNode
|
||||||
|
@ -12,7 +21,13 @@ import type { BaseNode as JsNode } from 'estree'
|
||||||
* @param {string} _html
|
* @param {string} _html
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function evaluateServerScripts(_html: string) {
|
export function evaluateServerScripts(_html: string, event: H3Event) {
|
||||||
|
McFlyGlobal.event = {
|
||||||
|
url: event.node.req.url,
|
||||||
|
method: event.node.req.method,
|
||||||
|
statusCode: event.node.req.statusCode,
|
||||||
|
statusMessage: event.node.req.statusMessage,
|
||||||
|
}
|
||||||
let html = evaluateServerScript(_html)
|
let html = evaluateServerScript(_html)
|
||||||
html = deleteServerScripts(html)
|
html = deleteServerScripts(html)
|
||||||
return html
|
return html
|
||||||
|
@ -52,9 +67,15 @@ function evaluateServerScript(html: string) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
.map((n) => n.declarations[0].id.name) //['declarations'][0].id.name)
|
.map((n) => n.declarations[0].id.name) //['declarations'][0].id.name)
|
||||||
|
|
||||||
const constructor = `(function(){}.constructor)(\`${script}; return {${keys.join(
|
// const McFly=${JSON.stringify(McFlyGlobal)};
|
||||||
','
|
const constructor = `(function(){}.constructor)(\`
|
||||||
)}}\`);`
|
const McFly=${JSON.stringify(McFlyGlobal)}
|
||||||
|
${script};
|
||||||
|
return {
|
||||||
|
${keys.join(',')},
|
||||||
|
McFly: ${JSON.stringify(McFlyGlobal)}
|
||||||
|
}\`);`
|
||||||
|
|
||||||
const evalStore = eval(constructor)
|
const evalStore = eval(constructor)
|
||||||
Object.assign(setupMap, new evalStore())
|
Object.assign(setupMap, new evalStore())
|
||||||
})
|
})
|
||||||
|
|
19
site/src/pages/cozy.html
Normal file
19
site/src/pages/cozy.html
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<my-head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Cozy (McFly)</title>
|
||||||
|
<script server:setup>
|
||||||
|
const appName = "Cozy"
|
||||||
|
console.log('>>> Event (from app)', McFly.event)
|
||||||
|
</script>
|
||||||
|
</my-head>
|
||||||
|
<body>
|
||||||
|
<h1>{{ appName }} - {{McFly.hello}}</h1>
|
||||||
|
<form>
|
||||||
|
<input id="url" name="url" />
|
||||||
|
<button type="submit">Extract</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue