vite-plugin-mirrorstate 0.4.0 → 0.6.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/dist/index.js +28 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,8 +5,19 @@ import { promises as fsPromises } from "fs";
|
|
|
5
5
|
import * as path from "path";
|
|
6
6
|
import { glob } from "glob";
|
|
7
7
|
import debug from "debug";
|
|
8
|
+
import * as crypto from "crypto";
|
|
8
9
|
const logger = debug("mirrorstate:vite-plugin");
|
|
9
10
|
const WS_PATH = "/mirrorstate";
|
|
11
|
+
function computeStatesHash(states) {
|
|
12
|
+
// Sort keys for deterministic output
|
|
13
|
+
const sortedKeys = Object.keys(states).sort();
|
|
14
|
+
const sortedStates = {};
|
|
15
|
+
for (const key of sortedKeys) {
|
|
16
|
+
sortedStates[key] = states[key];
|
|
17
|
+
}
|
|
18
|
+
const content = JSON.stringify(sortedStates);
|
|
19
|
+
return crypto.createHash("sha256").update(content).digest("hex").slice(0, 8);
|
|
20
|
+
}
|
|
10
21
|
export function mirrorStatePlugin() {
|
|
11
22
|
let wss;
|
|
12
23
|
let watcher;
|
|
@@ -183,7 +194,23 @@ export function mirrorStatePlugin() {
|
|
|
183
194
|
});
|
|
184
195
|
const results = await Promise.all(filePromises);
|
|
185
196
|
const states = Object.fromEntries(results.filter((x) => x != null));
|
|
186
|
-
|
|
197
|
+
const statesHash = computeStatesHash(states);
|
|
198
|
+
logger(`Computed states hash: ${statesHash}`);
|
|
199
|
+
// Read project name from package.json
|
|
200
|
+
let projectName = "default";
|
|
201
|
+
try {
|
|
202
|
+
const packageJsonPath = path.join(baseDir, "package.json");
|
|
203
|
+
const packageJsonContent = await fsPromises.readFile(packageJsonPath, "utf8");
|
|
204
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
205
|
+
if (packageJson.name) {
|
|
206
|
+
projectName = packageJson.name;
|
|
207
|
+
}
|
|
208
|
+
logger(`Using project name: ${projectName}`);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
logger(`Could not read package.json, using default project name`);
|
|
212
|
+
}
|
|
213
|
+
return `export const INITIAL_STATES = ${JSON.stringify(states)};\nexport const STATES_HASH = "${statesHash}";\nexport const PROJECT_NAME = "${projectName}";`;
|
|
187
214
|
}
|
|
188
215
|
},
|
|
189
216
|
closeBundle() {
|
package/package.json
CHANGED