import { init, parse as parseImports } from "es-module-lexer"; import MagicString from "magic-string"; /** * Ensures every import/re-export whose specifier contains `prefix` starts on * its own line. The ServiceNow instance rewrites these paths to point at * different bundles; if the import shares a line with other statements the * column shift corrupts their sourcemap entries too. * * Uses `es-module-lexer` to locate both static and dynamic imports reliably. * Only exported for testability. */ export async function isolateExternalImports( code: string, prefix: string, ): Promise<{ code: string; map: ReturnType; } | null> { if (!code.includes(prefix)) return null; await init; const [imports] = parseImports(code); const s = new MagicString(code); let changed = false; const inserted = new Set(); const staticImports: { start: number; end: number }[] = []; for (const imp of imports) { if (!imp.n?.startsWith(prefix)) continue; let stmtStart = imp.ss; let stmtEnd = imp.se; // se may point AT the trailing semicolon rather than past it. if (stmtEnd < code.length && code[stmtEnd] === ";") stmtEnd++; if (imp.d === -1) { // Static import — collect for hoisting. staticImports.push({ start: stmtStart, end: stmtEnd }); changed = true; } else { // Dynamic import — isolate on its own line. // ss/se cover only the `import(...)` expression; walk backward to the // containing statement start so the whole assignment lands on its own line. // The scan is quote-aware so a `;` inside a string literal doesn't // produce a wrong split point. let i = stmtStart - 1; while (i >= 0 && code[i] !== "\n" && code[i] !== ";") { const ch = code[i]; if (ch === '"' || ch === "'" || ch === "`") { // Skip backward past the matching opening quote. i--; while (i >= 0 && code[i] !== ch) { if (code[i] === "\\" && ch !== "`") i--; // skip escaped char i--; } } i--; } stmtStart = i + 1; if ( stmtStart > 0 && code[stmtStart - 1] !== "\n" && !inserted.has(stmtStart) ) { s.appendLeft(stmtStart, "\n"); inserted.add(stmtStart); changed = true; } if ( stmtEnd < code.length && code[stmtEnd] !== "\n" && !inserted.has(stmtEnd) ) { s.appendLeft(stmtEnd, "\n"); inserted.add(stmtEnd); changed = true; } } } // Move static imports to the top, preserving their relative order. // The target is the start of the first non-import character so that // imports already at the top don't trigger "cannot move inside itself". if (staticImports.length) { // Find the first position that isn't covered by a static import. // Imports are already sorted by position (from es-module-lexer). let target = 0; for (const { start, end } of staticImports) { if (start !== target) break; target = end; // Include the trailing newline so the next import's start matches. if (target < code.length && code[target] === "\n") target++; } for (const { start, end } of staticImports) { const moved = start >= target; if (moved) { s.move(start, end, target); } // Append a newline after the import unless it already has one in the // original code AND wasn't moved (moved imports need a separator // regardless of what followed them at their original position). if (moved || end >= code.length || code[end] !== "\n") { if (!inserted.has(end)) { s.appendLeft(end, "\n"); inserted.add(end); } } } // The header plugin prepends /*__ESM_BODY_START__*/ before the chunk // body. When imports are hoisted past it, insert a newline after the // comment so it sits on its own line between the imports and the code. const ESM_BODY_COMMENT = "/*__ESM_BODY_START__*/"; if (code.startsWith(ESM_BODY_COMMENT, target)) { const pos = target + ESM_BODY_COMMENT.length; if (pos < code.length && code[pos] !== "\n" && !inserted.has(pos)) { s.appendLeft(pos, "\n"); inserted.add(pos); } } } if (!changed) return null; return { code: s.toString(), map: s.generateMap({ hires: "boundary" }) }; }