@kopflos-cms/vite 0.1.1 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @kopflos-cms/vite
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - accad6e: Removed the type `KopflosPluginConstructor` and now all plugins are exported as classes directly and their options are passed to the constructor
8
+
9
+ ### Patch Changes
10
+
11
+ - 6511af6: build(deps): bump glob from 11.0.0 to 11.1.0
12
+ - 4214fce: Added an accessor to the `ViteDevServer` the plugin instance
13
+ - cdbb172: Adopt a simpler definition of a plugin where all methods are instance methods. Kopflos instance is passed as parameter
14
+ - 5295694: build(deps): bump express from 5.0.1 to 5.2.0
15
+
16
+ ## 0.1.2
17
+
18
+ ### Patch Changes
19
+
20
+ - 175c538: Add inline `config` to plugin config
21
+ - 3370532: Plugins are now implemented as classes
22
+
3
23
  ## 0.1.1
4
24
 
5
25
  ### Patch Changes
package/index.d.ts CHANGED
@@ -1,14 +1,35 @@
1
- import type { KopflosPlugin } from '@kopflos-cms/core';
1
+ import type { Kopflos, KopflosPlugin } from '@kopflos-cms/core';
2
+ import express from 'express';
3
+ import type { InlineConfig, ViteDevServer } from 'vite';
2
4
  export { defineConfig } from 'vite';
3
5
  export interface Options {
4
6
  configPath?: string;
7
+ config?: InlineConfig;
5
8
  root?: string;
6
9
  outDir?: string;
7
10
  entrypoints?: string[];
8
11
  }
12
+ interface VitePlugin extends KopflosPlugin {
13
+ viteDevServer?: ViteDevServer;
14
+ }
9
15
  declare module '@kopflos-cms/core' {
10
16
  interface PluginConfig {
11
17
  '@kopflos-cms/vite'?: Options;
12
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(): Promise<void>;
13
35
  }
14
- export default function ({ outDir, ...options }: Options): KopflosPlugin;
package/index.js CHANGED
@@ -5,32 +5,38 @@ 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 function ({ outDir = 'dist', ...options }) {
9
- const rootDir = resolve(process.cwd(), options.root || '');
10
- const buildDir = resolve(process.cwd(), outDir);
11
- return {
12
- onStart({ env }) {
13
- const viteVars = {
14
- basePath: env.kopflos.config.mode === 'development' ? rootDir : buildDir,
15
- };
16
- log.info('Variables', viteVars);
17
- env.kopflos.variables.VITE = Object.freeze(viteVars);
18
- },
19
- async beforeMiddleware(host, { env }) {
20
- if (env.kopflos.config.mode === 'development') {
21
- log.info('Development UI mode. Creating Vite server...');
22
- const viteServer = await createViteServer(options);
23
- host.use(viteServer.middlewares);
24
- }
25
- else {
26
- log.info('Serving UI from build directory');
27
- log.debug('Build directory:', buildDir);
28
- host.use(express.static(buildDir));
29
- }
30
- },
31
- async build() {
32
- log.info('Building UI...');
33
- await build(await prepareConfig({ outDir, ...options }));
34
- },
35
- };
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 = resolve(process.cwd(), options.root || '');
14
+ this.buildDir = resolve(process.cwd(), this.outDir);
15
+ }
16
+ get viteDevServer() {
17
+ return this._viteDevServer;
18
+ }
19
+ onStart({ env }) {
20
+ const viteVars = {
21
+ 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
+ this._viteDevServer = await createViteServer(this.options);
30
+ host.use(this._viteDevServer.middlewares);
31
+ }
32
+ else {
33
+ log.info('Serving UI from build directory');
34
+ log.debug('Build directory:', this.buildDir);
35
+ host.use(express.static(this.buildDir));
36
+ }
37
+ }
38
+ async build() {
39
+ log.info('Building UI...');
40
+ await build(await prepareConfig({ outDir: this.outDir, ...this.options }));
41
+ }
36
42
  }
package/lib/config.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import type { Options } from '../index.js';
2
- export declare function prepareConfig({ root, configPath, entrypoints, outDir }: Omit<Options, 'mode'>): Promise<Record<string, any>>;
2
+ export declare function prepareConfig({ root, configPath, entrypoints, outDir, config }: Omit<Options, 'mode'>): Promise<Record<string, any>>;
package/lib/config.js CHANGED
@@ -2,7 +2,7 @@ import { resolve } from 'node:path';
2
2
  import { glob } from 'glob';
3
3
  import { mergeConfig } from 'vite';
4
4
  import defaultConfig from '../vite.config.js';
5
- export async function prepareConfig({ root, configPath, entrypoints, outDir }) {
5
+ export async function prepareConfig({ root, configPath, entrypoints, outDir, config = {} }) {
6
6
  const inputConfig = {
7
7
  root,
8
8
  build: {
@@ -21,5 +21,5 @@ export async function prepareConfig({ root, configPath, entrypoints, outDir }) {
21
21
  const userConfig = await import(configPath);
22
22
  return mergeConfig(mergeConfig(defaultConfig, inputConfig), userConfig.default);
23
23
  }
24
- return mergeConfig(defaultConfig, inputConfig);
24
+ return mergeConfig(config, mergeConfig(defaultConfig, inputConfig));
25
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kopflos-cms/vite",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
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.1",
32
- "glob": "^11.0.0",
31
+ "express": "^5.2.0",
32
+ "glob": "^11.1.0",
33
33
  "onetime": "^7.0.0",
34
- "vite": "^6.0.9"
34
+ "vite": "^6.4.1"
35
35
  },
36
36
  "devDependencies": {
37
+ "@kopflos-cms/core": "^0.6.0",
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
- "loader": "ts-node/esm/transpile-only",
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
- 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),
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) {