import { ScriptElement, ExternalScriptElement } from '../../types'; /** * Rewrites relative imports in module code to absolute URLs. * * NOTE: When using a dev server like Vite, TypeScript imports work because * the server transpiles .ts files on-the-fly. Without a dev server, you need * to use .js files or pre-compile your TypeScript. * * @param code - The module script content * @param baseUrl - The URL to resolve relative paths against (component's URL) * @returns The code with rewritten imports */ export declare function rewriteImports(code: string, baseUrl: string): string; /** * Executes a single inline module script. * * @param script - The script element containing module code * @param componentUrl - The component's URL for resolving relative imports * @param componentId - Unique ID for tracking blob URLs * @returns Promise that resolves when the module has executed */ export declare function executeModuleScript(script: ScriptElement, componentUrl: string, componentId?: string): Promise; /** * Transforms import statements to wrap imported values in reactive proxies. * This enables imported arrays to trigger UI updates when mutated. * * Transforms: * import { foo, bar } from "./module.js"; * * Into: * import { foo as __raw_foo, bar as __raw_bar } from "./module.js"; * const foo = __wrapReactiveArray(__raw_foo, __ladrillos_componentId, "foo"); * const bar = __wrapReactiveArray(__raw_bar, __ladrillos_componentId, "bar"); * * @param code - The module code with imports * @returns Transformed code with reactive import wrapping * * Exported for tests (the blob-URL execution path can't run under Node). */ export declare function transformImportsForReactivity(code: string): string; export declare function generateHelperInjectionCode(componentId?: string, componentUrl?: string, exclude?: ReadonlySet): string; /** * Executes an external module script. * For external scripts, we fetch the content, auto-export all declarations, * and execute it via blob URL with injected framework helpers. * * If the script has the 'external' attribute, it's loaded as a plain * external script without any framework processing (no helpers, no auto-exports). * * @param script - The external script element * @param componentId - Optional component ID for event bus cleanup * @param componentUrl - Optional component URL for path resolution * @returns Promise that resolves with the module exports */ export declare function executeExternalScript(script: ExternalScriptElement, componentId?: string, componentUrl?: string): Promise; /** * Loads external scripts marked with the 'external' attribute. * These are third-party libraries (like highlight.js) that need to be loaded * BEFORE the component's inline scripts run, since they may depend on globals * provided by these libraries. * * @param externalScripts - External scripts from the component * @returns Promise that resolves when all external scripts are loaded */ export declare function loadPlainExternalScripts(externalScripts: ExternalScriptElement[]): Promise; /** * Loads external stylesheets () into the component. * For Shadow DOM components, fetches CSS content and injects directly into shadow root. * For light DOM components, adds to document head. * * @param externalStyles - External stylesheets from the component * @param root - The component's root (shadow root or element itself) * @param useShadowDOM - Whether the component uses Shadow DOM * @returns Promise that resolves when all stylesheets are loaded */ export declare function loadExternalStyles(externalStyles: Array<{ href: string; rel: string; }>, root?: ShadowRoot | HTMLElement, useShadowDOM?: boolean): Promise; /** * Executes all module scripts for a component. * Handles both inline and external scripts (module and non-module). * * @param scripts - Inline scripts from the component * @param externalScripts - External scripts from the component * @param componentUrl - The component's URL for resolving imports * @param componentId - Unique ID for cleanup tracking * @returns Promise that resolves when all modules have executed */ export declare function executeAllModuleScripts(scripts: ScriptElement[], externalScripts: ExternalScriptElement[], componentUrl: string, componentId?: string): Promise>; /** * Cleans up blob URLs created for a component. * Call this when a component is disconnected to prevent memory leaks. * * @param componentId - The component's unique ID */ export declare function cleanupModuleScripts(componentId: string): void; /** * Extracts import specifiers from module code. * Useful for debugging or pre-fetching dependencies. * * @param code - The module script content * @returns Array of import specifiers found in the code */ export declare function extractImportSpecifiers(code: string): string[]; /** * Represents a parsed import statement */ export interface ParsedImport { statement: string; specifier: string; imports: ImportBinding[]; isDefault: boolean; isNamespace: boolean; isSideEffect: boolean; } export interface ImportBinding { imported: string; local: string; } /** * Parses import statements from module code. * Returns structured info about each import. */ export declare function parseImports(code: string): ParsedImport[]; /** * Removes import statements from code, leaving just the executable code. */ export declare function stripImports(code: string): string; /** * Removes export syntax, leaving the declarations behind. * * A component's module script is inlined into a function body rather than * evaluated as a module, so `export` is a syntax error there. Nothing is lost: * every top-level declaration already becomes reactive state, which is what the * export was signalling. */ export declare function stripExports(code: string): string; /** * Extracts ONLY top-level variable and function names from code. * * This is critical for reactive state - we must NOT extract variables * declared inside functions (like `const myCanvas = refs.get(...)`). * * The approach: Track brace depth and only extract declarations at depth 0. */ export declare function extractDeclaredNames(code: string): { variables: string[]; functions: string[]; }; /** * Executes a module script and extracts declared variables for reactive state. * * This is the KEY function for reactivity support in module scripts. * It: * 1. Resolves all imports (wrapping arrays in reactive proxies) * 2. Strips import statements from the code * 3. Transforms variable access to go through the reactive state object * 4. Executes the remaining code in a sandbox with imports available * 5. Functions read/write directly to the reactive state for full reactivity * * The transformation ensures that functions declared in module scripts * read/write from the reactive state, not from local closure variables. * This makes `let x = 0; function inc() { x++; }` work reactively. * * @param reactiveState - The component's reactive state object. Module script * functions will read/write directly to this object. * @param onStateChange - Optional callback when imported arrays are mutated. * This triggers directive updates (like $for loops). */ export declare function executeModuleScriptWithReactivity(script: ScriptElement, componentUrl: string, componentId?: string, refs?: Map, reactiveState?: Record, onStateChange?: () => void, hostElement?: HTMLElement): Promise>; /** * Executes all module scripts with reactivity support. * Returns merged state from all module scripts. * @param refs - Optional refs Map that will be populated by scanDirectives later. * Functions in module scripts can capture this reference. * @param reactiveState - The component's reactive state object. Module script * functions will read/write directly to this object. * @param onStateChange - Optional callback when imported arrays are mutated. * This triggers directive updates (like $for loops). */ export declare function executeModuleScriptsWithReactivity(scripts: ScriptElement[], externalScripts: ExternalScriptElement[], componentUrl: string, componentId?: string, refs?: Map, reactiveState?: Record, onStateChange?: () => void, hostElement?: HTMLElement): Promise>;