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

feat: small vite+template improvements #195

Merged
merged 5 commits into from
Nov 8, 2024
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
5 changes: 5 additions & 0 deletions .changeset/many-dolphins-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/vite": patch
---

The plugin populates a variable `VITE.basePath` which will be resolved to the correct path in development and production mode
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.
2 changes: 1 addition & 1 deletion labs/html-template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function bindTemplate<A extends unknown[] = unknown[]>(evaluateTe
return new Error('Template handler must be chained after another which returns a HTML response')
}

let dataset: DatasetCore | undefined
let dataset: DatasetCore = context.subject.dataset

if (fetchData) {
const templateData = fetchData(...args)(context)
Expand Down
5 changes: 5 additions & 0 deletions labs/html-template/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ exports[`@kopflos-labs/html-template fetches data with given arguments 1`] = `
"_:c14n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Foo> .
"
`;

exports[`@kopflos-labs/html-template uses core representation a default template data 1`] = `
"<> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Foo> .
"
`;
33 changes: 33 additions & 0 deletions labs/html-template/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ import bindTemplate from '../index.js'
describe('@kopflos-labs/html-template', () => {
use(snapshots)

it('uses core representation a default template data', async () => {
// given
const templateFunc = sinon.stub()
const context = {
subject: rdf.clownface()
.namedNode('')
.addOut(rdf.ns.rdf.type, rdf.namedNode('http://example.com/Foo')),
env: createEnv({
baseIri: 'http://example.com/',
sparql: {
default: 'http://example.com/query',
},
}),
} as HandlerArgs
const previousResponse = {
status: 200,
body: '<html><body><template target-class="Foo"></template></body></html>',
headers: {
'Content-Type': 'text/html',
},
}

// when
const handler = bindTemplate(templateFunc)
await handler(context, previousResponse)

// then
const templateData: AnyPointer = templateFunc.firstCall.args[1].pointer
expect(templateData.dataset).canonical.toMatchSnapshot()
})

it('fetches data with given arguments', async () => {
// given
const templateFunc = sinon.stub()
Expand All @@ -20,6 +51,7 @@ describe('@kopflos-labs/html-template', () => {
.dataset
})
const context = {
subject: rdf.clownface().namedNode(''),
env: createEnv({
baseIri: 'http://example.com/',
sparql: {
Expand Down Expand Up @@ -55,6 +87,7 @@ describe('@kopflos-labs/html-template', () => {
.dataset.toStream()
})
const context = {
subject: rdf.clownface().namedNode(''),
env: createEnv({
baseIri: 'http://example.com/',
sparql: {
Expand Down
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
4 changes: 4 additions & 0 deletions packages/core/test/lib/env/SparqlClientFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('lib/env/SparqlClientFactory', () => {
default: 'http://example.com/sparql',
},
},
variables: {},
}
}
}
Expand Down Expand Up @@ -63,6 +64,7 @@ describe('lib/env/SparqlClientFactory', () => {
},
},
},
variables: {},
}
}
}
Expand Down Expand Up @@ -112,6 +114,7 @@ describe('lib/env/SparqlClientFactory', () => {
},
},
},
variables: {},
}
}
}
Expand Down Expand Up @@ -148,6 +151,7 @@ describe('lib/env/SparqlClientFactory', () => {

get kopflos() {
return {
variables: {},
config: {
baseIri: 'http://example.com/',
sparql: {
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 @@
}

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