-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) | ||
|
||
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' } | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
try { | ||
const { Renderer } = await vite.ssrLoadModule('/src/entry-server.jsx') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... then use that dev server to access This is now more consistent with what's happening in our |
||
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() |
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, these changes are just to replace |
||
|
||
async function createServer() { | ||
const app = express() | ||
|
@@ -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) | ||
|
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] })], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We no longer need the extra |
||
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') | ||
} | ||
} | ||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are just to replace
__dirname
, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#what-do-i-use-instead-of-__dirname-and-__filename