-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(site): scientific typestyle SSR config for gatsby
- Loading branch information
1 parent
f063a76
commit b881c3e
Showing
6 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const defaultStyleId = 'typestyle-target' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const typestyle = require('typestyle') | ||
const constants = require('./constants') | ||
|
||
/** | ||
* MEMO: | ||
* Do not destructure on typestyle nor using es6 module, | ||
* or setStylesTarget cannot be resolved. | ||
* | ||
* weird... seems to be webpack module resolution related issue :thingking: | ||
*/ | ||
const setStyleTarget = typestyle.setStylesTarget | ||
|
||
const { defaultStyleId } = constants | ||
|
||
exports.onClientEntry = (_, { styleTargetId = defaultStyleId }) => { | ||
let styleTarget = document.getElementById(styleTargetId) | ||
if (!styleTarget) { | ||
const newStyleEl = document.createElement('style') | ||
newStyleEl.setAttribute('id', styleTargetId) | ||
styleTarget = document.head.appendChild(newStyleEl) | ||
} | ||
setStyleTarget(styleTarget) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as React from 'react' | ||
import { getStyles } from 'typestyle' | ||
import { renderToString } from 'react-dom/server' | ||
import { defaultStyleId } from './constants' | ||
|
||
export const onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }, { styleTargetId = defaultStyleId }) => { | ||
replaceHeadComponents([ | ||
...getHeadComponents(), | ||
React.createElement('style', { | ||
id: styleTargetId, | ||
dangerouslySetInnerHTML: { __html: getStyles() }, | ||
}), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@frantic1048/gatsby-plugin-typestyle", | ||
"description": "provides typestyle with SSR configured for Gatsby.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "frantic1048", | ||
"peerDependencies": { | ||
"typestyle": "2.0.0^" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters