-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from bensmithett/esmodule
Change project to "type": "module" so we can use pure ESM dependencies
- Loading branch information
Showing
7 changed files
with
2,127 additions
and
1,768 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -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' } | ||
}) | ||
|
||
try { | ||
const { Renderer } = await vite.ssrLoadModule('/src/entry-server.jsx') | ||
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() |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.