@skafform/vite-plugin 0.1.3 → 0.1.5
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/base.css +1 -0
- package/dist/index.js +145 -2
- package/package.json +5 -1
package/base.css
CHANGED
package/dist/index.js
CHANGED
|
@@ -9,6 +9,10 @@ var VIRTUAL_SERVER_INIT = "virtual:skafform/server-init";
|
|
|
9
9
|
var RESOLVED_SERVER_INIT = "\0virtual:skafform/server-init";
|
|
10
10
|
var VIRTUAL_ADMIN_SECTIONS = "virtual:skafform/admin-sections";
|
|
11
11
|
var RESOLVED_ADMIN_SECTIONS = "\0virtual:skafform/admin-sections";
|
|
12
|
+
var VIRTUAL_WIDGET_REGISTRY = "virtual:skafform/widget-registry";
|
|
13
|
+
var RESOLVED_WIDGET_REGISTRY = "\0virtual:skafform/widget-registry";
|
|
14
|
+
var VIRTUAL_WIDGET_LOADERS = "virtual:skafform/widget-loaders";
|
|
15
|
+
var RESOLVED_WIDGET_LOADERS = "\0virtual:skafform/widget-loaders";
|
|
12
16
|
var norm = (p) => p.replace(/\\/g, "/");
|
|
13
17
|
function loadBrickIndex(root) {
|
|
14
18
|
const bricksDir = resolve(root, "bricks");
|
|
@@ -85,13 +89,81 @@ function skafform() {
|
|
|
85
89
|
${vars}
|
|
86
90
|
}`;
|
|
87
91
|
}
|
|
92
|
+
const TAILWIND_TOKEN_MAP = {
|
|
93
|
+
"primary": "--color-primary",
|
|
94
|
+
"primary-fg": "--color-primary-fg",
|
|
95
|
+
"primary-hover": "--color-primary-hover",
|
|
96
|
+
"background": "--color-background",
|
|
97
|
+
"foreground": "--color-foreground",
|
|
98
|
+
"muted": "--color-muted",
|
|
99
|
+
"muted-fg": "--color-muted-fg",
|
|
100
|
+
"border": "--color-border",
|
|
101
|
+
"border-subtle": "--color-border-subtle",
|
|
102
|
+
"error": "--color-error",
|
|
103
|
+
"font": "--font-sans",
|
|
104
|
+
"font-heading": "--font-heading",
|
|
105
|
+
"font-body": "--font-body",
|
|
106
|
+
"font-size-xs": "--text-xs",
|
|
107
|
+
"font-size-sm": "--text-sm",
|
|
108
|
+
"font-size-base": "--text-base",
|
|
109
|
+
"font-size-lg": "--text-lg",
|
|
110
|
+
"font-size-xl": "--text-xl",
|
|
111
|
+
"font-size-2xl": "--text-2xl",
|
|
112
|
+
"radius": "--radius",
|
|
113
|
+
"radius-sm": "--radius-sm",
|
|
114
|
+
"radius-md": "--radius-md",
|
|
115
|
+
"radius-lg": "--radius-lg",
|
|
116
|
+
"radius-full": "--radius-full",
|
|
117
|
+
"shadow-sm": "--shadow-sm",
|
|
118
|
+
"shadow-md": "--shadow-md",
|
|
119
|
+
"shadow-lg": "--shadow-lg"
|
|
120
|
+
};
|
|
121
|
+
function generateTailwindTheme(tokens) {
|
|
122
|
+
const lines = [];
|
|
123
|
+
for (const key of Object.keys(tokens)) {
|
|
124
|
+
const twVar = TAILWIND_TOKEN_MAP[key];
|
|
125
|
+
if (twVar) lines.push(` ${twVar}: var(--skafform-${key});`);
|
|
126
|
+
}
|
|
127
|
+
if (lines.length === 0) return "";
|
|
128
|
+
return `@theme inline {
|
|
129
|
+
${lines.join("\n")}
|
|
130
|
+
}`;
|
|
131
|
+
}
|
|
132
|
+
function generateDarkTokensCss(darkTokens, lightTokens) {
|
|
133
|
+
const entries = Object.entries(darkTokens);
|
|
134
|
+
if (entries.length === 0) return "";
|
|
135
|
+
const darkVars = entries.map(([k, v]) => ` --skafform-${k}: ${v};`).join("\n");
|
|
136
|
+
const classVars = entries.map(([k, v]) => ` --skafform-${k}: ${v};`).join("\n");
|
|
137
|
+
const lightVars = entries.filter(([k]) => k in lightTokens).map(([k]) => ` --skafform-${k}: ${lightTokens[k]};`).join("\n");
|
|
138
|
+
return [
|
|
139
|
+
`@media (prefers-color-scheme: dark) {
|
|
140
|
+
:root {
|
|
141
|
+
${darkVars}
|
|
142
|
+
}
|
|
143
|
+
}`,
|
|
144
|
+
`.dark {
|
|
145
|
+
${classVars}
|
|
146
|
+
}`,
|
|
147
|
+
lightVars ? `.light {
|
|
148
|
+
${lightVars}
|
|
149
|
+
}` : ""
|
|
150
|
+
].filter(Boolean).join("\n");
|
|
151
|
+
}
|
|
88
152
|
function getCssString() {
|
|
89
153
|
const themeJson = getThemeJson(config.theme);
|
|
90
154
|
const themeCssPath = resolve(root, `themes/${config.theme}/styles/theme.css`);
|
|
91
155
|
const base = readCss(baseCssPath);
|
|
92
156
|
const theme = existsSync(themeCssPath) ? readCss(themeCssPath) : "";
|
|
93
157
|
const tokens = generateTokensCss(themeJson.tokens ?? {});
|
|
94
|
-
|
|
158
|
+
const dark = themeJson.tokens_dark ? generateDarkTokensCss(themeJson.tokens_dark, themeJson.tokens ?? {}) : "";
|
|
159
|
+
const css = base + "\n" + theme + (tokens ? "\n" + tokens : "") + (dark ? "\n" + dark : "");
|
|
160
|
+
if (config.tailwind) {
|
|
161
|
+
const tailwindTheme = generateTailwindTheme(themeJson.tokens ?? {});
|
|
162
|
+
return `@import "tailwindcss";
|
|
163
|
+
|
|
164
|
+
` + css + (tailwindTheme ? "\n" + tailwindTheme : "");
|
|
165
|
+
}
|
|
166
|
+
return css;
|
|
95
167
|
}
|
|
96
168
|
return {
|
|
97
169
|
name: "skafform",
|
|
@@ -110,6 +182,8 @@ ${vars}
|
|
|
110
182
|
if (id === VIRTUAL_CONFIG) return RESOLVED_CONFIG;
|
|
111
183
|
if (id === VIRTUAL_SERVER_INIT) return RESOLVED_SERVER_INIT;
|
|
112
184
|
if (id === VIRTUAL_ADMIN_SECTIONS) return RESOLVED_ADMIN_SECTIONS;
|
|
185
|
+
if (id === VIRTUAL_WIDGET_REGISTRY) return RESOLVED_WIDGET_REGISTRY;
|
|
186
|
+
if (id === VIRTUAL_WIDGET_LOADERS) return RESOLVED_WIDGET_LOADERS;
|
|
113
187
|
if (id.startsWith(".") && importer) {
|
|
114
188
|
const themeDir = norm(resolve(root, `themes/${config.theme}`));
|
|
115
189
|
const componentsDir = themeDir + "/components";
|
|
@@ -133,7 +207,8 @@ ${vars}
|
|
|
133
207
|
const themeJson = getThemeJson(config.theme);
|
|
134
208
|
return `export default ${JSON.stringify({
|
|
135
209
|
...config,
|
|
136
|
-
customize: { ...themeJson.customize ?? {}, ...config.customize ?? {} }
|
|
210
|
+
customize: { ...themeJson.customize ?? {}, ...config.customize ?? {} },
|
|
211
|
+
fonts: themeJson.fonts ?? []
|
|
137
212
|
})}`;
|
|
138
213
|
}
|
|
139
214
|
if (id === RESOLVED_ADMIN_SECTIONS) {
|
|
@@ -147,6 +222,74 @@ ${vars}
|
|
|
147
222
|
}
|
|
148
223
|
return `export default ${JSON.stringify(sections)}`;
|
|
149
224
|
}
|
|
225
|
+
if (id === RESOLVED_WIDGET_REGISTRY) {
|
|
226
|
+
const bricksConfig = getBricksConfig();
|
|
227
|
+
if (!bricksConfig) return "export const widgetRegistry = {}";
|
|
228
|
+
const zones = {};
|
|
229
|
+
for (const [brickName, brick] of Object.entries(bricksConfig.bricks ?? {})) {
|
|
230
|
+
for (const widget of brick.widgets ?? []) {
|
|
231
|
+
if (!zones[widget.zone]) zones[widget.zone] = [];
|
|
232
|
+
zones[widget.zone].push({ brick: brickName, component: widget.component });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
for (const widgets of Object.values(zones)) {
|
|
236
|
+
widgets.sort((a, b) => a.brick.localeCompare(b.brick));
|
|
237
|
+
}
|
|
238
|
+
const imports = [];
|
|
239
|
+
const entries = [];
|
|
240
|
+
let i = 0;
|
|
241
|
+
for (const [zone, widgets] of Object.entries(zones)) {
|
|
242
|
+
const items = [];
|
|
243
|
+
for (const w of widgets) {
|
|
244
|
+
const absPath = norm(resolve(root, w.component));
|
|
245
|
+
const basename = absPath.split("/").pop().replace(/\.[^.]+$/, "");
|
|
246
|
+
const varName = `wr_${i++}`;
|
|
247
|
+
imports.push(`import * as ${varName} from ${JSON.stringify(absPath)}`);
|
|
248
|
+
items.push(` { brick: ${JSON.stringify(w.brick)}, basename: ${JSON.stringify(basename)}, Component: ${varName}.default }`);
|
|
249
|
+
}
|
|
250
|
+
entries.push(` ${JSON.stringify(zone)}: [
|
|
251
|
+
${items.join(",\n")}
|
|
252
|
+
]`);
|
|
253
|
+
}
|
|
254
|
+
return [
|
|
255
|
+
...imports,
|
|
256
|
+
`export const widgetRegistry = {
|
|
257
|
+
${entries.join(",\n")}
|
|
258
|
+
}`
|
|
259
|
+
].join("\n");
|
|
260
|
+
}
|
|
261
|
+
if (id === RESOLVED_WIDGET_LOADERS) {
|
|
262
|
+
const bricksConfig = getBricksConfig();
|
|
263
|
+
if (!bricksConfig) return "export async function runWidgetLoaders() { return {} }";
|
|
264
|
+
const widgets = [];
|
|
265
|
+
let i = 0;
|
|
266
|
+
for (const [brickName, brick] of Object.entries(bricksConfig.bricks ?? {})) {
|
|
267
|
+
for (const widget of brick.widgets ?? []) {
|
|
268
|
+
const absPath = norm(resolve(root, widget.component));
|
|
269
|
+
const basename = absPath.split("/").pop().replace(/\.[^.]+$/, "");
|
|
270
|
+
const key = `${widget.zone}:${brickName}:${basename}`;
|
|
271
|
+
widgets.push({ key, absPath, varName: `wl_${i++}` });
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (widgets.length === 0) {
|
|
275
|
+
return "export async function runWidgetLoaders() { return {} }";
|
|
276
|
+
}
|
|
277
|
+
return [
|
|
278
|
+
...widgets.map((w) => `import * as ${w.varName} from ${JSON.stringify(w.absPath)}`),
|
|
279
|
+
`const _loaders = [`,
|
|
280
|
+
...widgets.map((w) => ` { key: ${JSON.stringify(w.key)}, fn: ${w.varName}.loader ?? null },`),
|
|
281
|
+
`]`,
|
|
282
|
+
`export async function runWidgetLoaders(request) {`,
|
|
283
|
+
` const results = {}`,
|
|
284
|
+
` await Promise.all(`,
|
|
285
|
+
` _loaders.filter(w => w.fn !== null).map(async ({ key, fn }) => {`,
|
|
286
|
+
` try { results[key] = await fn({ request }) } catch { results[key] = null }`,
|
|
287
|
+
` })`,
|
|
288
|
+
` )`,
|
|
289
|
+
` return results`,
|
|
290
|
+
`}`
|
|
291
|
+
].join("\n");
|
|
292
|
+
}
|
|
150
293
|
if (id === RESOLVED_SERVER_INIT) {
|
|
151
294
|
const bricksConfig = getBricksConfig();
|
|
152
295
|
if (!bricksConfig) return "";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skafform/vite-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
"base.css",
|
|
14
14
|
"types.d.ts"
|
|
15
15
|
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"registry": "https://registry.npmjs.org",
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
16
20
|
"scripts": {
|
|
17
21
|
"build": "tsup",
|
|
18
22
|
"dev": "tsup --watch"
|