@skafform/vite-plugin 0.1.2 → 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 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -57,10 +57,22 @@ function skafform() {
57
57
  let root = process.cwd();
58
58
  let brickIndex = /* @__PURE__ */ new Set();
59
59
  let config = {};
60
+ let themeJsonCache = null;
61
+ let bricksConfigCache = null;
60
62
  function getThemeJson(theme) {
63
+ if (themeJsonCache) return themeJsonCache;
61
64
  const childPath = resolve(root, `themes/${theme}/child/theme.json`);
62
65
  if (!existsSync(childPath)) return {};
63
- 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;
64
76
  }
65
77
  function readCss(path) {
66
78
  return readFileSync(path, "utf-8").replace(/^/, "");
@@ -86,7 +98,11 @@ ${vars}
86
98
  enforce: "pre",
87
99
  configResolved(resolved) {
88
100
  root = resolved.root;
89
- 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
+ }
90
106
  brickIndex = loadBrickIndex(root);
91
107
  writeFileSync(resolve(root, "app/skafform-theme.css"), getCssString(), "utf-8");
92
108
  },
@@ -121,9 +137,8 @@ ${vars}
121
137
  })}`;
122
138
  }
123
139
  if (id === RESOLVED_ADMIN_SECTIONS) {
124
- const bricksConfigPath = resolve(root, "skafform-bricks.json");
125
- if (!existsSync(bricksConfigPath)) return "export default []";
126
- const bricksConfig = JSON.parse(readFileSync(bricksConfigPath, "utf-8"));
140
+ const bricksConfig = getBricksConfig();
141
+ if (!bricksConfig) return "export default []";
127
142
  const sections = [];
128
143
  for (const brick of Object.values(bricksConfig.bricks ?? {})) {
129
144
  for (const section of brick.adminSections ?? []) {
@@ -133,23 +148,31 @@ ${vars}
133
148
  return `export default ${JSON.stringify(sections)}`;
134
149
  }
135
150
  if (id === RESOLVED_SERVER_INIT) {
136
- const bricksConfigPath = resolve(root, "skafform-bricks.json");
137
- if (!existsSync(bricksConfigPath)) return "";
138
- const bricksConfig = JSON.parse(readFileSync(bricksConfigPath, "utf-8"));
151
+ const bricksConfig = getBricksConfig();
152
+ if (!bricksConfig) return "";
139
153
  const adapters = bricksConfig.adapters ?? {};
140
154
  if (!adapters.auth) return "";
141
155
  const adapterPath = resolve(root, adapters.auth).replace(/\\/g, "/");
142
156
  return [
143
157
  `import { setAdapter } from "@skafform/core/runtime"`,
144
- `import { authAdapter } from "${adapterPath}"`,
158
+ `import { authAdapter } from ${JSON.stringify(adapterPath)}`,
145
159
  `setAdapter(authAdapter)`
146
160
  ].join("\n");
147
161
  }
148
162
  },
149
163
  handleHotUpdate({ file, server }) {
150
- if (file.includes("skafform.config") || file.includes("themes") || file.includes("bricks")) {
151
- 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
+ }
152
172
  brickIndex = loadBrickIndex(root);
173
+ themeJsonCache = null;
174
+ bricksConfigCache = null;
175
+ writeFileSync(resolve(root, "app/skafform-theme.css"), getCssString(), "utf-8");
153
176
  server.moduleGraph.invalidateAll();
154
177
  server.ws.send({ type: "full-reload" });
155
178
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skafform/vite-plugin",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {