@kopflos-cms/vite 0.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +9 -0
- package/index.js +28 -0
- package/lib/config.d.ts +2 -0
- package/lib/config.js +25 -0
- package/lib/log.d.ts +1 -0
- package/lib/log.js +2 -0
- package/lib/server.d.ts +2 -0
- package/lib/server.js +7 -0
- package/package.json +47 -0
- package/template.d.ts +2 -0
- package/template.js +31 -0
- package/vite.config.d.ts +2 -0
- package/vite.config.js +7 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { KopflosPlugin } from '@kopflos-cms/core';
|
|
2
|
+
export { defineConfig } from 'vite';
|
|
3
|
+
export interface Options {
|
|
4
|
+
configPath?: string;
|
|
5
|
+
root?: string;
|
|
6
|
+
outDir?: string;
|
|
7
|
+
entrypoints?: string[];
|
|
8
|
+
}
|
|
9
|
+
export default function ({ outDir, ...options }: Options): KopflosPlugin;
|
package/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { build } from 'vite';
|
|
4
|
+
import { createViteServer } from './lib/server.js';
|
|
5
|
+
import { prepareConfig } from './lib/config.js';
|
|
6
|
+
import { log } from './lib/log.js';
|
|
7
|
+
export { defineConfig } from 'vite';
|
|
8
|
+
export default function ({ outDir = 'dist', ...options }) {
|
|
9
|
+
return {
|
|
10
|
+
async beforeMiddleware(host, { env }) {
|
|
11
|
+
if (env.kopflos.config.mode === 'development') {
|
|
12
|
+
log.info('Development UI mode. Creating Vite server...');
|
|
13
|
+
const viteServer = await createViteServer(options);
|
|
14
|
+
host.use(viteServer.middlewares);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
log.info('Serving UI from build directory');
|
|
18
|
+
const buildDir = resolve(process.cwd(), outDir);
|
|
19
|
+
log.debug('Build directory:', buildDir);
|
|
20
|
+
host.use('/assets', express.static(resolve(buildDir, 'assets')));
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
async build() {
|
|
24
|
+
log.info('Building UI...');
|
|
25
|
+
await build(await prepareConfig({ outDir, ...options }));
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
package/lib/config.d.ts
ADDED
package/lib/config.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { glob } from 'glob';
|
|
3
|
+
import { mergeConfig } from 'vite';
|
|
4
|
+
import defaultConfig from '../vite.config.js';
|
|
5
|
+
export async function prepareConfig({ root, configPath, entrypoints, outDir }) {
|
|
6
|
+
const inputConfig = {
|
|
7
|
+
root,
|
|
8
|
+
build: {
|
|
9
|
+
emptyOutDir: true,
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
if (outDir) {
|
|
13
|
+
inputConfig.build.outDir = resolve(process.cwd(), outDir);
|
|
14
|
+
}
|
|
15
|
+
if (entrypoints) {
|
|
16
|
+
inputConfig.build.rollupOptions = {
|
|
17
|
+
input: entrypoints.flatMap(entry => glob.sync(entry)),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (configPath) {
|
|
21
|
+
const userConfig = await import(configPath);
|
|
22
|
+
return mergeConfig(mergeConfig(defaultConfig, inputConfig), userConfig.default);
|
|
23
|
+
}
|
|
24
|
+
return mergeConfig(defaultConfig, inputConfig);
|
|
25
|
+
}
|
package/lib/log.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const log: import("anylogger").Logger<import("anylogger").BaseLevels>;
|
package/lib/log.js
ADDED
package/lib/server.d.ts
ADDED
package/lib/server.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kopflos-cms/vite",
|
|
3
|
+
"version": "0.0.1-beta.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "Zazuko GmbH",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "mocha",
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"prepack": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib/*.js",
|
|
15
|
+
"lib/*.d.ts",
|
|
16
|
+
"*.js",
|
|
17
|
+
"*.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git://github.com/zazuko/kopflos.git",
|
|
22
|
+
"directory": "packages/vite"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/zazuko/kopflos/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/zazuko/kopflos",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@kopflos-cms/logger": "^0.1.0-beta.0",
|
|
30
|
+
"express": "^5.0.1",
|
|
31
|
+
"glob": "^11.0.0",
|
|
32
|
+
"onetime": "^7.0.0",
|
|
33
|
+
"vite": "^5.4.8"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/glob": "^8.1.0",
|
|
37
|
+
"chai": "^5.1.1"
|
|
38
|
+
},
|
|
39
|
+
"mocha": {
|
|
40
|
+
"extension": [
|
|
41
|
+
"ts"
|
|
42
|
+
],
|
|
43
|
+
"spec": "test/**/*.test.ts",
|
|
44
|
+
"loader": "ts-node/esm/transpile-only",
|
|
45
|
+
"require": "../../mocha-setup.js"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/template.d.ts
ADDED
package/template.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createViteServer } from './lib/server.js';
|
|
2
|
+
import { log } from './lib/log.js';
|
|
3
|
+
async function prepareDevTemplate(env, subject, template) {
|
|
4
|
+
const config = env.kopflos.config.plugins?.['@kopflos-cms/vite'];
|
|
5
|
+
const vite = await createViteServer(config || {});
|
|
6
|
+
const subjectPath = new URL(subject.value).pathname;
|
|
7
|
+
return vite.transformIndexHtml(subjectPath, template);
|
|
8
|
+
}
|
|
9
|
+
export const transform = () => async ({ subject, env }, response) => {
|
|
10
|
+
if (!isHtmlResponse(response)) {
|
|
11
|
+
throw new Error('Vite handler must be chained after another which returns a HTML response');
|
|
12
|
+
}
|
|
13
|
+
if (env.kopflos.config.mode === 'production') {
|
|
14
|
+
return response;
|
|
15
|
+
}
|
|
16
|
+
log.debug('Compiling page template');
|
|
17
|
+
return {
|
|
18
|
+
...response,
|
|
19
|
+
body: await prepareDevTemplate(env, subject, response.body),
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
function isHtmlResponse(response) {
|
|
23
|
+
return typeof response?.body === 'string' || hasHeader(response?.headers, 'Content-Type', 'text/html');
|
|
24
|
+
}
|
|
25
|
+
function hasHeader(headers, headerName, headerValue) {
|
|
26
|
+
const normalizedHeaderName = headerName.toLowerCase();
|
|
27
|
+
return !!headers && Object.entries(headers)
|
|
28
|
+
.some(([key, value]) => {
|
|
29
|
+
return key.toLowerCase() === normalizedHeaderName && (value === headerValue || (Array.isArray(value) && value.includes(headerValue)));
|
|
30
|
+
});
|
|
31
|
+
}
|
package/vite.config.d.ts
ADDED