Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change vite.ssrLoadModule to a plain import() when prerendering #44

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"private": true,
"scripts": {
"dev": "node server",
"build": "yarn build:clientAssets && node prerender",
"build": "yarn build:clientAssets && yarn build:server && node --experimental-specifier-resolution=node prerender",
"build:clientAssets": "vite build --outDir dist/static",
"build:server": "vite --config vite.config.server.js build --outDir dist/server --ssr src/entry-server.jsx",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"page": "tropical-scaffold --type=page",
Expand Down
21 changes: 6 additions & 15 deletions prerender.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import fse from 'fs-extra'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { createServer as createViteServer } from 'vite'

const dir = dirname(fileURLToPath(import.meta.url))

const transformedTemplate = fse.readFileSync(
resolve(dir, 'dist/static/index.html'),
'utf-8'
)

async function prerender () {
const vite = await createViteServer({
server: { middlewareMode: 'ssr' }
})
const transformedTemplate = fse.readFileSync(resolve(dir, 'dist/static/index.html'), 'utf-8')

async function prerender() {
try {
const { Renderer } = await vite.ssrLoadModule('/src/entry-server.jsx')
const { Renderer } = await import('./dist/server/entry-server.js')
const renderer = new Renderer(transformedTemplate)

Object.entries(renderer.pages).forEach(([pathname, page]) => {
Expand All @@ -35,14 +27,13 @@ async function prerender () {

const pkg = JSON.parse(await fse.readFile('./package.json'))
if (pkg.tropical.siteHost === 'https://www.example.org') {
console.log(`⚠️ Configure tropical.siteHost in package.json, otherwise links in your JSON Feed won't work!`)
console.log(
`⚠️ Configure tropical.siteHost in package.json, otherwise links in your JSON Feed won't work!`
)
}
} catch (e) {
vite.ssrFixStacktrace(e)
console.error(e)
}

await vite.close()
}

prerender()
16 changes: 16 additions & 0 deletions vite.config.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import mdx from '@mdx-js/rollup'
import rehypeSlug from 'rehype-slug'

export default defineConfig({
plugins: [react(), mdx({ rehypePlugins: [rehypeSlug] })],
build: {
assetsInlineLimit: 0,
rollupOptions: {
output: {
format: 'es'
}
}
}
})