mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-11-03 14:58:18 +00:00
Reduce production bundles while preserving host registration
This commit is contained in:
1 parent
b9dcb0d4c7
commit
b8d4ff2c16
10 files changed
+133
-20
No files matched your search
@@ -1,18 +1,11 @@
|
||||
import { defaultServerConditions, defaultServerMainFields, defineConfig } from "vite";
|
||||
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
||||
import { fileURLToPath, fs, isBuiltin, path } from "@vrtmrz/livesync-commonlib/node";
|
||||
import { fileURLToPath, isBuiltin, path } from "@vrtmrz/livesync-commonlib/node";
|
||||
import { readVersion } from "../readVersion";
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const resolve = (...args: string[]) => path.resolve(...args).replace(/\\/g, "/");
|
||||
const repoRoot = path.resolve(__dirname, "../../..");
|
||||
|
||||
function readVersion(filePath: string): string | undefined {
|
||||
const parsed: unknown = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
||||
if (typeof parsed !== "object" || parsed === null || !("version" in parsed)) {
|
||||
return undefined;
|
||||
}
|
||||
return typeof parsed.version === "string" ? parsed.version : undefined;
|
||||
}
|
||||
|
||||
const packageVersion = readVersion(path.resolve(repoRoot, "package.json"));
|
||||
const manifestVersion = readVersion(path.resolve(repoRoot, "manifest.json"));
|
||||
// https://vite.dev/config/
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { fs } from "@vrtmrz/livesync-commonlib/node";
|
||||
|
||||
/** Read a string-valued version field from a JSON file during Node builds. */
|
||||
export function readVersion(filePath: string): string | undefined {
|
||||
const parsed: unknown = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
||||
if (typeof parsed !== "object" || parsed === null || !("version" in parsed)) {
|
||||
return undefined;
|
||||
}
|
||||
return typeof parsed.version === "string" ? parsed.version : undefined;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { readVersion } from "./readVersion";
|
||||
|
||||
describe("readVersion", () => {
|
||||
let directory: string;
|
||||
|
||||
beforeEach(() => {
|
||||
directory = mkdtempSync(join(tmpdir(), "livesync-read-version-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("returns only string version values", () => {
|
||||
const filePath = join(directory, "version.json");
|
||||
writeFileSync(filePath, JSON.stringify({ version: "1.2.3" }));
|
||||
expect(readVersion(filePath)).toBe("1.2.3");
|
||||
|
||||
for (const contents of ["null", "[]", "{}", '{"version": 42}', '{"version": null}']) {
|
||||
writeFileSync(filePath, contents);
|
||||
expect(readVersion(filePath)).toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves file read and JSON parse errors", () => {
|
||||
expect(() => readVersion(join(directory, "missing.json"))).toThrow();
|
||||
|
||||
const filePath = join(directory, "invalid.json");
|
||||
writeFileSync(filePath, "not JSON");
|
||||
expect(() => readVersion(filePath)).toThrow(SyntaxError);
|
||||
});
|
||||
});
|
||||
@@ -1,17 +1,10 @@
|
||||
import { defineConfig } from "vite";
|
||||
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
||||
import { fileURLToPath, fs, path } from "@vrtmrz/livesync-commonlib/node";
|
||||
import { fileURLToPath, path } from "@vrtmrz/livesync-commonlib/node";
|
||||
import { readVersion } from "../readVersion";
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = path.resolve(__dirname, "../../..");
|
||||
|
||||
function readVersion(filePath: string): string | undefined {
|
||||
const parsed: unknown = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
||||
if (typeof parsed !== "object" || parsed === null || !("version" in parsed)) {
|
||||
return undefined;
|
||||
}
|
||||
return typeof parsed.version === "string" ? parsed.version : undefined;
|
||||
}
|
||||
|
||||
const packageVersion = readVersion(path.resolve(repoRoot, "package.json"));
|
||||
const manifestVersion = readVersion(path.resolve(repoRoot, "manifest.json"));
|
||||
// https://vite.dev/config/
|
||||
|
||||
Reference in new issue
Block a user