Skip to content

Commit

Permalink
Merge pull request #728 from contember/feat/vite-plugin
Browse files Browse the repository at this point in the history
feat(vite-plugin): add build options and middleware
  • Loading branch information
matej21 authored Jun 27, 2024
2 parents 70d2a7e + d601464 commit 105f669
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
import { createHash } from 'node:crypto'
import { resolve } from 'node:path'
import { Plugin } from 'vite'

type ContemberOptions = {
buildVersion?: boolean
disableMiddleware?: boolean,
appPath?: string,
}

export function contember(options?: ContemberOptions): Plugin {
const appPath = options?.appPath ?? '/app'
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, `.${appPath}/index.html`),
},
},
},
}
},
configureServer(serve) {
if (!options?.disableMiddleware) {
return () => serve.middlewares.use((req, res, next) => {
if (req.url === appPath || req.url?.startsWith(`${appPath}/`) && !req.url?.match(/\.\w+($|\?)/)) {
req.url = `${appPath}/`
}
next()
})
}
},
transformIndexHtml: options?.buildVersion === false
? undefined
: {
Expand Down

0 comments on commit 105f669

Please sign in to comment.