Skip to content

Commit

Permalink
feat(vite-plugin): add build options and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnobile authored and matej21 committed Jun 27, 2024
1 parent 70d2a7e commit d601464
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 d601464

Please sign in to comment.