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

Mount HTML serving middlewares at config.base #91

Merged
merged 5 commits into from
Nov 17, 2023
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
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ async function injectViteIndexMiddleware(
) {
const config = await getViteConfig();

app.get("/*", async (req, res, next) => {
app.use(config.base, async (req, res, next) => {
if (req.method !== "GET") return next();

if (isIgnoredPath(req.path, req)) return next();

if (isStaticFilePath(req.path)) next();
Expand All @@ -230,11 +232,12 @@ async function injectViteIndexMiddleware(

async function injectIndexMiddleware(app: core.Express) {
const distPath = await getDistPath();
const config = await getViteConfig();

app.use("*", (req, res, next) => {
if (isIgnoredPath(req.baseUrl, req)) return next();
app.use(config.base, (req, res, next) => {
if (isIgnoredPath(req.path, req)) return next();

const indexPath = findClosestIndexToRoot(req.originalUrl, distPath);
const indexPath = findClosestIndexToRoot(req.path, distPath);
if (indexPath === undefined) return next();

const html = fs.readFileSync(indexPath, "utf8");
Expand Down
13 changes: 6 additions & 7 deletions tests/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,24 @@ for (const template of templates) {
test(`Template "${template}" with custom inline config`, async (done) => {
process.chdir(`create-vite-express/templates/${template}`);

const base = "/admin";

ViteExpress.config({
inlineViteConfig: {
base: "/admin",
build: { outDir: "out" },
},
inlineViteConfig: { base, build: { outDir: "out" } },
});
await ViteExpress.build();

await testCase(template, done);
await testCase(template, done, base);
});
}

const testCase = async (template: string, done: () => void) => {
const testCase = async (template: string, done: () => void, base = "/") => {
const server = ViteExpress.listen(express(), 3000, () => {
const browser = puppeteer.launch();

browser.then(async (browser) => {
const page = await browser.newPage();
await page.goto("http://localhost:3000");
await page.goto(`http://localhost:3000${base}`);

it("test set up");

Expand Down