From d6a989106607ddd4dc9d3930e9b7de337583a797 Mon Sep 17 00:00:00 2001 From: Paul Bakaus Date: Mon, 3 Aug 2026 14:51:46 -0700 Subject: [PATCH] Share browser detector bundling (#498) Centralize the browser-safe module set and source transformation so the browser and extension builders cannot drift. AI assistance: This refactor was prepared by Codex under pbakaus's scheduled architecture-simplification authorization. --- scripts/build-browser-detector.js | 24 ++------------------- scripts/build-extension.js | 24 ++------------------- scripts/lib/browser-detector-bundle.js | 30 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 44 deletions(-) create mode 100644 scripts/lib/browser-detector-bundle.js diff --git a/scripts/build-browser-detector.js b/scripts/build-browser-detector.js index b21e23b9f..6f5d3070c 100644 --- a/scripts/build-browser-detector.js +++ b/scripts/build-browser-detector.js @@ -10,35 +10,15 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; +import { bundleBrowserDetectorModules } from './lib/browser-detector-bundle.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.resolve(__dirname, '..'); -const MODULES = [ - 'cli/engine/shared/constants.mjs', - 'cli/engine/registry/antipatterns.mjs', - 'cli/engine/shared/color.mjs', - 'cli/engine/shared/fonts.mjs', - 'cli/engine/rules/checks.mjs', - 'cli/engine/browser/injected/index.mjs', -]; const OUTPUT = path.join(ROOT, 'cli/engine/detect-antipatterns-browser.js'); const SITE_OUTPUT = path.join(ROOT, 'site/public/js/detect-antipatterns-browser.js'); -function browserSafeModule(relPath) { - let code = fs.readFileSync(path.join(ROOT, relPath), 'utf-8'); - if (relPath === 'cli/engine/registry/antipatterns.mjs') { - const match = code.match(/const ANTIPATTERNS = \[[\s\S]*?\n\];/); - if (!match) throw new Error('Could not extract browser antipattern registry'); - code = match[0]; - } - code = code.replace(/^import[\s\S]*?;\n/gm, ''); - code = code.replace(/^export\s+\{[^}]*\};\n?/gm, ''); - code = code.replace(/^export\s+\{[\s\S]*?^};\n?/gm, ''); - return `// --- ${relPath} ---\n${code.trim()}\n`; -} - -const code = MODULES.map(browserSafeModule).join('\n'); +const code = bundleBrowserDetectorModules(ROOT); const output = `/** * Anti-Pattern Browser Detector for Impeccable diff --git a/scripts/build-extension.js b/scripts/build-extension.js index ad7999c9b..3ba34febf 100644 --- a/scripts/build-extension.js +++ b/scripts/build-extension.js @@ -19,36 +19,16 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { ANTIPATTERNS } from '../cli/engine/registry/antipatterns.mjs'; +import { bundleBrowserDetectorModules } from './lib/browser-detector-bundle.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.resolve(__dirname, '..'); const EXT_DIR = path.join(ROOT, 'extension'); -const BROWSER_MODULES = [ - 'cli/engine/shared/constants.mjs', - 'cli/engine/registry/antipatterns.mjs', - 'cli/engine/shared/color.mjs', - 'cli/engine/shared/fonts.mjs', - 'cli/engine/rules/checks.mjs', - 'cli/engine/browser/injected/index.mjs', -]; const DETECTOR_OUTPUT = path.join(EXT_DIR, 'detector/detect.js'); const AP_OUTPUT = path.join(EXT_DIR, 'detector/antipatterns.json'); -function browserSafeModule(relPath) { - let code = fs.readFileSync(path.join(ROOT, relPath), 'utf-8'); - if (relPath === 'cli/engine/registry/antipatterns.mjs') { - const match = code.match(/const ANTIPATTERNS = \[[\s\S]*?\n\];/); - if (!match) throw new Error('Could not extract browser antipattern registry'); - code = match[0]; - } - code = code.replace(/^import[\s\S]*?;\n/gm, ''); - code = code.replace(/^export\s+\{[^}]*\};\n?/gm, ''); - code = code.replace(/^export\s+\{[\s\S]*?^};\n?/gm, ''); - return `// --- ${relPath} ---\n${code.trim()}\n`; -} - -const code = BROWSER_MODULES.map(browserSafeModule).join('\n'); +const code = bundleBrowserDetectorModules(ROOT); // --- 1. Build detector --- diff --git a/scripts/lib/browser-detector-bundle.js b/scripts/lib/browser-detector-bundle.js new file mode 100644 index 000000000..cd9f7f5f3 --- /dev/null +++ b/scripts/lib/browser-detector-bundle.js @@ -0,0 +1,30 @@ +import fs from 'node:fs'; +import path from 'node:path'; + +const BROWSER_DETECTOR_MODULES = [ + 'cli/engine/shared/constants.mjs', + 'cli/engine/registry/antipatterns.mjs', + 'cli/engine/shared/color.mjs', + 'cli/engine/shared/fonts.mjs', + 'cli/engine/rules/checks.mjs', + 'cli/engine/browser/injected/index.mjs', +]; + +function browserSafeModule(root, relPath) { + let code = fs.readFileSync(path.join(root, relPath), 'utf-8'); + if (relPath === 'cli/engine/registry/antipatterns.mjs') { + const match = code.match(/const ANTIPATTERNS = \[[\s\S]*?\n\];/); + if (!match) throw new Error('Could not extract browser antipattern registry'); + code = match[0]; + } + code = code.replace(/^import[\s\S]*?;\n/gm, ''); + code = code.replace(/^export\s+\{[^}]*\};\n?/gm, ''); + code = code.replace(/^export\s+\{[\s\S]*?^};\n?/gm, ''); + return `// --- ${relPath} ---\n${code.trim()}\n`; +} + +export function bundleBrowserDetectorModules(root) { + return BROWSER_DETECTOR_MODULES + .map((relPath) => browserSafeModule(root, relPath)) + .join('\n'); +}