Skip to content

Commit

Permalink
Fix mpa routing in build and dev
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong committed Mar 5, 2023
1 parent 1ec7c31 commit 27f474a
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 267 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pages/logviewer.html → logviewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
</nav>
<main></main>
<aside></aside>
<script type="module" src="../logviewer/main.js"></script>
<script type="module" src="./src/main.js"></script>
</body>

</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[[redirects]]
from = "/logviewer"
to = "/logviewer.html"
to = "/logviewer/index.html"
status = 200

[[redirects]]
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"build": "tsc && vite build",
"build:scripting-runtime": "./src/engine/scripting/emscripten/build.sh",
"build:examples": "./examples/build.sh",
"preview": "vite-node ./preview-server.ts",
"preview:testnet": "VITE_USE_TESTNET=true yarn preview",
"typecheck": "tsc --noEmit",
"lint": "npm run lint:js && npm run lint:css",
"lint:js": "eslint ./src",
Expand Down Expand Up @@ -97,7 +95,6 @@
"@vitejs/plugin-react": "^3.1.0",
"@vitest/ui": "^0.29.2",
"c8": "^7.11.0",
"chokidar": "^3.5.3",
"connect-history-api-fallback": "^2.0.0",
"eslint": "^8.12.0",
"eslint-config-google": "^0.14.0",
Expand All @@ -109,7 +106,6 @@
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.4.0",
"eslint-plugin-unicorn": "^46.0.0",
"glob": "^9.2.1",
"husky": "^8.0.3",
"jsdom": "^21.1.0",
"postcss": "^8.4.12",
Expand Down
75 changes: 0 additions & 75 deletions preview-server.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/live-reload.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "./live-reload";
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, useLocation, useNavigationType, createRoutesFromChildren, matchRoutes } from "react-router-dom";
Expand Down
53 changes: 12 additions & 41 deletions src/vite/mpa-router.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,24 @@
import { Connect, PluginOption } from "vite";
import { glob } from "glob";
import path from "path";
import history, { Rewrite } from "connect-history-api-fallback";
import history from "connect-history-api-fallback";

export function mpaRouter(): PluginOption {
return {
name: "vite-plugin-mpa-router",
enforce: "pre",
async config(config) {
const pages = await glob("../pages/**/*.html", { cwd: __dirname });

const input: { [key: string]: string } = {};

for (const pagePath of pages) {
const name = path.basename(pagePath, ".html");
input[name] = path.resolve(__dirname, pagePath);
}

config.build = config.build || {};
config.build.rollupOptions = config.build.rollupOptions || {};
config.build.rollupOptions.input = input;
},
async configureServer(server) {
const pages = await glob("../pages/**/*.html", { cwd: __dirname });

const rewrites: Rewrite[] = [];

for (const pagePath of pages) {
const name = path.basename(pagePath, ".html");
const to = `./src/pages/${name}.html`;

if (name === "index") {
rewrites.push({
from: /^\/$/,
to,
});
}

rewrites.push({
from: new RegExp(`^/${name}$`),
to,
});
}

server.middlewares.use(
history({
htmlAcceptHeaders: ["text/html", "application/xhtml+xml"],
index: "./src/pages/index.html",
rewrites,
index: "./index.html",
rewrites: [
{
from: /^\/logviewer$/,
to: "./logviewer/index.html",
},
{
from: /^\/$/,
to: "./index.html",
},
],
}) as Connect.NextHandleFunction
);
},
Expand Down
File renamed without changes.
10 changes: 1 addition & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,5 @@
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"./src",
"deps.d.ts",
"./test",
"./preview-server.ts",
"./vite.config.ts",
"types/WebSG.d.ts",
"docs/examples/light/light.js"
]
"include": ["./src", "./deps.d.ts", "./test", "./vite.config.ts", "./logviewer/src"]
}
73 changes: 0 additions & 73 deletions types/WebSG.d.ts

This file was deleted.

9 changes: 8 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { serviceWorkerPlugin } from "@gautemo/vite-plugin-service-worker";
import { viteStaticCopy } from "vite-plugin-static-copy";
import path from "path";

import testnetServerPlugin from "./src/testnet";
import { mpaRouter } from "./src/vite/mpa-router";
import testnetServerPlugin from "./src/vite/testnet";

// https://vitejs.dev/config/
export default defineConfig({
appType: "mpa",
base: "/",
server: {
port: 3000,
Expand Down Expand Up @@ -55,6 +56,12 @@ export default defineConfig({
},
build: {
sourcemap: true,
rollupOptions: {
input: {
main: path.resolve(__dirname, "index.html"),
logviewer: path.resolve(__dirname, "logviewer/index.html"),
},
},
},
test: {
globals: true,
Expand Down
Loading

0 comments on commit 27f474a

Please sign in to comment.