@kopflos-cms/vite 0.1.2 → 0.2.1
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/CHANGELOG.md +20 -0
- package/index.d.ts +22 -3
- package/index.js +47 -29
- package/package.json +7 -9
- package/template.d.ts +2 -2
- package/template.js +21 -18
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @kopflos-cms/vite
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 46491b5: Relative `configPath` option will be resolved against `basePath` from kopflos environment.
|
|
8
|
+
- 0c33953: Build plugin command now receives `KopflosEnvironment` as argument
|
|
9
|
+
|
|
10
|
+
## 0.2.0
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- accad6e: Removed the type `KopflosPluginConstructor` and now all plugins are exported as classes directly and their options are passed to the constructor
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 6511af6: build(deps): bump glob from 11.0.0 to 11.1.0
|
|
19
|
+
- 4214fce: Added an accessor to the `ViteDevServer` the plugin instance
|
|
20
|
+
- cdbb172: Adopt a simpler definition of a plugin where all methods are instance methods. Kopflos instance is passed as parameter
|
|
21
|
+
- 5295694: build(deps): bump express from 5.0.1 to 5.2.0
|
|
22
|
+
|
|
3
23
|
## 0.1.2
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
1
|
+
import type { Kopflos, KopflosEnvironment, KopflosPlugin } from '@kopflos-cms/core';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import type { InlineConfig, ViteDevServer } from 'vite';
|
|
3
4
|
export { defineConfig } from 'vite';
|
|
4
5
|
export interface Options {
|
|
5
6
|
configPath?: string;
|
|
@@ -8,9 +9,27 @@ export interface Options {
|
|
|
8
9
|
outDir?: string;
|
|
9
10
|
entrypoints?: string[];
|
|
10
11
|
}
|
|
12
|
+
interface VitePlugin extends KopflosPlugin {
|
|
13
|
+
viteDevServer?: ViteDevServer;
|
|
14
|
+
}
|
|
11
15
|
declare module '@kopflos-cms/core' {
|
|
12
16
|
interface PluginConfig {
|
|
13
17
|
'@kopflos-cms/vite'?: Options;
|
|
14
18
|
}
|
|
19
|
+
interface Plugins {
|
|
20
|
+
'@kopflos-cms/vite': VitePlugin;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export default class implements VitePlugin {
|
|
24
|
+
private readonly options;
|
|
25
|
+
readonly name = "@kopflos-cms/vite";
|
|
26
|
+
private readonly rootDir;
|
|
27
|
+
private readonly buildDir;
|
|
28
|
+
private readonly outDir;
|
|
29
|
+
private _viteDevServer?;
|
|
30
|
+
constructor(options: Options);
|
|
31
|
+
get viteDevServer(): ViteDevServer | undefined;
|
|
32
|
+
onStart({ env }: Kopflos): Promise<void> | void;
|
|
33
|
+
beforeMiddleware(host: express.Router, { env }: Kopflos): Promise<void>;
|
|
34
|
+
build(env: KopflosEnvironment): Promise<void>;
|
|
15
35
|
}
|
|
16
|
-
export default function ({ outDir, ...options }: Options): KopflosPluginConstructor;
|
package/index.js
CHANGED
|
@@ -5,35 +5,53 @@ import { createViteServer } from './lib/server.js';
|
|
|
5
5
|
import { prepareConfig } from './lib/config.js';
|
|
6
6
|
import { log } from './lib/log.js';
|
|
7
7
|
export { defineConfig } from 'vite';
|
|
8
|
-
export default
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
export default class {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.name = '@kopflos-cms/vite';
|
|
12
|
+
this.outDir = options.outDir || 'dist';
|
|
13
|
+
this.rootDir = options.root || '';
|
|
14
|
+
this.buildDir = this.outDir;
|
|
15
|
+
}
|
|
16
|
+
get viteDevServer() {
|
|
17
|
+
return this._viteDevServer;
|
|
18
|
+
}
|
|
19
|
+
onStart({ env }) {
|
|
20
|
+
const viteVars = {
|
|
21
|
+
basePath: resolve(env.kopflos.basePath, env.kopflos.config.mode === 'development' ? this.rootDir : this.buildDir),
|
|
22
|
+
};
|
|
23
|
+
log.info('Variables', viteVars);
|
|
24
|
+
env.kopflos.variables.VITE = Object.freeze(viteVars);
|
|
25
|
+
}
|
|
26
|
+
async beforeMiddleware(host, { env }) {
|
|
27
|
+
if (env.kopflos.config.mode === 'development') {
|
|
28
|
+
log.info('Development UI mode. Creating Vite server...');
|
|
29
|
+
const configPath = this.options.configPath
|
|
30
|
+
? resolve(env.kopflos.basePath, this.options.configPath)
|
|
31
|
+
: this.options.configPath;
|
|
32
|
+
this._viteDevServer = await createViteServer({
|
|
33
|
+
configPath,
|
|
34
|
+
...this.options,
|
|
35
|
+
});
|
|
36
|
+
host.use(this._viteDevServer.middlewares);
|
|
14
37
|
}
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this.env.kopflos.variables.VITE = Object.freeze(viteVars);
|
|
38
|
+
else {
|
|
39
|
+
const buildDir = resolve(env.kopflos.basePath, this.buildDir);
|
|
40
|
+
log.info('Serving UI from build directory');
|
|
41
|
+
log.debug('Build directory:', buildDir);
|
|
42
|
+
host.use(express.static(buildDir));
|
|
21
43
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
log.info('Building UI...');
|
|
36
|
-
await build(await prepareConfig({ outDir, ...options }));
|
|
37
|
-
}
|
|
38
|
-
};
|
|
44
|
+
}
|
|
45
|
+
async build(env) {
|
|
46
|
+
log.info('Building UI...');
|
|
47
|
+
const outDir = resolve(env.kopflos.basePath, this.outDir);
|
|
48
|
+
const configPath = this.options.configPath
|
|
49
|
+
? resolve(env.kopflos.basePath, this.options.configPath)
|
|
50
|
+
: this.options.configPath;
|
|
51
|
+
await build(await prepareConfig({
|
|
52
|
+
outDir,
|
|
53
|
+
configPath,
|
|
54
|
+
...this.options,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
39
57
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kopflos-cms/vite",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Zazuko GmbH",
|
|
@@ -28,21 +28,19 @@
|
|
|
28
28
|
"homepage": "https://github.com/zazuko/kopflos",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@kopflos-cms/logger": "^0.1.0",
|
|
31
|
-
"express": "^5.0
|
|
32
|
-
"glob": "^11.
|
|
31
|
+
"express": "^5.2.0",
|
|
32
|
+
"glob": "^11.1.0",
|
|
33
33
|
"onetime": "^7.0.0",
|
|
34
|
-
"vite": "^6.
|
|
34
|
+
"vite": "^6.4.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
+
"@kopflos-cms/core": "^0.6.1",
|
|
38
|
+
"@zazuko/env-node": "^2.0.0",
|
|
37
39
|
"@types/glob": "^8.1.0",
|
|
38
40
|
"chai": "^5.1.1"
|
|
39
41
|
},
|
|
40
42
|
"mocha": {
|
|
41
|
-
"extension": [
|
|
42
|
-
"ts"
|
|
43
|
-
],
|
|
44
43
|
"spec": "test/**/*.test.ts",
|
|
45
|
-
"
|
|
46
|
-
"require": "../../mocha-setup.js"
|
|
44
|
+
"extends": "../../package.json"
|
|
47
45
|
}
|
|
48
46
|
}
|
package/template.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { SubjectHandler } from '@kopflos-cms/core';
|
|
2
|
-
export declare const transform: () => SubjectHandler;
|
|
1
|
+
import type { Kopflos, SubjectHandler } from '@kopflos-cms/core';
|
|
2
|
+
export declare const transform: (this: Kopflos) => SubjectHandler;
|
package/template.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
import { createViteServer } from './lib/server.js';
|
|
2
1
|
import { log } from './lib/log.js';
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
export const transform = function () {
|
|
3
|
+
const prepareDevTemplate = async (subject, template) => {
|
|
4
|
+
const vite = this.getPlugin('@kopflos-cms/vite');
|
|
5
|
+
if (!vite?.viteDevServer) {
|
|
6
|
+
throw new Error('Vite dev server not initialized. Check vite plugin configuration.');
|
|
7
|
+
}
|
|
8
|
+
const subjectPath = new URL(subject.value).pathname;
|
|
9
|
+
return vite.viteDevServer.transformIndexHtml(subjectPath, template);
|
|
10
|
+
};
|
|
11
|
+
return async ({ subject, env }, response) => {
|
|
12
|
+
if (!isHtmlResponse(response)) {
|
|
13
|
+
throw new Error('Vite handler must be chained after another which returns a HTML response');
|
|
14
|
+
}
|
|
15
|
+
if (env.kopflos.config.mode === 'production') {
|
|
16
|
+
return response;
|
|
17
|
+
}
|
|
18
|
+
log.debug('Compiling page template');
|
|
19
|
+
return {
|
|
20
|
+
...response,
|
|
21
|
+
body: await prepareDevTemplate(subject, response.body),
|
|
22
|
+
};
|
|
20
23
|
};
|
|
21
24
|
};
|
|
22
25
|
function isHtmlResponse(response) {
|