MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / rewriteImports

Function rewriteImports

packages/vite/hmr/server/websocket.ts:1779–2158  ·  view source on GitHub ↗

* THE SINGLE REWRITE FUNCTION - used everywhere for consistency

(code: string, importerPath: string, sfcFileMap: Map<string, string>, depFileMap: Map<string, string>, projectRoot: string, verbose: boolean = false, outputDirOverrideRel?: string, httpOrigin?: string)

Source from the content-addressed store, hash-verified

1777 * THE SINGLE REWRITE FUNCTION - used everywhere for consistency
1778 */
1779function rewriteImports(code: string, importerPath: string, sfcFileMap: Map<string, string>, depFileMap: Map<string, string>, projectRoot: string, verbose: boolean = false, outputDirOverrideRel?: string, httpOrigin?: string): string {
1780 let result = code;
1781 const httpOriginSafe = httpOrigin;
1782 const importerDir = path.posix.dirname(importerPath);
1783 // Determine importer output relative path (project-relative .mjs) to compute relative imports consistently
1784 const importerOutRel = outputDirOverrideRel || getProjectRelativeImportPath(importerPath, projectRoot) || stripToProjectRelative(importerPath, projectRoot).replace(/\.(ts|js|tsx|jsx|mjs|mts|cts)$/i, '.mjs');
1785 const importerOutDir = importerOutRel ? path.posix.dirname(importerOutRel) : '';
1786 const ensureRel = (p: string) => (p.startsWith('.') ? p : `./${p}`);
1787
1788 // Normalize all @nativescript/core imports to the unified HTTP ESM core bridge to guarantee a single realm on device
1789 try {
1790 let coreAliasIdx = 0;
1791 const mkAlias = () => `__NSC${coreAliasIdx++}`;
1792 const coreUrl = (sub?: string) => {
1793 const p = (sub || '').replace(/^\//, '');
1794 return `${httpOriginSafe || ''}/ns/core` + (p ? `?p=${p}` : '');
1795 };
1796 // Case 1: import { A, B } from '@nativescript/core[/sub]'
1797 result = result.replace(/(^|\n)\s*import\s*\{\s*([^}]+?)\s*\}\s*from\s+["']@nativescript\/core([^"'\n]*)["'];?/g, (_m, pfx: string, names: string, sub: string) => {
1798 const alias = mkAlias();
1799 const url = coreUrl(sub || '');
1800 const cleaned = names
1801 .split(',')
1802 .map((s) => s.trim())
1803 .filter(Boolean)
1804 .join(', ');
1805 return `${pfx}import * as ${alias} from ${JSON.stringify(url)};\nconst { ${cleaned} } = ${alias};`;
1806 });
1807 // Case 2: import Default, { A, B } from '@nativescript/core[/sub]'
1808 result = result.replace(/(^|\n)\s*import\s+([A-Za-z_$][\w$]*)\s*,\s*\{([^}]+?)\s*\}\s*from\s*["']@nativescript\/core([^"'\n]*)["'];?/g, (_m, pfx: string, defName: string, names: string, sub: string) => {
1809 const alias = mkAlias();
1810 const url = coreUrl(sub || '');
1811 const cleaned = names
1812 .split(',')
1813 .map((s) => s.trim())
1814 .filter(Boolean)
1815 .join(', ');
1816 return `${pfx}import * as ${alias} from ${JSON.stringify(url)};\nconst ${defName} = (${alias}.default || ${alias});\nconst { ${cleaned} } = ${alias};`;
1817 });
1818 // Case 3: import Default from '@nativescript/core[/sub]'
1819 result = result.replace(/(^|\n)\s*import\s+([A-Za-z_$][\w$]*)\s+from\s*["']@nativescript\/core([^"'\n]*)["'];?/g, (_m, pfx: string, defName: string, sub: string) => {
1820 const alias = mkAlias();
1821 const url = coreUrl(sub || '');
1822 return `${pfx}import * as ${alias} from ${JSON.stringify(url)};\nconst ${defName} = (${alias}.default || ${alias});`;
1823 });
1824 // Case 4: import * as NS from '@nativescript/core[/sub]'
1825 result = result.replace(/(^|\n)\s*import\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s*["']@nativescript\/core([^"'\n]*)["'];?/g, (_m, pfx: string, nsName: string, sub: string) => {
1826 const url = coreUrl(sub || '');
1827 return `${pfx}import * as ${nsName} from ${JSON.stringify(url)};`;
1828 });
1829 // Case 5: side-effect import '@nativescript/core[/sub]'
1830 result = result.replace(/(^|\n)\s*import\s*["']@nativescript\/core([^"'\n]*)["'];?/g, (_m, pfx: string, sub: string) => {
1831 const url = coreUrl(sub || '');
1832 return `${pfx}import ${JSON.stringify(url)};`;
1833 });
1834 // Case 6: dynamic import('@nativescript/core[/sub]')
1835 result = result.replace(/import\(\s*["']@nativescript\/core([^"'\n]*)["']\s*\)/g, (_m, sub: string) => {
1836 const url = coreUrl(sub || '');

Callers 2

configureServerFunction · 0.85
handleHotUpdateFunction · 0.85

Calls 15

stripToProjectRelativeFunction · 0.85
mkAliasFunction · 0.85
coreUrlFunction · 0.85
resolveVueKeyFunction · 0.85
ensureRelFunction · 0.85
toAppModuleBaseIdFunction · 0.85
replaceMethod · 0.80
joinMethod · 0.80
resolveMethod · 0.80
warnMethod · 0.80
indexOfMethod · 0.80

Tested by

no test coverage detected