Skip to content

Commit

Permalink
ensure the HTML jsx template used xarc's createElement (electrode-io#…
Browse files Browse the repository at this point in the history
  • Loading branch information
divyakarippath authored and ImgBotApp committed Aug 20, 2020
1 parent 12edbfb commit 5e76e01
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/subapp-server/lib/template-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require("lodash");
const Path = require("path");
const { TagRenderer } = require("@xarc/tag-renderer");
const { JsxRenderer } = require("@xarc/jsx-renderer");
const { JsxRenderer, xarcJsxElement } = require("@xarc/jsx-renderer");
const { loadTokenModuleHandler } = require("@xarc/render-context");
const {
utils: {
Expand Down Expand Up @@ -56,7 +56,7 @@ function initializeTemplate(
const templateModule = require(templateFullPath); // eslint-disable-line
const template = _.get(templateModule, "default", templateModule);

if (template.children) {
if (template.$$typeof === xarcJsxElement || template.children) {
// JSX
asyncTemplate = new JsxRenderer({
templateFullPath: Path.dirname(templateFullPath),
Expand Down
8 changes: 4 additions & 4 deletions packages/xarc-jsx-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"test": "clap test-only",
"coverage": "clap check",
"prepublishOnly": "clap -n build docs && clap check",
"docs": "clap xarc/docs"
"test": "xrun xarc/test-only",
"coverage": "xrun xarc/check",
"prepublishOnly": "xrun [[build, docs], xarc/check]",
"docs": "xrun xarc/docs"
},
"publishConfig": {
"access": "public"
Expand Down
12 changes: 9 additions & 3 deletions packages/xarc-jsx-renderer/src/JsxRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "@xarc/render-context";
import { omittedCloseTags, expandProps } from "./utils";
import { makeDefer, each } from "xaa";
import { xarcJsxElement } from "./symbols";

/**
* The JSX renderer
Expand Down Expand Up @@ -177,6 +178,11 @@ export class JsxRenderer {
}
};

assert(
element.$$typeof === xarcJsxElement,
"Invalid xarc jsx element. Please make sure the JSX pragma /* @jsx createElement */ is added in the template file"
);

if (element.memoize) {
context.output.add(`${element.memoize}\n`);
} else if (element.tag) {
Expand All @@ -187,9 +193,9 @@ export class JsxRenderer {
context.output.add(`<${element.tag}${expandProps(element.props, context)}/>`);
}
} else if (!element.type) {
return handleElementResult(
element(element.props, context, { element, depth, output: context.output })
);
return handleElementResult(
element(element.props, context, { element, depth, output: context.output })
);
} else if (element.Construct) {
const inst = new element.Construct(element.props, context);
return handleElementResult(
Expand Down
5 changes: 4 additions & 1 deletion packages/xarc-jsx-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { IndexPage } from "./tags/IndexPage";
import { Require } from "./tags/Require";
import { Literal } from "./tags/Literal";
import { RegisterTokenIds } from "./tags/RegisterTokenIds";
import { xarcJsxElement } from "./symbols";

/** @ignore */
export { Component, Token, IndexPage, Require, Literal, JsxRenderer, RegisterTokenIds };
export { Component, Token, IndexPage, Require, Literal, JsxRenderer, RegisterTokenIds, xarcJsxElement };

let ELEMENT_ID = 0;

export type Element = {
$$typeof: symbol;
id: number;
type: any;
children: any;
Expand All @@ -41,6 +43,7 @@ export function createElement(type: any, props: any, ...children: any[]) {
}

const element: Element = {
$$typeof: xarcJsxElement,
id: ELEMENT_ID++,
type,
children,
Expand Down
1 change: 1 addition & 0 deletions packages/xarc-jsx-renderer/src/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const xarcJsxElement = Symbol("xarc.jsx.element");
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
Require,
Literal,
Component,
RegisterTokenIds
RegisterTokenIds,
xarcJsxElement
} from "../../src";

const Template = () => {
Expand All @@ -32,4 +33,5 @@ const Template = () => {
);
};

Template.$$typeof = xarcJsxElement;
export default Template;
4 changes: 3 additions & 1 deletion packages/xarc-jsx-renderer/test/jsx-templates/test1.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @jsx createElement */

import { IndexPage, createElement, Token, Require, Literal, Component } from "../../src";
import { IndexPage, createElement, Token, Require, Literal, Component, xarcJsxElement } from "../../src";

const getBogelFontUrl = () => {
return "bogel";
Expand Down Expand Up @@ -137,4 +137,6 @@ const Template = () => (
</IndexPage>
);

Template.$$typeof = xarcJsxElement;

export default Template;

0 comments on commit 5e76e01

Please sign in to comment.