From f5f974fd746e4e26246d750dc56eab298faadd05 Mon Sep 17 00:00:00 2001 From: wujunchen Date: Mon, 27 Apr 2026 19:57:28 +0800 Subject: [PATCH] build: remove __test barrel from production bundle Tests now load src/test-exports.ts via esbuild buildSync instead of importing __test from the production main.js. This removes ~60 internal symbols and all test-only module code from the shipped plugin bundle. Change-Id: I2caf5232f6894c7514215e1751f2e268b74d7dc4 --- main.ts | 2 -- tests/generation-job-manager.test.js | 5 ++--- tests/main.test.js | 8 +------- tests/test-setup.js | 24 +++++++++++++++++++++++- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/main.ts b/main.ts index e1fc7ef..1dc8d0a 100644 --- a/main.ts +++ b/main.ts @@ -32,7 +32,6 @@ import { createRafThrottledHandler, visibleTopProbeY } from './src/scroll'; import { cacheEntryMatches, DEFAULT_SETTINGS, normalizeSettings } from './src/settings'; import { ParallelReaderSettingTab } from './src/settings-tab'; import type { StreamProgress } from './src/streaming'; -import * as testExports from './src/test-exports'; import type { CacheEntry, ObsidianEditorWithCm, @@ -690,4 +689,3 @@ class ParallelReaderPlugin extends Plugin { } export default ParallelReaderPlugin; -export const __test = testExports; diff --git a/tests/generation-job-manager.test.js b/tests/generation-job-manager.test.js index d6f62e5..ea98c9b 100644 --- a/tests/generation-job-manager.test.js +++ b/tests/generation-job-manager.test.js @@ -1,8 +1,7 @@ -const assert = require('assert'); -require('./obsidian-mock'); +const { assert, t } = require('./test-setup'); const { GenerationJobAlreadyRunningError, GenerationJobCancelledError, GenerationJobManager, classifyGenerationError } = - require('../main.js').__test; + t; async function testSingleFlightAndCleanup() { const manager = new GenerationJobManager(); diff --git a/tests/main.test.js b/tests/main.test.js index c0f6aa2..a5c390b 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -1,13 +1,7 @@ -const assert = require('assert'); const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); -require('./obsidian-mock'); - -const plugin = require('../main.js'); -const t = plugin.__test; - -assert.ok(t, 'test helpers should be exported'); +const { assert, t } = require('./test-setup'); const mainSource = fs.readFileSync(path.join(__dirname, '..', 'main.ts'), 'utf8'); const viewSource = fs.readFileSync(path.join(__dirname, '..', 'src', 'view.ts'), 'utf8'); assert.ok(!/\basync\s+onOpen\s*\(/.test(viewSource), 'ParallelReaderView.onOpen should not be async without await'); diff --git a/tests/test-setup.js b/tests/test-setup.js index 2450912..497460a 100644 --- a/tests/test-setup.js +++ b/tests/test-setup.js @@ -1,8 +1,30 @@ const assert = require('assert'); +const esbuild = require('esbuild'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); const { EventEmitter } = require('events'); + +// Load obsidian mock first — hooks Module._load so that require('obsidian') +// returns the mock for both us and the esbuild-bundled test-exports module. const { getRequestUrlMock, setRequestUrlMock } = require('./obsidian-mock'); -const t = require('../main.js').__test; +// Bundle test-exports.ts synchronously with obsidian marked as external. +// The bundled code will require('obsidian') at runtime, hitting our mock. +const repoRoot = path.join(__dirname, '..'); +const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'parallel-reader-test-setup-')); +const outfile = path.join(tempDir, 'test-exports.cjs'); + +esbuild.buildSync({ + entryPoints: [path.join(repoRoot, 'src/test-exports.ts')], + bundle: true, + platform: 'node', + format: 'cjs', + outfile, + external: ['obsidian'], +}); + +const t = require(outfile); function openAiCardsResponse(cards) { const json = {