Skip to content

Commit

Permalink
feat: small vite+template improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 7, 2024
1 parent 6fb4851 commit 1b8a2bc
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-carrots-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-labs/html-template": patch
---

By default, pass Core Representation of the page resource to template func
5 changes: 5 additions & 0 deletions .changeset/rotten-cameras-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/core": patch
---

Added `env.kopflos.variables` to allow accessing and setting variables
5 changes: 5 additions & 0 deletions .changeset/unlucky-suns-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/vite": patch
---

Serve the entire UI build statically. Before, `public` vite resources would not be accessible
3 changes: 0 additions & 3 deletions example/kopflos.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default <KopflosConfig> {
updateUrl: 'http://localhost:7878/update',
},
},
variables: {
uiRoot: 'ui',
},
plugins: {
'@kopflos-cms/plugin-deploy-resources': {
paths: ['resources', 'resources.dev'],
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "kopflos build",
"start": "kopflos serve --mode development --trust-proxy",
"prestart:prod": "npm run build",
"start:prod": "kopflos serve --trust-proxy --variable uiRoot=dist"
"start:prod": "kopflos serve --trust-proxy"
},
"dependencies": {
"@kopflos-cms/vite": "0.0.1-beta.1",
Expand Down
2 changes: 1 addition & 1 deletion example/resources/api/index.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ PREFIX kl: <https://kopflos.described.at/>
[
a code:EcmaScriptModule ;
code:link <node:@kopflos-cms/serve-file#default> ;
code:arguments ( "${uiRoot}/${type}.html"^^code:EcmaScriptTemplateLiteral ) ;
code:arguments ( "${VITE.basePath}/${type}.html"^^code:EcmaScriptTemplateLiteral ) ;
]
[
a code:EcmaScriptModule ;
Expand Down
File renamed without changes.
18 changes: 10 additions & 8 deletions labs/html-template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@ export interface TemplateDataFunc {
(context: HandlerArgs): Promise<DatasetCore> | DatasetCore | Stream
}

export default function bindTemplate<A extends unknown[] = unknown[]>(evaluateTemplate: TemplateFunc, fetchData?: (...args: A) => TemplateDataFunc, ...args: A): Handler {
export default function bindTemplate<A extends unknown[] = unknown[]>(evaluateTemplate: TemplateFunc, fetchData: (...args: A) => TemplateDataFunc = coreRepresentation, ...args: A): Handler {
return async (context, response) => {
if (typeof response?.body !== 'string') {
return new Error('Template handler must be chained after another which returns a HTML response')
}

let dataset: DatasetCore | undefined

if (fetchData) {
const templateData = fetchData(...args)(context)
if ('then' in templateData || 'size' in templateData) {
dataset = await templateData
} else {
dataset = await context.env.dataset().import(templateData)
}
const templateData = fetchData(...args)(context)
if ('then' in templateData || 'size' in templateData) {
dataset = await templateData
} else {
dataset = await context.env.dataset().import(templateData)
}
const graph = context.env.clownface({ dataset })

Expand All @@ -45,3 +43,7 @@ export default function bindTemplate<A extends unknown[] = unknown[]>(evaluateTe
}
}
}

function coreRepresentation(): TemplateDataFunc {
return ({ subject }) => subject.dataset
}

Check warning on line 49 in labs/html-template/index.ts

View check run for this annotation

Codecov / codecov/patch

labs/html-template/index.ts#L47-L49

Added lines #L47 - L49 were not covered by tests
18 changes: 12 additions & 6 deletions packages/core/lib/env/KopflosFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import type { KopflosConfig } from '../Kopflos.js'
export interface KopflosFactory {
readonly kopflos: {
readonly config: KopflosConfig
readonly variables: Record<string, unknown>
}
}

export default (config: KopflosConfig) => class implements KopflosFactory {
get kopflos() {
return {
config: Object.freeze(config),
export default (config: KopflosConfig) => {
const variables = config.variables || {}

return class implements KopflosFactory {
get kopflos() {
return {
config: Object.freeze(config),
variables,
}
}
}

static exports = ['kopflos']
static exports = ['kopflos']
}
}
2 changes: 1 addition & 1 deletion packages/core/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const loadHandlers: HandlerLookup = ({ resourceShape, ...rest }: Resource
.out(env.ns.kopflos.handler)
.filter(matchingMethod(env, method))

const vars = new Map(Object.entries(env.kopflos.config.variables || {}))
const vars = new Map(Object.entries(env.kopflos.variables))
if ('subjectVariables' in rest) {
for (const [k, v] of rest.subjectVariables) {
vars.set(k, v)
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'node:path'
import type { KopflosPlugin } from '@kopflos-cms/core'
import type { Kopflos, KopflosPlugin } from '@kopflos-cms/core'

Check warning on line 2 in packages/vite/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/index.ts#L2

Added line #L2 was not covered by tests
import express from 'express'
import { build } from 'vite'
import { createViteServer } from './lib/server.js'
Expand All @@ -22,17 +22,26 @@ declare module '@kopflos-cms/core' {
}

export default function ({ outDir = 'dist', ...options }: Options): KopflosPlugin {
const rootDir = resolve(process.cwd(), options.root || '')
const buildDir = resolve(process.cwd(), outDir)

Check warning on line 27 in packages/vite/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/index.ts#L25-L27

Added lines #L25 - L27 were not covered by tests
return {
onStart({ env }: Kopflos): Promise<void> | void {
const viteVars = {
basePath: env.kopflos.config.mode === 'development' ? rootDir : buildDir,
}
log.info('Variables', viteVars)
env.kopflos.variables.VITE = Object.freeze(viteVars)
},

Check warning on line 35 in packages/vite/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/index.ts#L29-L35

Added lines #L29 - L35 were not covered by tests
async beforeMiddleware(host: express.Router, { env }) {
if (env.kopflos.config.mode === 'development') {
log.info('Development UI mode. Creating Vite server...')
const viteServer = await createViteServer(options)
host.use(viteServer.middlewares)
} else {
log.info('Serving UI from build directory')
const buildDir = resolve(process.cwd(), outDir)
log.debug('Build directory:', buildDir)
host.use('/assets', express.static(resolve(buildDir, 'assets')))
host.use(express.static(buildDir))

Check warning on line 44 in packages/vite/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/index.ts#L44

Added line #L44 was not covered by tests
}
},
async build() {
Expand Down

0 comments on commit 1b8a2bc

Please sign in to comment.