Skip to content

Commit

Permalink
Browser Testing
Browse files Browse the repository at this point in the history
Did some testing to see if this works in the browser really, and it actually does! Super cool! It even works on iOS.

I guess it's more of a TypeScript thing to test, since it more depends on that being able to transpile down to runnable-JS.

I also collapsed some of the `createElement()` types to use the JSX ones I already defined in the `namespace JSX` declaration.

Found an issue with importing a component from a module, as both a type, and a side-effect declaration, so I had to look into a setting to configure how the imports should be handled for other scripts. Another way around from running into this is to simply load the script with an inline-import (`import "./web-component.js"`), versus as a default/named import (`import { WebComponent } from "./web-component.js"`). The problem is that TypeScript doesn't see this script as a side-effect script, while it is still required to be able to define `WebComponent` as a custom element for the window. So if you do these on two separate lines, instead of one + the config change, then you don't have to change your TypeScript config settings for that.
  • Loading branch information
Offroaders123 committed Jan 26, 2023
1 parent bbe3307 commit 9ccff98
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 20 deletions.
12 changes: 1 addition & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@
<title>dino-jsx</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="importmap">
{
"imports": {
"texsaur": "./node_modules/texsaur/dist/index.js",
"./node_modules/texsaur/dist/index": "./node_modules/texsaur/dist/index.js",
"./node_modules/texsaur/dist/dom": "./node_modules/texsaur/dist/dom.js",
"./node_modules/texsaur/dist/svg": "./node_modules/texsaur/dist/svg.js"
}
}
</script>
<script type="module" src="./dist/app.js"></script>
</head>

<body>
<script type="module" src="./dist/app.js"></script>
</body>

</html>
4 changes: 2 additions & 2 deletions src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createElement } from "./index.js";
import type Epic from "./epic.js";
import * as JSX from "./index.js";
import Epic from "./epic.js";

const myDiv = <div></div> as HTMLDivElement;

Expand Down
2 changes: 1 addition & 1 deletion src/epic.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from "./index.js";
import * as JSX from "./index.js";

export class Epic extends HTMLElement {
readonly shadowRoot = this.attachShadow({ mode: "open" });
Expand Down
6 changes: 3 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ declare global {
type Fragment = Node[];

interface IntrinsicElements extends IntrinsicElementsTagNameMap {}

type IntrinsicElementsTagNameMap = {
[K in keyof HTMLElementTagNameMap]: Partial<HTMLElementTagNameMap[K]>;
};
}
}
}

export function createElement<K extends keyof HTMLElementTagNameMap>(tagName: K, attributes?: Partial<HTMLElementTagNameMap[K]> | null, ...children: Node[]): HTMLElementTagNameMap[K] {
export function createElement<K extends keyof JSX.IntrinsicElements>(tagName: K, attributes?: JSX.IntrinsicElementsTagNameMap[K] | null, ...children: Node[]): JSX.IntrinsicElementsTagNameMap[K] {
const element = document.createElement(tagName);

if (attributes !== null && attributes !== undefined){
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"moduleResolution": "Node",
"target": "ESNext",
"jsx": "react",
"jsxFactory": "createElement",
"jsxFragmentFactory": "Fragment",
"strict": true
"jsxFactory": "JSX.createElement",
"jsxFragmentFactory": "JSX.Fragment",
"strict": true,
"importsNotUsedAsValues": "preserve"
}
}

0 comments on commit 9ccff98

Please sign in to comment.