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 project to "type": "module" so we can use pure ESM dependencies #38

Merged
merged 1 commit into from
Feb 1, 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
File renamed without changes.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@babel/core": "^7.14.6",
"@mdx-js/mdx": "^1.6.22",
"@mdx-js/react": "^1.6.22",
"@mdx-js/rollup": "^2.0.0-rc.1",
"@storybook/addon-actions": "^6.4.8",
"@storybook/addon-essentials": "^6.4.8",
"@storybook/addon-links": "^6.4.8",
Expand All @@ -21,23 +22,22 @@
"react-dom": "^17.0.0",
"react-fela": "^11.6.1",
"react-head": "^3.4.0",
"rehype-slug": "^4.0.1",
"rehype-slug": "^5.0.1",
"storybook-builder-vite": "^0.1.10",
"tropical-islands": "^0.1.2",
"tropical-scaffold": "^0.1.1",
"vite": "^2.4.2",
"vite-plugin-mdx": "^3.5.6"
"tropical-islands": "^1.0.0",
"tropical-scaffold": "^1.0.0",
"vite": "^2.4.2"
},
"engines": {
"node": ">=16"
},
"type": "module",
"license": "MIT",
"private": true,
"scripts": {
"dev": "node server",
"build": "yarn build:clientAssets && yarn build:server && node prerender",
"build": "yarn build:clientAssets && 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
67 changes: 43 additions & 24 deletions prerender.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
const fse = require('fs-extra')
const path = require('path')
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))
Comment on lines +2 to +6
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const transformedTemplate = fse.readFileSync(
path.resolve(__dirname, 'dist/static/index.html'),
resolve(dir, 'dist/static/index.html'),
'utf-8'
)
const { Renderer } = require('./dist/server/entry-server.js')

const renderer = new Renderer(transformedTemplate)

Object.entries(renderer.pages).forEach(([pathname, page]) => {
const { body } = renderer.render(pathname)
const filePath = `dist/static${page.filePath}.html`
fse.outputFileSync(path.resolve(__dirname, filePath), body)
console.log('🖨 Prerendered', filePath)
})
Object.entries(renderer.feeds).forEach(([pathname, feed]) => {
const body = feed(renderer.pages)
const filePath = `dist/static${pathname}`
fse.outputFileSync(path.resolve(__dirname, filePath), body)
console.log('🖨 Prerendered', filePath)
})
console.log('🦖 Your static site is ready to deploy from dist/static')

const pkg = require('./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!`)

async function prerender () {
const vite = await createViteServer({
server: { middlewareMode: 'ssr' }
})
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of these changes are mostly the same as before. We've just wrapped everything in an async function so that we can create a Vite dev server...


try {
const { Renderer } = await vite.ssrLoadModule('/src/entry-server.jsx')
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... then use that dev server to access vite.ssrLoadModule.

This is now more consistent with what's happening in our server.js.

const renderer = new Renderer(transformedTemplate)

Object.entries(renderer.pages).forEach(([pathname, page]) => {
const { body } = renderer.render(pathname)
const filePath = `dist/static${page.filePath}.html`
fse.outputFileSync(resolve(dir, filePath), body)
console.log('🖨 Prerendered', filePath)
})
Object.entries(renderer.feeds).forEach(([pathname, feed]) => {
const body = feed(renderer.pages)
const filePath = `dist/static${pathname}`
fse.outputFileSync(resolve(dir, filePath), body)
console.log('🖨 Prerendered', filePath)
})
console.log('🦖 Your static site is ready to deploy from dist/static')

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!`)
}
} catch (e) {
vite.ssrFixStacktrace(e)
console.error(e)
}

await vite.close()
}

prerender()
13 changes: 8 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const fs = require('fs')
const path = require('path')
const express = require('express')
const { createServer: createViteServer } = require('vite')
import fse from 'fs-extra'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import express from 'express'
import { createServer as createViteServer } from 'vite'

const dir = dirname(fileURLToPath(import.meta.url))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


async function createServer() {
const app = express()
Expand All @@ -14,7 +17,7 @@ async function createServer() {
const { pathname } = new URL(`${req.protocol}://${req.get('host')}${req.originalUrl}`)

try {
const template = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8')
const template = fse.readFileSync(resolve(dir, 'index.html'), 'utf-8')
const transformedTemplate = await vite.transformIndexHtml(pathname, template)
const { Renderer } = await vite.ssrLoadModule('/src/entry-server.jsx')
const renderer = new Renderer(transformedTemplate)
Expand Down
17 changes: 11 additions & 6 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { defineConfig } from 'vite'
import path from 'path'
import { config } from './vite.config.server'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import react from '@vitejs/plugin-react'
import mdx from '@mdx-js/rollup'
import rehypeSlug from 'rehype-slug'

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

export default defineConfig({
plugins: config.plugins,
plugins: [react(), mdx({ rehypePlugins: [rehypeSlug] })],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need the extra vite.config.server.js due to the changes in prerender.js, so its config has moved in here.

build: {
...config.build,
assetsInlineLimit: 0,
rollupOptions: {
input: {
client: path.resolve(__dirname, 'index.html'),
client: resolve(dir, 'index.html'),
// We'll never actually use this bundle, but need it to build assets that are only referenced by SSR pages
ssrAssetCollector: path.resolve(__dirname, 'src/entry-server.jsx')
ssrAssetCollector: resolve(dir, 'src/entry-server.jsx')
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions vite.config.server.js

This file was deleted.

Loading