From 68cee78d5987695c64b7c8e56cbefbb5c6492991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Nobile?= Date: Wed, 19 Jun 2024 18:44:30 +0200 Subject: [PATCH] feat(vite-plugin): add build options and middleware --- packages/vite-plugin/src/index.ts | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/vite-plugin/src/index.ts b/packages/vite-plugin/src/index.ts index 3e87e94ed3..537ebd84c1 100644 --- a/packages/vite-plugin/src/index.ts +++ b/packages/vite-plugin/src/index.ts @@ -1,13 +1,49 @@ import { createHash } from 'node:crypto' +import { resolve } from 'node:path' import { Plugin } from 'vite' type ContemberOptions = { buildVersion?: boolean + disableMiddleware?: boolean, } export function contember(options?: ContemberOptions): Plugin { + const contemberDsn = process.argv.find(it => it.includes('contember://')) + const projectName = contemberDsn ? new URL(contemberDsn).username : null + + const defineConfig = projectName ? { + 'import.meta.env.VITE_CONTEMBER_ADMIN_PROJECT_NAME': JSON.stringify(projectName), + } : {} + return ({ name: 'contember', + config(config) { + return { + define: defineConfig, + base: '/', + ...config, + build: { + ...config.build, + rollupOptions: { + ...config.build?.rollupOptions, + input: config.build?.rollupOptions?.input ?? { + root: resolve(__dirname, './index.html'), + app: resolve(__dirname, './app/index.html'), + }, + }, + }, + } + }, + configureServer(serve) { + if (!options?.disableMiddleware) { + return () => serve.middlewares.use((req, res, next) => { + if (req.url === '/app' || req.url?.startsWith('/app/') && !req.url?.match(/\.\w+($|\?)/)) { + req.url = '/app/' + } + next() + }) + } + }, transformIndexHtml: options?.buildVersion === false ? undefined : {