Loheide-eu-mitarbeiterplatt.../sdk/vite.js
Friederich Loheide 326679b854
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m15s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m19s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m3s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m41s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m11s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m9s
Plugin-SDK: Varianten gegen CSS anderer Plugins absichern (0.39.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 16:32:48 +00:00

127 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Vite-Voreinstellung für Inhaus-Plugins (Plugin-SDK).
*
* // vite.config.js im Plugin
* import { defineConfig } from 'vite';
* import { inhausPlugin } from './inhaus-sdk/vite.js';
* export default defineConfig(inhausPlugin());
*
* Baut jede Vue-Datei unter resources/js/{pages,widgets,blocks,profile,profile-panels,editorial,header}/
* als eigenes ES-Modul nach dist/ und schreibt dist/inhaus.json. Vue, Inertia und @inhaus/ui
* bleiben extern – das Portal stellt sie per Import Map bereit.
*/
import { existsSync, globSync } from 'node:fs';
import { resolve } from 'node:path';
import postcss from 'postcss';
import vue from '@vitejs/plugin-vue';
import tailwindcss from '@tailwindcss/vite';
export const KINDS = ['pages', 'widgets', 'blocks', 'profile', 'profile-panels', 'editorial', 'header'];
export const EXTERNAL = ['vue', '@inertiajs/vue3', '@inhaus/ui'];
export function inhausPlugin({ root = process.cwd() } = {}) {
const input = {};
for (const kind of KINDS) {
for (const file of globSync(`resources/js/${kind}/**/*.vue`, { cwd: root })) {
const name = file.replace(`resources/js/${kind}/`, '').replace(/\.vue$/, '');
input[`${kind}/${name}`] = resolve(root, file);
}
}
if (existsSync(resolve(root, 'resources/css/style.css'))) {
input.style = resolve(root, 'resources/css/style.css');
}
if (Object.keys(input).length === 0) {
throw new Error('Keine Oberfläche gefunden: Vue-Dateien gehören nach resources/js/<pages|widgets|blocks|profile|profile-panels|editorial|header>/.');
}
return {
plugins: [vue(), tailwindcss(), manifest(Object.keys(input)), strongerVariants()],
// Plugins laufen unter /plugin-assets/<slug>/<version>/ – Pfade relativ halten
base: './',
build: {
outDir: 'dist',
emptyOutDir: true,
target: 'es2022',
rollupOptions: {
input,
external: EXTERNAL,
preserveEntrySignatures: 'strict',
output: {
format: 'es',
entryFileNames: '[name].js',
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash][extname]',
},
},
},
};
}
/** dist/inhaus.json: welche Komponente in welcher Datei steckt, welches CSS dazugehört */
function manifest(entries) {
return {
name: 'inhaus-manifest',
generateBundle(_, bundle) {
const components = {};
const css = [];
for (const file of Object.values(bundle)) {
if (file.type === 'chunk' && file.isEntry && entries.includes(file.name) && file.name !== 'style') {
components[file.name] = file.fileName;
}
if (file.type === 'asset' && file.fileName.endsWith('.css')) {
css.push(file.fileName);
}
}
// Leere Hülle des CSS-Einstiegs nicht ausliefern
for (const [key, file] of Object.entries(bundle)) {
if (file.type === 'chunk' && file.name === 'style') delete bundle[key];
}
this.emitFile({ type: 'asset', fileName: 'inhaus.json', source: JSON.stringify({ components, css: [...new Set(css)] }, null, 4) });
},
};
}
/**
* Mehrere Plugins bringen jeweils eigenes CSS mit, und dieselbe Hilfsklasse (z. B. ih:grid) kann in
* mehreren Dateien stehen. Lädt ein anderes Plugin sein CSS später, würde dessen „ih:grid“ sonst
* die Varianten dieses Plugins (ih:lg:hidden, ih:hover:…) überstimmen. Deshalb bekommen Regeln
* mit Variante (Media Query, :hover …) eine doppelte Klasse – sie gewinnen so immer gegen
* einfache Klassen, egal in welcher Reihenfolge die Dateien laden.
*/
export function strongerVariants() {
const pseudo = /(?<!\\):/;
const firstClass = /\.((?:\\.|[\w-])+)/;
return {
name: 'inhaus-stronger-variants',
enforce: 'post',
generateBundle(_, bundle) {
for (const file of Object.values(bundle)) {
if (file.type !== 'asset' || !file.fileName.endsWith('.css')) continue;
const root = postcss.parse(String(file.source));
root.walkRules((rule) => {
let conditional = false;
for (let parent = rule.parent; parent && parent.type !== 'root'; parent = parent.parent) {
if (parent.type === 'atrule' && parent.name !== 'layer') conditional = true;
}
rule.selectors = rule.selectors.map((selector) => (conditional || pseudo.test(selector.replace(/^[^.]*/, ''))) && firstClass.test(selector)
? selector.replace(firstClass, (match) => match + match)
: selector);
});
file.source = root.toString();
}
},
};
}