Skip to content

Commit

Permalink
use own runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Jul 25, 2024
1 parent 2ee7fd9 commit dfd11e8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/example-project/component-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@stencil/angular-output-target": "workspace:*",
"@stencil/core": "4.19.2-dev.1720817033.ad4ba40",
"@stencil/core": "4.19.2-dev.1721945383.1ecdb19",
"@stencil/react-output-target": "workspace:*",
"@stencil/vue-output-target": "workspace:*",
"@types/puppeteer": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const config: Config = {
}),
{
type: 'dist-custom-elements',
externalRuntime: false,
dir: 'components'
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Button() {
const [inputEvent, setInputEvent] = useState<number>(0);
return (
<>
<MyButton href="#" onClick={(e) => setInputEvent(inputEvent + 1)}>
<MyButton href="#" onClick={() => setInputEvent(inputEvent + 1)}>
Click me <b>now</b>!
<MyComponent first="Stencil" last="'Don't call me a framework' JS">Hello</MyComponent>
</MyButton>
Expand Down
33 changes: 19 additions & 14 deletions packages/react-output-target/src/react/ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const createComponentForServerSideRendering = <I extends HTMLElement, E e

let stringProps = '';
for (const [key, value] of Object.entries(props)) {
if ((typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean')) {
if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') {
continue;
}
stringProps += ` ${key}="${value}"`;
Expand Down Expand Up @@ -92,13 +92,14 @@ export const createComponentForServerSideRendering = <I extends HTMLElement, E e
/**
* cut out the inner content of the component
*/
const [__html, hydrationComment] = html
.slice(startTag.length, -endTag.length)
.replace('<template shadowrootmode="open">', '')
.split('</template>');
if (!__html) {
throw new Error('No HTML returned from renderToString');
}
const templateStartTag = '<template shadowrootmode="open">';
const templateEndTag = '</template>';
const hydrationComment = '<!--r.1-->';
const __html = html.slice(startTag.length, -endTag.length).startsWith(templateStartTag)
? html
.slice(startTag.length, -endTag.length)
.slice(templateStartTag.length, -(templateEndTag + hydrationComment).length)
: html.slice(startTag.length, -endTag.length);

/**
* `html-react-parser` is a Node.js dependency so we should make sure to only import it when run on the server
Expand All @@ -117,11 +118,16 @@ export const createComponentForServerSideRendering = <I extends HTMLElement, E e
* remove the outer tag (e.g. `options.tagName`) so we only have the inner content
*/
const CustomTag = `${options.tagName}`;
return <CustomTag {...props}>
{/* @ts-expect-error */}
<template shadowrootmode="open" dangerouslySetInnerHTML={{ __html: (hydrationComment || '') + __html! }}></template>
{children}
</CustomTag>;
return (
<CustomTag {...props}>
<template
// @ts-expect-error
shadowrootmode="open"
dangerouslySetInnerHTML={{ __html: hydrationComment + __html }}
></template>
{children}
</CustomTag>
);
}

return;
Expand All @@ -131,4 +137,3 @@ export const createComponentForServerSideRendering = <I extends HTMLElement, E e
return <StencilElement />;
}) as unknown as ReactWebComponent<I, E>;
};

22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfd11e8

Please sign in to comment.