UNPKG

@adonisjs/inertia

Version:

Official Inertia.js adapter for AdonisJS

57 lines (56 loc) 2.1 kB
import { t as debug_default } from "../../../debug-CBMTuPUm.js"; import { encode } from "html-entities"; import { EdgeError } from "edge-error"; function isSubsetOf(expression, expressions, errorCallback) { if (!expressions.includes(expression.type)) errorCallback(); } const inertiaTag = { block: false, tagName: "inertia", seekable: true, compile(parser, buffer, { filename, loc, properties }) { if (properties.jsArg.trim() === "") { buffer.writeExpression(`out += state.inertia(state.page)`, filename, loc.start.line); return; } properties.jsArg = `(${properties.jsArg})`; const parsed = parser.utils.transformAst(parser.utils.generateAST(properties.jsArg, loc, filename), filename, parser); isSubsetOf(parsed, ["ObjectExpression"], () => { const { line, col } = parser.utils.getExpressionLoc(parsed); throw new EdgeError(`"${properties.jsArg}" is not a valid argument for @inertia`, "E_UNALLOWED_EXPRESSION", { line, col, filename }); }); const attributes = parser.utils.stringify(parsed); buffer.outputExpression(`state.inertia(state.page, ${attributes})`, filename, loc.start.line, false); } }; const inertiaHeadTag = { block: false, tagName: "inertiaHead", seekable: false, compile(_, buffer, { filename, loc }) { buffer.outputExpression("state.inertiaHead(state.page)", filename, loc.start.line, false); } }; const edgePluginInertia = () => { return (edge) => { debug_default("sharing globals and inertia tags with edge"); edge.global("inertia", (page = {}, attributes = {}) => { if (page.ssrBody) return page.ssrBody; const className = attributes?.class ? ` class="${attributes.class}"` : ""; const id = attributes?.id ? ` id="${attributes.id}"` : " id=\"app\""; const tag = attributes?.as || "div"; return `<${tag}${id}${className} data-page="${encode(JSON.stringify(page))}"></${tag}>`; }); edge.global("inertiaHead", (page) => { const { ssrHead = [] } = page || {}; return ssrHead.join("\n"); }); edge.registerTag(inertiaHeadTag); edge.registerTag(inertiaTag); }; }; export { edgePluginInertia };