import type { Plugin, OutputBundle, OutputChunk } from "rollup"; import path from "path"; import picomatch from "picomatch"; import { parseDocument } from "htmlparser2"; import { selectAll } from "css-select"; import type { Element } from "domhandler"; import type { IsomorphicRollupFs } from "../fs-iface"; // Some plugins (e.g. @rollup/plugin-commonjs) append a query suffix to a // real file path to disambiguate how a module is being imported (e.g. // "./foo.js?commonjs-es-import"). Strip it before treating the id as a // filesystem path, otherwise it leaks into manifest output as a bogus // path that doesn't correspond to a real file. const stripQuery = (moduleId: string): string => moduleId.replace(/\?[^?]*$/, ""); const DEFAULT_EXCLUDE_PATTERNS = [ "**/node_modules/**", "**/dist/**", "**/build/**", "**/.now/**", "**/.git/**", "**/*.min.js", "**/*.bundle.js", ]; export interface UiPageSourceManifestOptions { /** * Project root directory for resolving relative paths. */ rootDir: string; /** * Client directory for resolving relative path for HTML file. */ clientDir: string; /** * Glob patterns for files to exclude from the manifest. * Defaults to common build/dependency directories. * @default DEFAULT_EXCLUDE_PATTERNS */ excludePatterns?: string[]; /** * @deprecated No longer used. */ fs?: IsomorphicRollupFs; } export interface UiPageSourceManifest { /** * The HTML source file path (relative to rootDir). * Example: "src/client/index.html" */ html: string; /** * The main JavaScript/TypeScript entry point (relative to rootDir). * Example: "src/client/main.tsx" */ entry: string; /** * Array of all source file paths used in the bundle (relative to rootDir). * Example: ["src/client/app.tsx", "src/client/components/Button.tsx"] */ files: string[]; /** * Vendor chunks extracted from node_modules for this entry. * Each tuple is [fileName, contentHash]. * Example: [["vendor-react-dom--d217b640.jsdbx", "d217b640"]] */ vendors: [string, string][]; } /** * Rollup plugin that generates source manifest files for each entry point. * * This plugin tracks all source files used in each chunk and generates a JSON manifest * file that maps entry points to their source files. This is useful for: * - Source code tracking and versioning * - Debugging and development workflows * - IDE integrations that need to know the original source files * * @example * ```typescript * import { uiPageSourceManifest } from '@servicenow/isomorphic-rollup'; * * export default { * plugins: [ * uiPageSourceManifest({ * rootDir: "/project", * clientDir: "/project/src/client", * excludePatterns: ['**\/node_modules\/**', '**\/dist\/**'], * }), * ], * }; * ``` */ export default function uiPageSourceManifest( options: UiPageSourceManifestOptions, ): Plugin { const { rootDir, clientDir, excludePatterns = DEFAULT_EXCLUDE_PATTERNS, } = options; const isExcluded = picomatch(excludePatterns); return { name: "source-manifest", async generateBundle(_, bundle: OutputBundle) { // ────────────────────────────────────────────────────────────── // Flow overview (multi-entry HTML apps): // // Given: login.html → app-login.tsx, dashboard.html → app-dash.tsx // // 1. COLLECT — Walk the bundle once, building lookup maps: // // entryToSources "app-login" → {app-login.tsx} // chunk name → direct source files in that chunk // // entryToFacadePath "app-login" → "client/src/app-login.tsx" // chunk name → original entry file (rollup facadeModuleId) // // chunkFileNameToEntry "app-login.js" → "app-login" // output fileName → chunk name (used to match HTML refs) // // htmlFiles ["login.html", "dashboard.html"] // // 2. MATCH — For each HTML, determine which entries belong to it. // htmlScriptEntrypoints has already rewritten