From b75a9695ebd9666ba235e5877f7fd58f667423a3 Mon Sep 17 00:00:00 2001 From: Taki Date: Tue, 17 Nov 2026 16:52:10 +0700 Subject: [PATCH 1/6] refactor(core): move shared modules into src/core directory --- package.json | 2 +- src/{ => core}/archive.ts | 0 src/{ => core}/epub.ts | 0 src/{ => core}/reader-shell.ts | 0 src/{ => core}/state-manager.ts | 0 src/{ => core}/xhtml.ts | 0 src/extension.ts | 10 +++++----- 7 files changed, 6 insertions(+), 6 deletions(-) rename src/{ => core}/archive.ts (100%) rename src/{ => core}/epub.ts (100%) rename src/{ => core}/reader-shell.ts (100%) rename src/{ => core}/state-manager.ts (100%) rename src/{ => core}/xhtml.ts (100%) diff --git a/package.json b/package.json index 83a962c..08e3bd3 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ ] }, "scripts": { - "build": "esbuild src/extension.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/extension.js && esbuild src/xhtml.ts --bundle --platform=node --format=cjs --outfile=dist/xhtml.js && esbuild src/epub.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/epub.js && esbuild src/state-manager.ts --bundle --platform=node --format=cjs --outfile=dist/state-manager.js && esbuild src/archive.ts --bundle --platform=node --format=cjs --outfile=dist/archive.js", + "build": "esbuild src/extension.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/extension.js && esbuild src/core/xhtml.ts --bundle --platform=node --format=cjs --outfile=dist/xhtml.js && esbuild src/core/epub.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/epub.js && esbuild src/core/state-manager.ts --bundle --platform=node --format=cjs --outfile=dist/state-manager.js && esbuild src/core/archive.ts --bundle --platform=node --format=cjs --outfile=dist/archive.js", "check": "tsc --noEmit", "test": "npm run build && node --test test/*.test.mjs", "watch": "npm run build -- --watch" diff --git a/src/archive.ts b/src/core/archive.ts similarity index 100% rename from src/archive.ts rename to src/core/archive.ts diff --git a/src/epub.ts b/src/core/epub.ts similarity index 100% rename from src/epub.ts rename to src/core/epub.ts diff --git a/src/reader-shell.ts b/src/core/reader-shell.ts similarity index 100% rename from src/reader-shell.ts rename to src/core/reader-shell.ts diff --git a/src/state-manager.ts b/src/core/state-manager.ts similarity index 100% rename from src/state-manager.ts rename to src/core/state-manager.ts diff --git a/src/xhtml.ts b/src/core/xhtml.ts similarity index 100% rename from src/xhtml.ts rename to src/core/xhtml.ts diff --git a/src/extension.ts b/src/extension.ts index d355d34..f7fde6c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,11 +1,11 @@ import * as vscode from 'vscode'; import * as os from 'node:os'; import * as path from 'node:path'; -import { sanitizeXhtml, extractElementById } from './xhtml'; -import { readerShell, ReaderNavInfo } from './reader-shell'; -import { locateEpubContainer, parseContainerXml, parseOpfXml, loadEpubToc, EpubPackage } from './epub'; -import { StateManager, ReaderPreferences, NavigationHistory, ReadingLocation } from './state-manager'; -import { EpubCacheManager, ArchiveSecurityError, ArchiveCorruptError } from './archive'; +import { sanitizeXhtml, extractElementById } from './core/xhtml'; +import { readerShell, ReaderNavInfo } from './core/reader-shell'; +import { locateEpubContainer, parseContainerXml, parseOpfXml, loadEpubToc, EpubPackage } from './core/epub'; +import { StateManager, ReaderPreferences, NavigationHistory, ReadingLocation } from './core/state-manager'; +import { EpubCacheManager, ArchiveSecurityError, ArchiveCorruptError } from './core/archive'; const supportedExtensions = new Set(['.xhtml', '.html', '.htm', '.epub']); const resourceAttributes = /(<(?:img|source|video|audio|link)\b[^>]*?\b(?:src|href)\s*=\s*)(["'])([^"']+)\2/gi; From 0e439e48ec5d981ef9a24d610d2c61e76f87ea6a Mon Sep 17 00:00:00 2001 From: Taki Date: Tue, 17 Nov 2026 16:55:00 +0700 Subject: [PATCH 2/6] refactor(core): remove all vscode dependencies from src/core modules --- src/core/epub.ts | 98 ++++++++++++++++++++++++---------------- src/core/reader-shell.ts | 30 ++++++++++-- 2 files changed, 84 insertions(+), 44 deletions(-) diff --git a/src/core/epub.ts b/src/core/epub.ts index d1d0c40..782676c 100644 --- a/src/core/epub.ts +++ b/src/core/epub.ts @@ -1,10 +1,20 @@ -import type * as vscodeTypes from 'vscode'; +export interface ResourceUri { + path: string; + fsPath?: string; + scheme?: string; + toString(): string; + with?(change: { path?: string }): ResourceUri; +} -let vscodeModule: typeof import('vscode') | undefined; -try { - vscodeModule = require('vscode'); -} catch { - // Allow running in pure Node.js environments (e.g. unit tests) +export function createResourceUri(filePath: string): ResourceUri { + const normalized = filePath.replace(/\\/g, '/'); + return { + path: normalized, + fsPath: filePath, + scheme: 'file', + toString: () => normalized, + with: (change: { path?: string }) => createResourceUri(change.path ?? normalized) + }; } export interface EpubManifestItem { @@ -12,27 +22,27 @@ export interface EpubManifestItem { href: string; mediaType: string; properties?: string; - uri: vscodeTypes.Uri; + uri: ResourceUri; } export interface EpubSpineItem { idref: string; linear?: boolean; href: string; - uri: vscodeTypes.Uri; + uri: ResourceUri; } export interface EpubTocItem { id?: string; label: string; href: string; - uri: vscodeTypes.Uri; + uri: ResourceUri; } export interface EpubPackage { - containerRoot: vscodeTypes.Uri; - opfUri: vscodeTypes.Uri; - opfDir: vscodeTypes.Uri; + containerRoot: ResourceUri; + opfUri: ResourceUri; + opfDir: ResourceUri; title: string; creator?: string; language?: string; @@ -44,28 +54,37 @@ export interface EpubPackage { /** * Ascends from the given source file directory to locate a META-INF/container.xml file. */ -export async function locateEpubContainer(source: vscodeTypes.Uri): Promise { - if (!vscodeModule) { - return undefined; - } +export async function locateEpubContainer( + source: ResourceUri, + fileExists?: (candidate: ResourceUri) => Promise +): Promise { + const checkExists = fileExists ?? (async (candidate: ResourceUri) => { + try { + const fs = await import('node:fs/promises'); + const targetPath = candidate.fsPath || candidate.path; + await fs.stat(targetPath); + return true; + } catch { + return false; + } + }); + let current = parentUri(source); - const root = vscodeModule.workspace.workspaceFolders?.[0]?.uri; for (let i = 0; i < 6; i++) { const candidate = joinPath(current, 'META-INF', 'container.xml'); try { - await vscodeModule.workspace.fs.stat(candidate); - return current; - } catch { - const parent = parentUri(current); - if (parent.toString() === current.toString()) { - break; - } - if (root && current.toString() === root.toString()) { - break; + if (await checkExists(candidate)) { + return current; } - current = parent; + } catch { + // ignore + } + const parent = parentUri(current); + if (parent.toString() === current.toString()) { + break; } + current = parent; } return undefined; } @@ -81,7 +100,7 @@ export function parseContainerXml(xml: string): string | undefined { /** * Safely parses the OPF Package Document into metadata, manifest, and spine. */ -export function parseOpfXml(xml: string, opfUri: vscodeTypes.Uri, containerRoot: vscodeTypes.Uri): EpubPackage { +export function parseOpfXml(xml: string, opfUri: ResourceUri, containerRoot: ResourceUri): EpubPackage { const opfDir = parentUri(opfUri); // Metadata @@ -162,7 +181,7 @@ export function parseOpfXml(xml: string, opfUri: vscodeTypes.Uri, containerRoot: /** * Parses EPUB 3 Navigation Document XHTML into TOC items. */ -export function parseNavHtml(html: string, baseDir: vscodeTypes.Uri): EpubTocItem[] { +export function parseNavHtml(html: string, baseDir: ResourceUri): EpubTocItem[] { const items: EpubTocItem[] = []; // Find