|
| 1 | +/** |
| 2 | + * Centralized dynamic converter loader. |
| 3 | + * |
| 4 | + * Instead of bundling heavy converter packages, each converter is lazy-loaded |
| 5 | + * on demand from the jsdelivr CDN when the user actually requests an export. |
| 6 | + * Modules are cached after first successful load; failed loads are cleared |
| 7 | + * so a retry can succeed. |
| 8 | + */ |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// Module type declarations |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +type GerberConverterModule = { |
| 15 | + stringifyGerberCommandLayers: (...args: any[]) => Record<string, string> |
| 16 | + convertSoupToGerberCommands: (...args: any[]) => any |
| 17 | + convertSoupToExcellonDrillCommands: (...args: any[]) => any |
| 18 | + stringifyExcellonDrill: (...args: any[]) => string |
| 19 | +} |
| 20 | + |
| 21 | +type BomCsvConverterModule = { |
| 22 | + convertCircuitJsonToBomRows: (...args: any[]) => Promise<any[]> | any[] |
| 23 | + convertBomRowsToCsv: (...args: any[]) => Promise<string> | string |
| 24 | +} |
| 25 | + |
| 26 | +type PnpCsvConverterModule = { |
| 27 | + convertCircuitJsonToPickAndPlaceCsv: ( |
| 28 | + ...args: any[] |
| 29 | + ) => Promise<string> | string |
| 30 | +} |
| 31 | + |
| 32 | +type KicadConverterModule = { |
| 33 | + CircuitJsonToKicadPcbConverter: new ( |
| 34 | + ...args: any[] |
| 35 | + ) => { |
| 36 | + runUntilFinished: () => void |
| 37 | + getOutputString: () => string |
| 38 | + getModel3dSourcePaths: () => string[] |
| 39 | + } |
| 40 | + CircuitJsonToKicadSchConverter: new ( |
| 41 | + ...args: any[] |
| 42 | + ) => { |
| 43 | + runUntilFinished: () => void |
| 44 | + getOutputString: () => string |
| 45 | + } |
| 46 | + CircuitJsonToKicadProConverter: new ( |
| 47 | + ...args: any[] |
| 48 | + ) => { |
| 49 | + runUntilFinished: () => void |
| 50 | + getOutputString: () => string |
| 51 | + } |
| 52 | + CircuitJsonToKicadLibraryConverter: new ( |
| 53 | + ...args: any[] |
| 54 | + ) => { |
| 55 | + runUntilFinished: () => void |
| 56 | + getOutput: () => { |
| 57 | + kicadSymString: string |
| 58 | + footprints: Array<{ footprintName: string; kicadModString: string }> |
| 59 | + model3dSourcePaths: string[] |
| 60 | + fpLibTableString: string |
| 61 | + symLibTableString: string |
| 62 | + } |
| 63 | + } |
| 64 | + resolveAndLoadKicad3dModelFiles: (...args: any[]) => Promise<void> |
| 65 | +} |
| 66 | + |
| 67 | +type GltfConverterModule = { |
| 68 | + convertCircuitJsonToGltf: ( |
| 69 | + ...args: any[] |
| 70 | + ) => Promise<ArrayBuffer> | ArrayBuffer |
| 71 | +} |
| 72 | + |
| 73 | +type StepConverterModule = { |
| 74 | + circuitJsonToStep: (...args: any[]) => Promise<string> | string |
| 75 | +} |
| 76 | + |
| 77 | +type LbrnConverterModule = { |
| 78 | + convertCircuitJsonToLbrn: (...args: any[]) => Promise<{ toXml: () => string }> |
| 79 | +} |
| 80 | + |
| 81 | +// --------------------------------------------------------------------------- |
| 82 | +// Package → type mapping |
| 83 | +// --------------------------------------------------------------------------- |
| 84 | + |
| 85 | +type ConverterModules = { |
| 86 | + "circuit-json-to-gerber": GerberConverterModule |
| 87 | + "circuit-json-to-bom-csv": BomCsvConverterModule |
| 88 | + "circuit-json-to-pnp-csv": PnpCsvConverterModule |
| 89 | + "circuit-json-to-kicad": KicadConverterModule |
| 90 | + "circuit-json-to-gltf": GltfConverterModule |
| 91 | + "circuit-json-to-step": StepConverterModule |
| 92 | + "circuit-json-to-lbrn": LbrnConverterModule |
| 93 | +} |
| 94 | + |
| 95 | +export type ConverterPackageName = keyof ConverterModules |
| 96 | + |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | +// Pinned CDN URLs (jsdelivr ESM) |
| 99 | +// --------------------------------------------------------------------------- |
| 100 | + |
| 101 | +const CONVERTER_CDN_URLS: Record<ConverterPackageName, string> = { |
| 102 | + "circuit-json-to-gerber": |
| 103 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-gerber@0.0.76/+esm", |
| 104 | + "circuit-json-to-bom-csv": |
| 105 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-bom-csv@0.0.9/+esm", |
| 106 | + "circuit-json-to-pnp-csv": |
| 107 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-pnp-csv@0.0.8/+esm", |
| 108 | + "circuit-json-to-kicad": |
| 109 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-kicad@0.0.147/+esm", |
| 110 | + "circuit-json-to-gltf": |
| 111 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-gltf@0.0.102/+esm", |
| 112 | + "circuit-json-to-step": |
| 113 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-step@0.0.33/+esm", |
| 114 | + "circuit-json-to-lbrn": |
| 115 | + "https://cdn.jsdelivr.net/npm/circuit-json-to-lbrn@0.0.82/+esm", |
| 116 | +} |
| 117 | + |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +// Loader with singleton-promise cache + failure retry |
| 120 | +// --------------------------------------------------------------------------- |
| 121 | + |
| 122 | +export type ConverterModuleImporter = <TModule>( |
| 123 | + specifier: string, |
| 124 | +) => Promise<TModule> |
| 125 | + |
| 126 | +declare global { |
| 127 | + var tscircuitDynamicModules: Record<string, unknown> | undefined |
| 128 | +} |
| 129 | + |
| 130 | +const converterModulePromises = new Map< |
| 131 | + ConverterPackageName, |
| 132 | + Promise<ConverterModules[ConverterPackageName]> |
| 133 | +>() |
| 134 | + |
| 135 | +const importConverterModule: ConverterModuleImporter = (specifier) => |
| 136 | + import(/* @vite-ignore */ specifier) |
| 137 | + |
| 138 | +export const getConverterCdnUrl = (packageName: ConverterPackageName) => |
| 139 | + CONVERTER_CDN_URLS[packageName] |
| 140 | + |
| 141 | +export const clearConverterModuleCache = () => { |
| 142 | + converterModulePromises.clear() |
| 143 | +} |
| 144 | + |
| 145 | +const registerConverterModule = <TName extends ConverterPackageName>( |
| 146 | + packageName: TName, |
| 147 | + module: ConverterModules[TName], |
| 148 | +) => { |
| 149 | + globalThis.tscircuitDynamicModules ??= {} |
| 150 | + globalThis.tscircuitDynamicModules[packageName] = module |
| 151 | + return module |
| 152 | +} |
| 153 | + |
| 154 | +export const loadConverterModule = async <TName extends ConverterPackageName>( |
| 155 | + packageName: TName, |
| 156 | + importer: ConverterModuleImporter = importConverterModule, |
| 157 | +): Promise<ConverterModules[TName]> => { |
| 158 | + let modulePromise = converterModulePromises.get(packageName) as |
| 159 | + | Promise<ConverterModules[TName]> |
| 160 | + | undefined |
| 161 | + |
| 162 | + if (!modulePromise) { |
| 163 | + modulePromise = importer<ConverterModules[TName]>( |
| 164 | + getConverterCdnUrl(packageName), |
| 165 | + ) |
| 166 | + .then((module) => registerConverterModule(packageName, module)) |
| 167 | + .catch((error) => { |
| 168 | + // Clear cache on failure so next call retries |
| 169 | + converterModulePromises.delete(packageName) |
| 170 | + throw error |
| 171 | + }) |
| 172 | + converterModulePromises.set( |
| 173 | + packageName, |
| 174 | + modulePromise as Promise<ConverterModules[ConverterPackageName]>, |
| 175 | + ) |
| 176 | + } |
| 177 | + |
| 178 | + return modulePromise |
| 179 | +} |
| 180 | + |
| 181 | +// --------------------------------------------------------------------------- |
| 182 | +// Convenience loaders (one per converter) |
| 183 | +// --------------------------------------------------------------------------- |
| 184 | + |
| 185 | +export const loadGerberConverter = () => |
| 186 | + loadConverterModule("circuit-json-to-gerber") |
| 187 | + |
| 188 | +export const loadBomCsvConverter = () => |
| 189 | + loadConverterModule("circuit-json-to-bom-csv") |
| 190 | + |
| 191 | +export const loadPnpCsvConverter = () => |
| 192 | + loadConverterModule("circuit-json-to-pnp-csv") |
| 193 | + |
| 194 | +export const loadKicadConverter = () => |
| 195 | + loadConverterModule("circuit-json-to-kicad") |
| 196 | + |
| 197 | +export const loadGltfConverter = () => |
| 198 | + loadConverterModule("circuit-json-to-gltf") |
| 199 | + |
| 200 | +export const loadStepConverter = () => |
| 201 | + loadConverterModule("circuit-json-to-step") |
| 202 | + |
| 203 | +export const loadLbrnConverter = () => |
| 204 | + loadConverterModule("circuit-json-to-lbrn") |
0 commit comments