From 04815100656eaf2acaa07cf2011e24e16362b53d Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Fri, 1 Jul 2022 02:03:29 +0200 Subject: [PATCH] Added a routes.js file for writing routes and entry html files only once --- .gitignore | 3 ++- README.md | 4 +++- _frontend/routes.js | 3 +++ _frontend/server.js | 7 +++---- _frontend/vite.config.js | 12 +++++++++--- 5 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 _frontend/routes.js diff --git a/.gitignore b/.gitignore index e2857a7..815d0cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ # NodeJS node_modules/ +dist/ -# Server +# Server Executable server # Local Files diff --git a/README.md b/README.md index 645d68b..80d2205 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,11 @@ Minimal boilerplate project for a Golang server using [Fiber](https://github.com This is a Vite project for building all the static pages used by this app. + The `routes.js` (this is used both from `server.js` for _serving_ and from `vite.config.js` for _building_) file contains a mapping from express route patterns to entry html files, this is useful for rendering the same page for multiple urls in development mode. + - `database/` - Module with a `Database` interface and two implementation: `memDB` is an in-memory database for testing purposes while `sqliteDB` is a wrapper for working with an SQLite database. + Module with a `Database` interface and two implementation: `memDB` is an in-memory database for testing purposes. `sqliteDB` is a wrapper for working with an SQLite database. - `routes/` diff --git a/_frontend/routes.js b/_frontend/routes.js new file mode 100644 index 0000000..e550119 --- /dev/null +++ b/_frontend/routes.js @@ -0,0 +1,3 @@ +export default { + '/': './index.html', +} diff --git a/_frontend/server.js b/_frontend/server.js index e544810..ef37a31 100644 --- a/_frontend/server.js +++ b/_frontend/server.js @@ -3,6 +3,8 @@ import express from 'express' import { createServer as createViteServer } from 'vite' import { fileURLToPath } from 'url' +import routes from './routes.js' + const __dirname = dirname(fileURLToPath(import.meta.url)) async function createServer(customHtmlRoutes) { @@ -28,7 +30,4 @@ async function createServer(customHtmlRoutes) { app.listen(3000) } -createServer({ - // Useful for developing with dynamic routes - '/': './index.html', -}) +createServer(routes) diff --git a/_frontend/vite.config.js b/_frontend/vite.config.js index f852793..9054f51 100644 --- a/_frontend/vite.config.js +++ b/_frontend/vite.config.js @@ -1,11 +1,17 @@ +import { basename } from 'path' import { defineConfig } from 'vite' +// Routes +import routes from './routes.js' + +const entryPoints = Object.fromEntries( + Object.values(routes).map(path => [basename(path, '.html'), path]) +) + export default defineConfig({ build: { rollupOptions: { - input: { - '/index': 'index.html', - }, + input: entryPoints, }, }, server: {