@skafform/vite-plugin 0.1.1 → 0.1.3

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.
Files changed (2) hide show
  1. package/dist/index.js +34 -39
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -7,8 +7,6 @@ var VIRTUAL_CONFIG = "virtual:skafform/config";
7
7
  var RESOLVED_CONFIG = "\0virtual:skafform/config";
8
8
  var VIRTUAL_SERVER_INIT = "virtual:skafform/server-init";
9
9
  var RESOLVED_SERVER_INIT = "\0virtual:skafform/server-init";
10
- var VIRTUAL_NAV = "virtual:skafform/nav";
11
- var RESOLVED_NAV = "\0virtual:skafform/nav";
12
10
  var VIRTUAL_ADMIN_SECTIONS = "virtual:skafform/admin-sections";
13
11
  var RESOLVED_ADMIN_SECTIONS = "\0virtual:skafform/admin-sections";
14
12
  var norm = (p) => p.replace(/\\/g, "/");
@@ -59,10 +57,22 @@ function skafform() {
59
57
  let root = process.cwd();
60
58
  let brickIndex = /* @__PURE__ */ new Set();
61
59
  let config = {};
60
+ let themeJsonCache = null;
61
+ let bricksConfigCache = null;
62
62
  function getThemeJson(theme) {
63
+ if (themeJsonCache) return themeJsonCache;
63
64
  const childPath = resolve(root, `themes/${theme}/child/theme.json`);
64
65
  if (!existsSync(childPath)) return {};
65
- return JSON.parse(readFileSync(childPath, "utf-8"));
66
+ const parsed = JSON.parse(readFileSync(childPath, "utf-8"));
67
+ themeJsonCache = parsed;
68
+ return parsed;
69
+ }
70
+ function getBricksConfig() {
71
+ if (bricksConfigCache) return bricksConfigCache;
72
+ const path = resolve(root, "skafform-bricks.json");
73
+ if (!existsSync(path)) return null;
74
+ bricksConfigCache = JSON.parse(readFileSync(path, "utf-8"));
75
+ return bricksConfigCache;
66
76
  }
67
77
  function readCss(path) {
68
78
  return readFileSync(path, "utf-8").replace(/^/, "");
@@ -88,14 +98,17 @@ ${vars}
88
98
  enforce: "pre",
89
99
  configResolved(resolved) {
90
100
  root = resolved.root;
91
- config = JSON.parse(readFileSync(resolve(root, "skafform.config.json"), "utf-8"));
101
+ try {
102
+ config = JSON.parse(readFileSync(resolve(root, "skafform.config.json"), "utf-8"));
103
+ } catch (err) {
104
+ throw new Error(`[skafform] Cannot read skafform.config.json: ${err instanceof Error ? err.message : err}`);
105
+ }
92
106
  brickIndex = loadBrickIndex(root);
93
107
  writeFileSync(resolve(root, "app/skafform-theme.css"), getCssString(), "utf-8");
94
108
  },
95
109
  resolveId(id, importer) {
96
110
  if (id === VIRTUAL_CONFIG) return RESOLVED_CONFIG;
97
111
  if (id === VIRTUAL_SERVER_INIT) return RESOLVED_SERVER_INIT;
98
- if (id === VIRTUAL_NAV) return RESOLVED_NAV;
99
112
  if (id === VIRTUAL_ADMIN_SECTIONS) return RESOLVED_ADMIN_SECTIONS;
100
113
  if (id.startsWith(".") && importer) {
101
114
  const themeDir = norm(resolve(root, `themes/${config.theme}`));
@@ -123,35 +136,9 @@ ${vars}
123
136
  customize: { ...themeJson.customize ?? {}, ...config.customize ?? {} }
124
137
  })}`;
125
138
  }
126
- if (id === RESOLVED_NAV) {
127
- const bricksConfigPath = resolve(root, "skafform-bricks.json");
128
- const themeJson = getThemeJson(config.theme);
129
- const navLocations = themeJson.nav_locations ?? config.nav_locations ?? {};
130
- const customNav = themeJson.nav ?? config.nav ?? {};
131
- const bricksConfig = existsSync(bricksConfigPath) ? JSON.parse(readFileSync(bricksConfigPath, "utf-8")) : { bricks: {} };
132
- const registry = {};
133
- for (const loc of Object.keys(navLocations)) registry[loc] = [];
134
- for (const [brickName, brick] of Object.entries(bricksConfig.bricks ?? {})) {
135
- for (const item of brick.nav ?? []) {
136
- if (registry[item.location] !== void 0) {
137
- registry[item.location].push({ ...item, order: item.order ?? 0, brick: brickName });
138
- }
139
- }
140
- }
141
- for (const [loc, items] of Object.entries(customNav)) {
142
- if (registry[loc] !== void 0) {
143
- registry[loc].push(...items.map((i) => ({ ...i, order: i.order ?? 0, brick: "config" })));
144
- }
145
- }
146
- for (const loc of Object.keys(registry)) {
147
- registry[loc].sort((a, b) => a.order - b.order);
148
- }
149
- return `export default ${JSON.stringify(registry)}`;
150
- }
151
139
  if (id === RESOLVED_ADMIN_SECTIONS) {
152
- const bricksConfigPath = resolve(root, "skafform-bricks.json");
153
- if (!existsSync(bricksConfigPath)) return "export default []";
154
- const bricksConfig = JSON.parse(readFileSync(bricksConfigPath, "utf-8"));
140
+ const bricksConfig = getBricksConfig();
141
+ if (!bricksConfig) return "export default []";
155
142
  const sections = [];
156
143
  for (const brick of Object.values(bricksConfig.bricks ?? {})) {
157
144
  for (const section of brick.adminSections ?? []) {
@@ -161,23 +148,31 @@ ${vars}
161
148
  return `export default ${JSON.stringify(sections)}`;
162
149
  }
163
150
  if (id === RESOLVED_SERVER_INIT) {
164
- const bricksConfigPath = resolve(root, "skafform-bricks.json");
165
- if (!existsSync(bricksConfigPath)) return "";
166
- const bricksConfig = JSON.parse(readFileSync(bricksConfigPath, "utf-8"));
151
+ const bricksConfig = getBricksConfig();
152
+ if (!bricksConfig) return "";
167
153
  const adapters = bricksConfig.adapters ?? {};
168
154
  if (!adapters.auth) return "";
169
155
  const adapterPath = resolve(root, adapters.auth).replace(/\\/g, "/");
170
156
  return [
171
157
  `import { setAdapter } from "@skafform/core/runtime"`,
172
- `import { authAdapter } from "${adapterPath}"`,
158
+ `import { authAdapter } from ${JSON.stringify(adapterPath)}`,
173
159
  `setAdapter(authAdapter)`
174
160
  ].join("\n");
175
161
  }
176
162
  },
177
163
  handleHotUpdate({ file, server }) {
178
- if (file.includes("skafform.config") || file.includes("themes") || file.includes("bricks")) {
179
- config = JSON.parse(readFileSync(resolve(root, "skafform.config.json"), "utf-8"));
164
+ const f = norm(file);
165
+ const r = norm(root);
166
+ if (f === `${r}/skafform.config.json` || f.startsWith(`${r}/themes/`) || f.startsWith(`${r}/bricks/`)) {
167
+ try {
168
+ config = JSON.parse(readFileSync(resolve(root, "skafform.config.json"), "utf-8"));
169
+ } catch {
170
+ return;
171
+ }
180
172
  brickIndex = loadBrickIndex(root);
173
+ themeJsonCache = null;
174
+ bricksConfigCache = null;
175
+ writeFileSync(resolve(root, "app/skafform-theme.css"), getCssString(), "utf-8");
181
176
  server.moduleGraph.invalidateAll();
182
177
  server.ws.send({ type: "full-reload" });
183
178
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skafform/vite-plugin",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {