@tscircuit/internal-dynamic-import 0.0.16 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -14
- package/dist/index.d.ts +624 -3
- package/dist/index.js +72 -28
- package/dist/index.js.map +1 -1
- package/lib/create-importer.ts +27 -0
- package/lib/dynamic-import-resolver.ts +58 -0
- package/lib/dynamic-module-registry.ts +37 -0
- package/lib/import-urls.ts +27 -0
- package/lib/index.ts +46 -0
- package/lib/supported-modules.ts +88 -0
- package/lib/type-bundles/@tscircuit/circuit-json-schematic-placement-analysis.d.ts +593 -0
- package/lib/type-bundles/circuit-json-to-3d-png.d.ts +48 -0
- package/lib/type-bundles/circuit-json-to-bom-csv.d.ts +36 -0
- package/lib/type-bundles/circuit-json-to-bpc.d.ts +15 -0
- package/lib/type-bundles/circuit-json-to-connectivity-map.d.ts +44 -0
- package/lib/type-bundles/circuit-json-to-fdm-component-box.d.ts +160 -0
- package/lib/type-bundles/circuit-json-to-gerber.d.ts +672 -0
- package/lib/type-bundles/circuit-json-to-gltf.d.ts +273 -0
- package/lib/type-bundles/circuit-json-to-kicad.d.ts +392 -0
- package/lib/type-bundles/circuit-json-to-lbrn.d.ts +54 -0
- package/lib/type-bundles/circuit-json-to-pnp-csv.d.ts +15 -0
- package/lib/type-bundles/circuit-json-to-readable-netlist.d.ts +5 -0
- package/lib/type-bundles/circuit-json-to-simple-3d.d.ts +41 -0
- package/lib/type-bundles/circuit-json-to-spice.d.ts +242 -0
- package/lib/type-bundles/circuit-json-to-step.d.ts +28 -0
- package/lib/type-bundles/circuit-json-to-tscircuit.d.ts +14 -0
- package/lib/type-bundles/circuit-to-canvas.d.ts +646 -0
- package/lib/type-bundles/circuit-to-svg.d.ts +399 -0
- package/lib/type-bundles/kicad-to-circuit-json.d.ts +375 -0
- package/package.json +10 -3
package/dist/index.js
CHANGED
|
@@ -31,51 +31,95 @@ var supportedModules = [
|
|
|
31
31
|
"circuit-json-to-tscircuit",
|
|
32
32
|
"circuit-to-canvas",
|
|
33
33
|
"circuit-to-svg",
|
|
34
|
-
"kicad-to-circuit-json"
|
|
34
|
+
"kicad-to-circuit-json",
|
|
35
|
+
"@tscircuit/circuit-json-schematic-placement-analysis"
|
|
35
36
|
];
|
|
36
37
|
var getModuleName = (specifier) => {
|
|
37
|
-
const versionSeparatorIndex = specifier.indexOf(
|
|
38
|
+
const versionSeparatorIndex = specifier.indexOf(
|
|
39
|
+
"@",
|
|
40
|
+
specifier.startsWith("@") ? 1 : 0
|
|
41
|
+
);
|
|
38
42
|
return versionSeparatorIndex === -1 ? specifier : specifier.slice(0, versionSeparatorIndex);
|
|
39
43
|
};
|
|
40
44
|
|
|
41
|
-
// lib/
|
|
45
|
+
// lib/import-urls.ts
|
|
42
46
|
var supportedModuleSet = new Set(supportedModules);
|
|
47
|
+
var browserEntrypoints = /* @__PURE__ */ new Map([
|
|
48
|
+
["@tscircuit/circuit-json-schematic-placement-analysis", "dist/browser.js"]
|
|
49
|
+
]);
|
|
50
|
+
function assertSupportedModule(specifier) {
|
|
51
|
+
if (!supportedModuleSet.has(getModuleName(specifier))) {
|
|
52
|
+
throw new Error(`Unsupported module: ${specifier}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
43
55
|
var getImportUrl = (specifier) => {
|
|
44
56
|
assertSupportedModule(specifier);
|
|
45
|
-
|
|
57
|
+
const moduleName = getModuleName(specifier);
|
|
58
|
+
const version = specifier.length === moduleName.length ? "latest" : specifier.slice(moduleName.length + 1);
|
|
59
|
+
const entrypoint = browserEntrypoints.get(moduleName) ?? "+esm";
|
|
60
|
+
return `https://jscdn.tscircuit.com/${moduleName}/${version}/${entrypoint}`;
|
|
46
61
|
};
|
|
47
62
|
var getImportUrls = (specifier) => {
|
|
48
|
-
|
|
49
|
-
return [getJscdnImportUrl(specifier), `https://esm.run/${specifier}`];
|
|
63
|
+
return [getImportUrl(specifier), `https://esm.run/${specifier}`];
|
|
50
64
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
65
|
+
|
|
66
|
+
// lib/create-importer.ts
|
|
67
|
+
function createDefaultDynamicImportResolver(importModule = (url) => import(
|
|
68
|
+
/* @vite-ignore */
|
|
69
|
+
/* webpackIgnore: true */
|
|
70
|
+
url
|
|
71
|
+
)) {
|
|
72
|
+
return async (specifier) => {
|
|
73
|
+
const modules = getDynamicModuleRegistry();
|
|
74
|
+
if (Object.hasOwn(modules, specifier)) return modules[specifier];
|
|
75
|
+
let lastError;
|
|
76
|
+
for (const importUrl of getImportUrls(specifier)) {
|
|
77
|
+
try {
|
|
78
|
+
const mod = await importModule(importUrl);
|
|
79
|
+
modules[specifier] = mod;
|
|
80
|
+
return mod;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
lastError = error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
throw lastError;
|
|
86
|
+
};
|
|
55
87
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
88
|
+
|
|
89
|
+
// lib/dynamic-import-resolver.ts
|
|
90
|
+
var getDynamicImportResolver = () => globalThis.tscircuitDynamicImportResolver;
|
|
91
|
+
function setDynamicImportResolver(resolver) {
|
|
92
|
+
globalThis.tscircuitDynamicImportResolver = resolver;
|
|
61
93
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
);
|
|
70
|
-
return registerDynamicModule(getModuleName(specifier), mod);
|
|
71
|
-
} catch (error) {
|
|
72
|
-
lastError = error;
|
|
94
|
+
function createDynamicImporter(loaders, fallback) {
|
|
95
|
+
const entries = new Map(Object.entries(loaders));
|
|
96
|
+
const pending = /* @__PURE__ */ new Map();
|
|
97
|
+
async function importer2(specifier) {
|
|
98
|
+
const loader = entries.get(specifier);
|
|
99
|
+
if (!loader) {
|
|
100
|
+
if (fallback) return fallback(specifier);
|
|
101
|
+
throw new Error(`No bundled module registered for: ${specifier}`);
|
|
73
102
|
}
|
|
103
|
+
if (!pending.has(specifier)) {
|
|
104
|
+
const load = Promise.resolve().then(loader).catch((error) => {
|
|
105
|
+
pending.delete(specifier);
|
|
106
|
+
throw error;
|
|
107
|
+
});
|
|
108
|
+
pending.set(specifier, load);
|
|
109
|
+
}
|
|
110
|
+
return pending.get(specifier);
|
|
74
111
|
}
|
|
75
|
-
|
|
112
|
+
return importer2;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// lib/index.ts
|
|
116
|
+
var loadModule = createDefaultDynamicImportResolver();
|
|
117
|
+
async function importer(specifier) {
|
|
118
|
+
const resolver = getDynamicImportResolver();
|
|
119
|
+
return resolver ? resolver(specifier, loadModule) : loadModule(specifier);
|
|
76
120
|
}
|
|
77
121
|
var index_default = importer;
|
|
78
122
|
|
|
79
|
-
export { index_default as default, getDynamicModuleRegistry, getImportUrl, getImportUrls, getModuleName, registerDynamicModule, supportedModules };
|
|
123
|
+
export { createDynamicImporter, index_default as default, getDynamicModuleRegistry, getImportUrl, getImportUrls, getModuleName, registerDynamicModule, setDynamicImportResolver, supportedModules };
|
|
80
124
|
//# sourceMappingURL=index.js.map
|
|
81
125
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../lib/dynamic-module-registry.ts","../lib/supported-modules.ts","../lib/index.ts"],"names":[],"mappings":";AAYO,IAAM,2BAA2B,MAA6B;AACnE,EAAA,IAAI,WAAW,uBAAA,EAAyB;AACtC,IAAA,OAAO,UAAA,CAAW,uBAAA;AAAA,EACpB;AAEA,EAAA,MAAM,WAAkC,EAAC;AACzC,EAAA,UAAA,CAAW,uBAAA,GAA0B,QAAA;AACrC,EAAA,OAAO,QAAA;AACT;AAUO,SAAS,qBAAA,CACd,MACA,GAAA,EACS;AACT,EAAA,wBAAA,EAAyB,CAAE,IAAI,CAAA,GAAI,G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OAAO,CAAA,4BAAA,EAA+B,UAAU,CAAA,CAAA,EAAI,OAAO,CAAA,KAAA,CAAA;AAC7D;AAEA,SAAS,sBAAsB,SAAA,EAAmB;AAChD,EAAA,MAAM,UAAA,GAAa,cAAc,SAAS,CAAA;AAE1C,EAAA,IAAI,CAAC,kBAAA,CAAmB,GAAA,CAAI,UAAU,CAAA,EAAG;AACvC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,oBAAA,EAAuB,SAAS,CAAA,CAAE,CAAA;AAAA,EACpD;AACF;AAMA,eAAe,SAAS,SAAA,EAAqC;AAC3D,EAAA,IAAI,SAAA;AAEJ,EAAA,KAAA,MAAW,SAAA,IAAa,aAAA,CAAc,SAAS,CAAA,EAAG;AAChD,IAAA,IAAI;AACF,MAAA,MAAM,MAAM,MAAM;AAAA;AAAA,QAA0B;AAAA,OAAA;AAC5C,MAAA,OAAO,qBAAA,CAAsB,aAAA,CAAc,SAAS,CAAA,EAAG,GAAG,CAAA;AAAA,IAC5D,SAAS,KAAA,EAAO;AACd,MAAA,SAAA,GAAY,KAAA;AAAA,IACd;AAAA,EACF;AAEA,EAAA,MAAM,SAAA;AACR;AAEA,IAAO,aAAA,GAAQ","file":"index.js","sourcesContent":["import type {\n SupportedModuleMap,\n SupportedModuleName,\n} from \"./supported-modules\"\n\nexport type DynamicModuleRegistry = Partial<SupportedModuleMap> &\n Record<string, unknown>\n\ndeclare global {\n var tscircuitDynamicModules: DynamicModuleRegistry | undefined\n}\n\nexport const getDynamicModuleRegistry = (): DynamicModuleRegistry => {\n if (globalThis.tscircuitDynamicModules) {\n return globalThis.tscircuitDynamicModules\n }\n\n const registry: DynamicModuleRegistry = {}\n globalThis.tscircuitDynamicModules = registry\n return registry\n}\n\nexport function registerDynamicModule<TSpecifier extends SupportedModuleName>(\n name: TSpecifier,\n mod: SupportedModuleMap[TSpecifier],\n): SupportedModuleMap[TSpecifier]\nexport function registerDynamicModule<TModule>(\n name: string,\n mod: TModule,\n): TModule\nexport function registerDynamicModule<TModule>(\n name: string,\n mod: TModule,\n): TModule {\n getDynamicModuleRegistry()[name] = mod\n return mod\n}\n","import type * as CircuitJsonTo3dPngModule from \"./type-bundles/circuit-json-to-3d-png\"\nimport type * as CircuitJsonToBomCsvModule from \"./type-bundles/circuit-json-to-bom-csv\"\nimport type * as CircuitJsonToBpcModule from \"./type-bundles/circuit-json-to-bpc\"\nimport type * as CircuitJsonToConnectivityMapModule from \"./type-bundles/circuit-json-to-connectivity-map\"\nimport type * as CircuitJsonToFdmComponentBoxModule from \"./type-bundles/circuit-json-to-fdm-component-box\"\nimport type * as CircuitJsonToGerberModule from \"./type-bundles/circuit-json-to-gerber\"\nimport type * as CircuitJsonToGltfModule from \"./type-bundles/circuit-json-to-gltf\"\nimport type * as CircuitJsonToKicadModule from \"./type-bundles/circuit-json-to-kicad\"\nimport type * as CircuitJsonToLbrnModule from \"./type-bundles/circuit-json-to-lbrn\"\nimport type * as CircuitJsonToPnpCsvModule from \"./type-bundles/circuit-json-to-pnp-csv\"\nimport type * as CircuitJsonToReadableNetlistModule from \"./type-bundles/circuit-json-to-readable-netlist\"\nimport type * as CircuitJsonToSimple3dModule from \"./type-bundles/circuit-json-to-simple-3d\"\nimport type * as CircuitJsonToSpiceModule from \"./type-bundles/circuit-json-to-spice\"\nimport type * as CircuitJsonToStepModule from \"./type-bundles/circuit-json-to-step\"\nimport type * as CircuitJsonToTscircuitModule from \"./type-bundles/circuit-json-to-tscircuit\"\nimport type * as CircuitToCanvasModule from \"./type-bundles/circuit-to-canvas\"\nimport type * as CircuitToSvgModule from \"./type-bundles/circuit-to-svg\"\nimport type * as KicadToCircuitJsonModule from \"./type-bundles/kicad-to-circuit-json\"\n\nexport const supportedModules = [\n \"circuit-json-to-3d-png\",\n \"circuit-json-to-bom-csv\",\n \"circuit-json-to-bpc\",\n \"circuit-json-to-connectivity-map\",\n \"circuit-json-to-fdm-component-box\",\n \"circuit-json-to-gerber\",\n \"circuit-json-to-gltf\",\n \"circuit-json-to-kicad\",\n \"circuit-json-to-lbrn\",\n \"circuit-json-to-pnp-csv\",\n \"circuit-json-to-readable-netlist\",\n \"circuit-json-to-simple-3d\",\n \"circuit-json-to-spice\",\n \"circuit-json-to-step\",\n \"circuit-json-to-tscircuit\",\n \"circuit-to-canvas\",\n \"circuit-to-svg\",\n \"kicad-to-circuit-json\",\n] as const\n\nexport type SupportedModuleName = (typeof supportedModules)[number]\nexport type SupportedModuleSpecifier =\n | SupportedModuleName\n | `${SupportedModuleName}@${string}`\n\nexport interface SupportedModuleMap {\n \"circuit-json-to-3d-png\": typeof CircuitJsonTo3dPngModule\n \"circuit-json-to-bom-csv\": typeof CircuitJsonToBomCsvModule\n \"circuit-json-to-bpc\": typeof CircuitJsonToBpcModule\n \"circuit-json-to-connectivity-map\": typeof CircuitJsonToConnectivityMapModule\n \"circuit-json-to-fdm-component-box\": typeof CircuitJsonToFdmComponentBoxModule\n \"circuit-json-to-gerber\": typeof CircuitJsonToGerberModule\n \"circuit-json-to-gltf\": typeof CircuitJsonToGltfModule\n \"circuit-json-to-kicad\": typeof CircuitJsonToKicadModule\n \"circuit-json-to-lbrn\": typeof CircuitJsonToLbrnModule\n \"circuit-json-to-pnp-csv\": typeof CircuitJsonToPnpCsvModule\n \"circuit-json-to-readable-netlist\": typeof CircuitJsonToReadableNetlistModule\n \"circuit-json-to-simple-3d\": typeof CircuitJsonToSimple3dModule\n \"circuit-json-to-spice\": typeof CircuitJsonToSpiceModule\n \"circuit-json-to-step\": typeof CircuitJsonToStepModule\n \"circuit-json-to-tscircuit\": typeof CircuitJsonToTscircuitModule\n \"circuit-to-canvas\": typeof CircuitToCanvasModule\n \"circuit-to-svg\": typeof CircuitToSvgModule\n \"kicad-to-circuit-json\": typeof KicadToCircuitJsonModule\n}\n\nexport type StripVersion<TSpecifier extends string> =\n TSpecifier extends `${infer TBase}@${string}`\n ? TBase extends SupportedModuleName\n ? TBase\n : never\n : TSpecifier extends SupportedModuleName\n ? TSpecifier\n : never\n\nexport const getModuleName = (specifier: string): string => {\n const versionSeparatorIndex = specifier.indexOf(\"@\")\n return versionSeparatorIndex === -1\n ? specifier\n : specifier.slice(0, versionSeparatorIndex)\n}\n","import { registerDynamicModule } from \"./dynamic-module-registry\"\nimport {\n getModuleName,\n supportedModules,\n type StripVersion,\n type SupportedModuleMap,\n type SupportedModuleSpecifier,\n} from \"./supported-modules\"\n\nexport {\n getDynamicModuleRegistry,\n registerDynamicModule,\n} from \"./dynamic-module-registry\"\nexport type { DynamicModuleRegistry } from \"./dynamic-module-registry\"\nexport {\n getModuleName,\n supportedModules,\n} from \"./supported-modules\"\nexport type {\n StripVersion,\n SupportedModuleMap,\n SupportedModuleName,\n SupportedModuleSpecifier,\n} from \"./supported-modules\"\n\nconst supportedModuleSet = new Set<string>(supportedModules)\n\nexport const getImportUrl = (specifier: string): string => {\n assertSupportedModule(specifier)\n\n return getJscdnImportUrl(specifier)\n}\n\nexport const getImportUrls = (specifier: string): string[] => {\n assertSupportedModule(specifier)\n\n return [getJscdnImportUrl(specifier), `https://esm.run/${specifier}`]\n}\n\nfunction getJscdnImportUrl(specifier: string): string {\n const moduleName = getModuleName(specifier)\n const version =\n specifier.length === moduleName.length\n ? \"latest\"\n : specifier.slice(moduleName.length + 1)\n\n return `https://jscdn.tscircuit.com/${moduleName}/${version}/+esm`\n}\n\nfunction assertSupportedModule(specifier: string) {\n const moduleName = getModuleName(specifier)\n\n if (!supportedModuleSet.has(moduleName)) {\n throw new Error(`Unsupported module: ${specifier}`)\n }\n}\n\nasync function importer<TSpecifier extends SupportedModuleSpecifier>(\n specifier: TSpecifier,\n): Promise<SupportedModuleMap[StripVersion<TSpecifier>]>\nasync function importer(specifier: string): Promise<unknown>\nasync function importer(specifier: string): Promise<unknown> {\n let lastError: unknown\n\n for (const importUrl of getImportUrls(specifier)) {\n try {\n const mod = await import(/* @vite-ignore */ importUrl)\n return registerDynamicModule(getModuleName(specifier), mod)\n } catch (error) {\n lastError = error\n }\n }\n\n throw lastError\n}\n\nexport default importer\n"]}
|
|
1
|
+
{"version":3,"sources":["../lib/dynamic-module-registry.ts","../lib/supported-modules.ts","../lib/import-urls.ts","../lib/create-importer.ts","../lib/dynamic-import-resolver.ts","../lib/index.ts"],"names":["importer"],"mappings":";AAYO,IAAM,2BAA2B,MAA6B;AACnE,EAAA,IAAI,WAAW,uBAAA,EAAyB;AACtC,IAAA,OAAO,UAAA,CAAW,uBAAA;AAAA,EACpB;AAEA,EAAA,MAAM,WAAkC,EAAC;AACzC,EAAA,UAAA,CAAW,uBAAA,GAA0B,QAAA;AACrC,EAAA,OAAO,QAAA;AACT;AAUO,SAAS,qBAAA,CACd,MACA,GAAA,EACS;AACT,EAAA,wBAAA,EAAyB,CAAE,IAAI,CAAA,GAAI,GAAA;AACnC,EAAA,OAAO,GAAA;AACT;;;AChBO,IAAM,gBAAA,GAAmB;AAAA,EAC9B,wBAAA;AAAA,EACA,yBAAA;AAAA,EACA,qBAAA;AAAA,EACA,kCAAA;AAAA,EACA,mCAAA;AAAA,EACA,wBAAA;AAAA,EACA,sBAAA;AAAA,EACA,uBAAA;AAAA,EACA,sBAAA;AAAA,EACA,yBAAA;AAAA,EACA,kCAAA;AAAA,EACA,2BAAA;AAAA,EACA,uBAAA;AAAA,EACA,sBAAA;AAAA,EACA,2BAAA;AAAA,EACA,mBAAA;AAAA,EACA,gBAAA;AAAA,EACA,uBAAA;AAAA,EACA;AACF;AAsCO,IAAM,aAAA,GAAgB,CAAC,SAAA,KAA8B;AAE1D,EAAA,MAAM,wBAAwB,SAAA,CAAU,OAAA;AAAA,IACtC,GAAA;AAAA,IACA,SAAA,CAAU,UAAA,CAAW,GAAG,CAAA,GAAI,CAAA,GAAI;AAAA,GAClC;AACA,EAAA,OAAO,0BAA0B,EAAA,GAC7B,SAAA,GACA,SAAA,CAAU,KAAA,CAAM,GAAG,qBAAqB,CAAA;AAC9C;;;ACrFA,IAAM,kBAAA,GAAqB,IAAI,GAAA,CAAY,gBAAgB,CAAA;AAC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,WAAW,IAAI,CAAA;AAAA,IAC7B;AACA,IAAA,OAAO,OAAA,CAAQ,IAAI,SAAS,CAAA;AAAA,EAC9B;AACA,EAAA,OAAOA,SAAAA;AACT;;;ACvBA,IAAM,aAAa,kCAAA,EAAmC;AAMtD,eAAe,SAAS,SAAA,EAAqC;AAC3D,EAAA,MAAM,WAAW,wBAAA,EAAyB;AAC1C,EAAA,OAAO,WAAW,QAAA,CAAS,SAAA,EAAW,UAAU,CAAA,GAAI,WAAW,SAAS,CAAA;AAC1E;AAEA,IAAO,aAAA,GAAQ","file":"index.js","sourcesContent":["import type {\n SupportedModuleMap,\n SupportedModuleName,\n} from \"./supported-modules\"\n\nexport type DynamicModuleRegistry = Partial<SupportedModuleMap> &\n Record<string, unknown>\n\ndeclare global {\n var tscircuitDynamicModules: DynamicModuleRegistry | undefined\n}\n\nexport const getDynamicModuleRegistry = (): DynamicModuleRegistry => {\n if (globalThis.tscircuitDynamicModules) {\n return globalThis.tscircuitDynamicModules\n }\n\n const registry: DynamicModuleRegistry = {}\n globalThis.tscircuitDynamicModules = registry\n return registry\n}\n\nexport function registerDynamicModule<TSpecifier extends SupportedModuleName>(\n name: TSpecifier,\n mod: SupportedModuleMap[TSpecifier],\n): SupportedModuleMap[TSpecifier]\nexport function registerDynamicModule<TModule>(\n name: string,\n mod: TModule,\n): TModule\nexport function registerDynamicModule<TModule>(\n name: string,\n mod: TModule,\n): TModule {\n getDynamicModuleRegistry()[name] = mod\n return mod\n}\n","import type * as CircuitJsonTo3dPngModule from \"./type-bundles/circuit-json-to-3d-png\"\nimport type * as CircuitJsonToBomCsvModule from \"./type-bundles/circuit-json-to-bom-csv\"\nimport type * as CircuitJsonToBpcModule from \"./type-bundles/circuit-json-to-bpc\"\nimport type * as CircuitJsonToConnectivityMapModule from \"./type-bundles/circuit-json-to-connectivity-map\"\nimport type * as CircuitJsonToFdmComponentBoxModule from \"./type-bundles/circuit-json-to-fdm-component-box\"\nimport type * as CircuitJsonToGerberModule from \"./type-bundles/circuit-json-to-gerber\"\nimport type * as CircuitJsonToGltfModule from \"./type-bundles/circuit-json-to-gltf\"\nimport type * as CircuitJsonToKicadModule from \"./type-bundles/circuit-json-to-kicad\"\nimport type * as CircuitJsonToLbrnModule from \"./type-bundles/circuit-json-to-lbrn\"\nimport type * as CircuitJsonToPnpCsvModule from \"./type-bundles/circuit-json-to-pnp-csv\"\nimport type * as CircuitJsonToReadableNetlistModule from \"./type-bundles/circuit-json-to-readable-netlist\"\nimport type * as CircuitJsonToSimple3dModule from \"./type-bundles/circuit-json-to-simple-3d\"\nimport type * as CircuitJsonToSpiceModule from \"./type-bundles/circuit-json-to-spice\"\nimport type * as CircuitJsonToStepModule from \"./type-bundles/circuit-json-to-step\"\nimport type * as CircuitJsonToTscircuitModule from \"./type-bundles/circuit-json-to-tscircuit\"\nimport type * as CircuitToCanvasModule from \"./type-bundles/circuit-to-canvas\"\nimport type * as CircuitToSvgModule from \"./type-bundles/circuit-to-svg\"\nimport type * as KicadToCircuitJsonModule from \"./type-bundles/kicad-to-circuit-json\"\nimport type * as SchematicPlacementAnalysisModule from \"./type-bundles/@tscircuit/circuit-json-schematic-placement-analysis\"\n\nexport const supportedModules = [\n \"circuit-json-to-3d-png\",\n \"circuit-json-to-bom-csv\",\n \"circuit-json-to-bpc\",\n \"circuit-json-to-connectivity-map\",\n \"circuit-json-to-fdm-component-box\",\n \"circuit-json-to-gerber\",\n \"circuit-json-to-gltf\",\n \"circuit-json-to-kicad\",\n \"circuit-json-to-lbrn\",\n \"circuit-json-to-pnp-csv\",\n \"circuit-json-to-readable-netlist\",\n \"circuit-json-to-simple-3d\",\n \"circuit-json-to-spice\",\n \"circuit-json-to-step\",\n \"circuit-json-to-tscircuit\",\n \"circuit-to-canvas\",\n \"circuit-to-svg\",\n \"kicad-to-circuit-json\",\n \"@tscircuit/circuit-json-schematic-placement-analysis\",\n] as const\n\nexport type SupportedModuleName = (typeof supportedModules)[number]\nexport type SupportedModuleSpecifier =\n | SupportedModuleName\n | `${SupportedModuleName}@${string}`\n\nexport interface SupportedModuleMap {\n \"circuit-json-to-3d-png\": typeof CircuitJsonTo3dPngModule\n \"circuit-json-to-bom-csv\": typeof CircuitJsonToBomCsvModule\n \"circuit-json-to-bpc\": typeof CircuitJsonToBpcModule\n \"circuit-json-to-connectivity-map\": typeof CircuitJsonToConnectivityMapModule\n \"circuit-json-to-fdm-component-box\": typeof CircuitJsonToFdmComponentBoxModule\n \"circuit-json-to-gerber\": typeof CircuitJsonToGerberModule\n \"circuit-json-to-gltf\": typeof CircuitJsonToGltfModule\n \"circuit-json-to-kicad\": typeof CircuitJsonToKicadModule\n \"circuit-json-to-lbrn\": typeof CircuitJsonToLbrnModule\n \"circuit-json-to-pnp-csv\": typeof CircuitJsonToPnpCsvModule\n \"circuit-json-to-readable-netlist\": typeof CircuitJsonToReadableNetlistModule\n \"circuit-json-to-simple-3d\": typeof CircuitJsonToSimple3dModule\n \"circuit-json-to-spice\": typeof CircuitJsonToSpiceModule\n \"circuit-json-to-step\": typeof CircuitJsonToStepModule\n \"circuit-json-to-tscircuit\": typeof CircuitJsonToTscircuitModule\n \"circuit-to-canvas\": typeof CircuitToCanvasModule\n \"circuit-to-svg\": typeof CircuitToSvgModule\n \"kicad-to-circuit-json\": typeof KicadToCircuitJsonModule\n \"@tscircuit/circuit-json-schematic-placement-analysis\": typeof SchematicPlacementAnalysisModule\n}\n\nexport type StripVersion<TSpecifier extends string> =\n TSpecifier extends SupportedModuleName\n ? TSpecifier\n : {\n [TName in SupportedModuleName]: TSpecifier extends `${TName}@${string}`\n ? TName\n : never\n }[SupportedModuleName]\n\nexport const getModuleName = (specifier: string): string => {\n // A scoped name's leading @ is part of the name, not a version separator.\n const versionSeparatorIndex = specifier.indexOf(\n \"@\",\n specifier.startsWith(\"@\") ? 1 : 0,\n )\n return versionSeparatorIndex === -1\n ? specifier\n : specifier.slice(0, versionSeparatorIndex)\n}\n","import { getModuleName, supportedModules } from \"./supported-modules\"\n\nconst supportedModuleSet = new Set<string>(supportedModules)\nconst browserEntrypoints = new Map<string, string>([\n [\"@tscircuit/circuit-json-schematic-placement-analysis\", \"dist/browser.js\"],\n])\n\nfunction assertSupportedModule(specifier: string) {\n if (!supportedModuleSet.has(getModuleName(specifier))) {\n throw new Error(`Unsupported module: ${specifier}`)\n }\n}\n\nexport const getImportUrl = (specifier: string): string => {\n assertSupportedModule(specifier)\n const moduleName = getModuleName(specifier)\n const version =\n specifier.length === moduleName.length\n ? \"latest\"\n : specifier.slice(moduleName.length + 1)\n const entrypoint = browserEntrypoints.get(moduleName) ?? \"+esm\"\n return `https://jscdn.tscircuit.com/${moduleName}/${version}/${entrypoint}`\n}\n\nexport const getImportUrls = (specifier: string): string[] => {\n return [getImportUrl(specifier), `https://esm.run/${specifier}`]\n}\n","import { getDynamicModuleRegistry } from \"./dynamic-module-registry\"\nimport { getImportUrls } from \"./import-urls\"\n\n/** Internal dependency seam lets tests observe CDN attempts without a network. */\nexport function createDefaultDynamicImportResolver(\n importModule: (url: string) => Promise<unknown> = (url) =>\n import(/* @vite-ignore */ /* webpackIgnore: true */ url),\n) {\n return async (specifier: string): Promise<unknown> => {\n const modules = getDynamicModuleRegistry()\n if (Object.hasOwn(modules, specifier)) return modules[specifier]\n\n let lastError: unknown\n for (const importUrl of getImportUrls(specifier)) {\n try {\n const mod = await importModule(importUrl)\n // A versioned module never silently satisfies a different version or\n // an unversioned request for latest.\n modules[specifier] = mod\n return mod\n } catch (error) {\n lastError = error\n }\n }\n throw lastError\n }\n}\n","export type DynamicModuleResolver = (\n specifier: string,\n) => unknown | PromiseLike<unknown>\n\n/** A custom resolver can delegate to the default registry/CDN resolver. */\nexport type DynamicImportResolver = (\n specifier: string,\n defaultResolver: DynamicModuleResolver,\n) => unknown | PromiseLike<unknown>\n\ndeclare global {\n var tscircuitDynamicImportResolver: DynamicImportResolver | undefined\n}\n\nexport const getDynamicImportResolver = () =>\n globalThis.tscircuitDynamicImportResolver\n\n/** Configure before requesting modules, separately in each JavaScript realm. */\nexport function setDynamicImportResolver(\n resolver: DynamicImportResolver | undefined,\n): void {\n globalThis.tscircuitDynamicImportResolver = resolver\n}\n\n/**\n * Create an importer from exact specifier keys and lazy module loaders.\n * Missing keys reject unless a fallback is supplied; loader failures retry\n * through the same loader on the next request.\n */\nexport function createDynamicImporter<\n TLoaders extends Record<string, () => unknown | PromiseLike<unknown>>,\n>(loaders: TLoaders, fallback?: DynamicModuleResolver) {\n const entries = new Map(Object.entries(loaders))\n const pending = new Map<string, Promise<unknown>>()\n\n function importer<TSpecifier extends keyof TLoaders & string>(\n specifier: TSpecifier,\n ): Promise<Awaited<ReturnType<TLoaders[TSpecifier]>>>\n function importer(specifier: string): Promise<unknown>\n async function importer(specifier: string): Promise<unknown> {\n const loader = entries.get(specifier)\n if (!loader) {\n if (fallback) return fallback(specifier)\n throw new Error(`No bundled module registered for: ${specifier}`)\n }\n if (!pending.has(specifier)) {\n const load = Promise.resolve()\n .then(loader)\n .catch((error) => {\n pending.delete(specifier)\n throw error\n })\n pending.set(specifier, load)\n }\n return pending.get(specifier)!\n }\n return importer\n}\n","import { createDefaultDynamicImportResolver } from \"./create-importer\"\nimport { getDynamicImportResolver } from \"./dynamic-import-resolver\"\nimport {\n type StripVersion,\n type SupportedModuleMap,\n type SupportedModuleSpecifier,\n} from \"./supported-modules\"\n\nexport {\n getDynamicModuleRegistry,\n registerDynamicModule,\n} from \"./dynamic-module-registry\"\nexport type { DynamicModuleRegistry } from \"./dynamic-module-registry\"\nexport {\n setDynamicImportResolver,\n createDynamicImporter,\n} from \"./dynamic-import-resolver\"\nexport type {\n DynamicImportResolver,\n DynamicModuleResolver,\n} from \"./dynamic-import-resolver\"\nexport {\n getModuleName,\n supportedModules,\n} from \"./supported-modules\"\nexport type {\n StripVersion,\n SupportedModuleMap,\n SupportedModuleName,\n SupportedModuleSpecifier,\n} from \"./supported-modules\"\n\nexport { getImportUrl, getImportUrls } from \"./import-urls\"\n\nconst loadModule = createDefaultDynamicImportResolver()\n\nasync function importer<TSpecifier extends SupportedModuleSpecifier>(\n specifier: TSpecifier,\n): Promise<SupportedModuleMap[StripVersion<TSpecifier>]>\nasync function importer(specifier: string): Promise<unknown>\nasync function importer(specifier: string): Promise<unknown> {\n const resolver = getDynamicImportResolver()\n return resolver ? resolver(specifier, loadModule) : loadModule(specifier)\n}\n\nexport default importer\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getDynamicModuleRegistry } from "./dynamic-module-registry"
|
|
2
|
+
import { getImportUrls } from "./import-urls"
|
|
3
|
+
|
|
4
|
+
/** Internal dependency seam lets tests observe CDN attempts without a network. */
|
|
5
|
+
export function createDefaultDynamicImportResolver(
|
|
6
|
+
importModule: (url: string) => Promise<unknown> = (url) =>
|
|
7
|
+
import(/* @vite-ignore */ /* webpackIgnore: true */ url),
|
|
8
|
+
) {
|
|
9
|
+
return async (specifier: string): Promise<unknown> => {
|
|
10
|
+
const modules = getDynamicModuleRegistry()
|
|
11
|
+
if (Object.hasOwn(modules, specifier)) return modules[specifier]
|
|
12
|
+
|
|
13
|
+
let lastError: unknown
|
|
14
|
+
for (const importUrl of getImportUrls(specifier)) {
|
|
15
|
+
try {
|
|
16
|
+
const mod = await importModule(importUrl)
|
|
17
|
+
// A versioned module never silently satisfies a different version or
|
|
18
|
+
// an unversioned request for latest.
|
|
19
|
+
modules[specifier] = mod
|
|
20
|
+
return mod
|
|
21
|
+
} catch (error) {
|
|
22
|
+
lastError = error
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw lastError
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export type DynamicModuleResolver = (
|
|
2
|
+
specifier: string,
|
|
3
|
+
) => unknown | PromiseLike<unknown>
|
|
4
|
+
|
|
5
|
+
/** A custom resolver can delegate to the default registry/CDN resolver. */
|
|
6
|
+
export type DynamicImportResolver = (
|
|
7
|
+
specifier: string,
|
|
8
|
+
defaultResolver: DynamicModuleResolver,
|
|
9
|
+
) => unknown | PromiseLike<unknown>
|
|
10
|
+
|
|
11
|
+
declare global {
|
|
12
|
+
var tscircuitDynamicImportResolver: DynamicImportResolver | undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const getDynamicImportResolver = () =>
|
|
16
|
+
globalThis.tscircuitDynamicImportResolver
|
|
17
|
+
|
|
18
|
+
/** Configure before requesting modules, separately in each JavaScript realm. */
|
|
19
|
+
export function setDynamicImportResolver(
|
|
20
|
+
resolver: DynamicImportResolver | undefined,
|
|
21
|
+
): void {
|
|
22
|
+
globalThis.tscircuitDynamicImportResolver = resolver
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create an importer from exact specifier keys and lazy module loaders.
|
|
27
|
+
* Missing keys reject unless a fallback is supplied; loader failures retry
|
|
28
|
+
* through the same loader on the next request.
|
|
29
|
+
*/
|
|
30
|
+
export function createDynamicImporter<
|
|
31
|
+
TLoaders extends Record<string, () => unknown | PromiseLike<unknown>>,
|
|
32
|
+
>(loaders: TLoaders, fallback?: DynamicModuleResolver) {
|
|
33
|
+
const entries = new Map(Object.entries(loaders))
|
|
34
|
+
const pending = new Map<string, Promise<unknown>>()
|
|
35
|
+
|
|
36
|
+
function importer<TSpecifier extends keyof TLoaders & string>(
|
|
37
|
+
specifier: TSpecifier,
|
|
38
|
+
): Promise<Awaited<ReturnType<TLoaders[TSpecifier]>>>
|
|
39
|
+
function importer(specifier: string): Promise<unknown>
|
|
40
|
+
async function importer(specifier: string): Promise<unknown> {
|
|
41
|
+
const loader = entries.get(specifier)
|
|
42
|
+
if (!loader) {
|
|
43
|
+
if (fallback) return fallback(specifier)
|
|
44
|
+
throw new Error(`No bundled module registered for: ${specifier}`)
|
|
45
|
+
}
|
|
46
|
+
if (!pending.has(specifier)) {
|
|
47
|
+
const load = Promise.resolve()
|
|
48
|
+
.then(loader)
|
|
49
|
+
.catch((error) => {
|
|
50
|
+
pending.delete(specifier)
|
|
51
|
+
throw error
|
|
52
|
+
})
|
|
53
|
+
pending.set(specifier, load)
|
|
54
|
+
}
|
|
55
|
+
return pending.get(specifier)!
|
|
56
|
+
}
|
|
57
|
+
return importer
|
|
58
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SupportedModuleMap,
|
|
3
|
+
SupportedModuleName,
|
|
4
|
+
} from "./supported-modules"
|
|
5
|
+
|
|
6
|
+
export type DynamicModuleRegistry = Partial<SupportedModuleMap> &
|
|
7
|
+
Record<string, unknown>
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
var tscircuitDynamicModules: DynamicModuleRegistry | undefined
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const getDynamicModuleRegistry = (): DynamicModuleRegistry => {
|
|
14
|
+
if (globalThis.tscircuitDynamicModules) {
|
|
15
|
+
return globalThis.tscircuitDynamicModules
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const registry: DynamicModuleRegistry = {}
|
|
19
|
+
globalThis.tscircuitDynamicModules = registry
|
|
20
|
+
return registry
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function registerDynamicModule<TSpecifier extends SupportedModuleName>(
|
|
24
|
+
name: TSpecifier,
|
|
25
|
+
mod: SupportedModuleMap[TSpecifier],
|
|
26
|
+
): SupportedModuleMap[TSpecifier]
|
|
27
|
+
export function registerDynamicModule<TModule>(
|
|
28
|
+
name: string,
|
|
29
|
+
mod: TModule,
|
|
30
|
+
): TModule
|
|
31
|
+
export function registerDynamicModule<TModule>(
|
|
32
|
+
name: string,
|
|
33
|
+
mod: TModule,
|
|
34
|
+
): TModule {
|
|
35
|
+
getDynamicModuleRegistry()[name] = mod
|
|
36
|
+
return mod
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getModuleName, supportedModules } from "./supported-modules"
|
|
2
|
+
|
|
3
|
+
const supportedModuleSet = new Set<string>(supportedModules)
|
|
4
|
+
const browserEntrypoints = new Map<string, string>([
|
|
5
|
+
["@tscircuit/circuit-json-schematic-placement-analysis", "dist/browser.js"],
|
|
6
|
+
])
|
|
7
|
+
|
|
8
|
+
function assertSupportedModule(specifier: string) {
|
|
9
|
+
if (!supportedModuleSet.has(getModuleName(specifier))) {
|
|
10
|
+
throw new Error(`Unsupported module: ${specifier}`)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const getImportUrl = (specifier: string): string => {
|
|
15
|
+
assertSupportedModule(specifier)
|
|
16
|
+
const moduleName = getModuleName(specifier)
|
|
17
|
+
const version =
|
|
18
|
+
specifier.length === moduleName.length
|
|
19
|
+
? "latest"
|
|
20
|
+
: specifier.slice(moduleName.length + 1)
|
|
21
|
+
const entrypoint = browserEntrypoints.get(moduleName) ?? "+esm"
|
|
22
|
+
return `https://jscdn.tscircuit.com/${moduleName}/${version}/${entrypoint}`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const getImportUrls = (specifier: string): string[] => {
|
|
26
|
+
return [getImportUrl(specifier), `https://esm.run/${specifier}`]
|
|
27
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createDefaultDynamicImportResolver } from "./create-importer"
|
|
2
|
+
import { getDynamicImportResolver } from "./dynamic-import-resolver"
|
|
3
|
+
import {
|
|
4
|
+
type StripVersion,
|
|
5
|
+
type SupportedModuleMap,
|
|
6
|
+
type SupportedModuleSpecifier,
|
|
7
|
+
} from "./supported-modules"
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
getDynamicModuleRegistry,
|
|
11
|
+
registerDynamicModule,
|
|
12
|
+
} from "./dynamic-module-registry"
|
|
13
|
+
export type { DynamicModuleRegistry } from "./dynamic-module-registry"
|
|
14
|
+
export {
|
|
15
|
+
setDynamicImportResolver,
|
|
16
|
+
createDynamicImporter,
|
|
17
|
+
} from "./dynamic-import-resolver"
|
|
18
|
+
export type {
|
|
19
|
+
DynamicImportResolver,
|
|
20
|
+
DynamicModuleResolver,
|
|
21
|
+
} from "./dynamic-import-resolver"
|
|
22
|
+
export {
|
|
23
|
+
getModuleName,
|
|
24
|
+
supportedModules,
|
|
25
|
+
} from "./supported-modules"
|
|
26
|
+
export type {
|
|
27
|
+
StripVersion,
|
|
28
|
+
SupportedModuleMap,
|
|
29
|
+
SupportedModuleName,
|
|
30
|
+
SupportedModuleSpecifier,
|
|
31
|
+
} from "./supported-modules"
|
|
32
|
+
|
|
33
|
+
export { getImportUrl, getImportUrls } from "./import-urls"
|
|
34
|
+
|
|
35
|
+
const loadModule = createDefaultDynamicImportResolver()
|
|
36
|
+
|
|
37
|
+
async function importer<TSpecifier extends SupportedModuleSpecifier>(
|
|
38
|
+
specifier: TSpecifier,
|
|
39
|
+
): Promise<SupportedModuleMap[StripVersion<TSpecifier>]>
|
|
40
|
+
async function importer(specifier: string): Promise<unknown>
|
|
41
|
+
async function importer(specifier: string): Promise<unknown> {
|
|
42
|
+
const resolver = getDynamicImportResolver()
|
|
43
|
+
return resolver ? resolver(specifier, loadModule) : loadModule(specifier)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default importer
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type * as CircuitJsonTo3dPngModule from "./type-bundles/circuit-json-to-3d-png"
|
|
2
|
+
import type * as CircuitJsonToBomCsvModule from "./type-bundles/circuit-json-to-bom-csv"
|
|
3
|
+
import type * as CircuitJsonToBpcModule from "./type-bundles/circuit-json-to-bpc"
|
|
4
|
+
import type * as CircuitJsonToConnectivityMapModule from "./type-bundles/circuit-json-to-connectivity-map"
|
|
5
|
+
import type * as CircuitJsonToFdmComponentBoxModule from "./type-bundles/circuit-json-to-fdm-component-box"
|
|
6
|
+
import type * as CircuitJsonToGerberModule from "./type-bundles/circuit-json-to-gerber"
|
|
7
|
+
import type * as CircuitJsonToGltfModule from "./type-bundles/circuit-json-to-gltf"
|
|
8
|
+
import type * as CircuitJsonToKicadModule from "./type-bundles/circuit-json-to-kicad"
|
|
9
|
+
import type * as CircuitJsonToLbrnModule from "./type-bundles/circuit-json-to-lbrn"
|
|
10
|
+
import type * as CircuitJsonToPnpCsvModule from "./type-bundles/circuit-json-to-pnp-csv"
|
|
11
|
+
import type * as CircuitJsonToReadableNetlistModule from "./type-bundles/circuit-json-to-readable-netlist"
|
|
12
|
+
import type * as CircuitJsonToSimple3dModule from "./type-bundles/circuit-json-to-simple-3d"
|
|
13
|
+
import type * as CircuitJsonToSpiceModule from "./type-bundles/circuit-json-to-spice"
|
|
14
|
+
import type * as CircuitJsonToStepModule from "./type-bundles/circuit-json-to-step"
|
|
15
|
+
import type * as CircuitJsonToTscircuitModule from "./type-bundles/circuit-json-to-tscircuit"
|
|
16
|
+
import type * as CircuitToCanvasModule from "./type-bundles/circuit-to-canvas"
|
|
17
|
+
import type * as CircuitToSvgModule from "./type-bundles/circuit-to-svg"
|
|
18
|
+
import type * as KicadToCircuitJsonModule from "./type-bundles/kicad-to-circuit-json"
|
|
19
|
+
import type * as SchematicPlacementAnalysisModule from "./type-bundles/@tscircuit/circuit-json-schematic-placement-analysis"
|
|
20
|
+
|
|
21
|
+
export const supportedModules = [
|
|
22
|
+
"circuit-json-to-3d-png",
|
|
23
|
+
"circuit-json-to-bom-csv",
|
|
24
|
+
"circuit-json-to-bpc",
|
|
25
|
+
"circuit-json-to-connectivity-map",
|
|
26
|
+
"circuit-json-to-fdm-component-box",
|
|
27
|
+
"circuit-json-to-gerber",
|
|
28
|
+
"circuit-json-to-gltf",
|
|
29
|
+
"circuit-json-to-kicad",
|
|
30
|
+
"circuit-json-to-lbrn",
|
|
31
|
+
"circuit-json-to-pnp-csv",
|
|
32
|
+
"circuit-json-to-readable-netlist",
|
|
33
|
+
"circuit-json-to-simple-3d",
|
|
34
|
+
"circuit-json-to-spice",
|
|
35
|
+
"circuit-json-to-step",
|
|
36
|
+
"circuit-json-to-tscircuit",
|
|
37
|
+
"circuit-to-canvas",
|
|
38
|
+
"circuit-to-svg",
|
|
39
|
+
"kicad-to-circuit-json",
|
|
40
|
+
"@tscircuit/circuit-json-schematic-placement-analysis",
|
|
41
|
+
] as const
|
|
42
|
+
|
|
43
|
+
export type SupportedModuleName = (typeof supportedModules)[number]
|
|
44
|
+
export type SupportedModuleSpecifier =
|
|
45
|
+
| SupportedModuleName
|
|
46
|
+
| `${SupportedModuleName}@${string}`
|
|
47
|
+
|
|
48
|
+
export interface SupportedModuleMap {
|
|
49
|
+
"circuit-json-to-3d-png": typeof CircuitJsonTo3dPngModule
|
|
50
|
+
"circuit-json-to-bom-csv": typeof CircuitJsonToBomCsvModule
|
|
51
|
+
"circuit-json-to-bpc": typeof CircuitJsonToBpcModule
|
|
52
|
+
"circuit-json-to-connectivity-map": typeof CircuitJsonToConnectivityMapModule
|
|
53
|
+
"circuit-json-to-fdm-component-box": typeof CircuitJsonToFdmComponentBoxModule
|
|
54
|
+
"circuit-json-to-gerber": typeof CircuitJsonToGerberModule
|
|
55
|
+
"circuit-json-to-gltf": typeof CircuitJsonToGltfModule
|
|
56
|
+
"circuit-json-to-kicad": typeof CircuitJsonToKicadModule
|
|
57
|
+
"circuit-json-to-lbrn": typeof CircuitJsonToLbrnModule
|
|
58
|
+
"circuit-json-to-pnp-csv": typeof CircuitJsonToPnpCsvModule
|
|
59
|
+
"circuit-json-to-readable-netlist": typeof CircuitJsonToReadableNetlistModule
|
|
60
|
+
"circuit-json-to-simple-3d": typeof CircuitJsonToSimple3dModule
|
|
61
|
+
"circuit-json-to-spice": typeof CircuitJsonToSpiceModule
|
|
62
|
+
"circuit-json-to-step": typeof CircuitJsonToStepModule
|
|
63
|
+
"circuit-json-to-tscircuit": typeof CircuitJsonToTscircuitModule
|
|
64
|
+
"circuit-to-canvas": typeof CircuitToCanvasModule
|
|
65
|
+
"circuit-to-svg": typeof CircuitToSvgModule
|
|
66
|
+
"kicad-to-circuit-json": typeof KicadToCircuitJsonModule
|
|
67
|
+
"@tscircuit/circuit-json-schematic-placement-analysis": typeof SchematicPlacementAnalysisModule
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type StripVersion<TSpecifier extends string> =
|
|
71
|
+
TSpecifier extends SupportedModuleName
|
|
72
|
+
? TSpecifier
|
|
73
|
+
: {
|
|
74
|
+
[TName in SupportedModuleName]: TSpecifier extends `${TName}@${string}`
|
|
75
|
+
? TName
|
|
76
|
+
: never
|
|
77
|
+
}[SupportedModuleName]
|
|
78
|
+
|
|
79
|
+
export const getModuleName = (specifier: string): string => {
|
|
80
|
+
// A scoped name's leading @ is part of the name, not a version separator.
|
|
81
|
+
const versionSeparatorIndex = specifier.indexOf(
|
|
82
|
+
"@",
|
|
83
|
+
specifier.startsWith("@") ? 1 : 0,
|
|
84
|
+
)
|
|
85
|
+
return versionSeparatorIndex === -1
|
|
86
|
+
? specifier
|
|
87
|
+
: specifier.slice(0, versionSeparatorIndex)
|
|
88
|
+
}
|