@vizhub/runtime 0.2.0 → 0.3.1

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.
Files changed (285) hide show
  1. package/README.md +280 -128
  2. package/dist/buildHTML.d.ts +14 -0
  3. package/dist/buildHTML.js +67 -0
  4. package/dist/cjs/buildHTML.js +71 -0
  5. package/dist/cjs/common/packageJson.js +81 -0
  6. package/dist/cjs/common/packageJson.test.js +148 -0
  7. package/dist/cjs/common/sucrasePlugin.js +27 -0
  8. package/dist/cjs/common/virtualFileSystem.js +79 -0
  9. package/dist/cjs/determineRuntimeVersion.js +26 -0
  10. package/dist/cjs/determineRuntimeVersion.test.js +103 -0
  11. package/dist/cjs/index.js +9 -0
  12. package/dist/cjs/package.json +1 -0
  13. package/dist/cjs/test/fixtures/v1/basicHTML.js +18 -0
  14. package/dist/cjs/test/fixtures/v1/fetchProxy.js +15 -0
  15. package/dist/cjs/test/fixtures/v1/index.js +15 -0
  16. package/dist/cjs/test/fixtures/v1/jsScriptTag.js +12 -0
  17. package/dist/cjs/test/fixtures/v1/protocolTest.js +18 -0
  18. package/dist/cjs/test/fixtures/v1/styleTest.js +18 -0
  19. package/dist/cjs/test/fixtures/v1/xmlTest.js +22 -0
  20. package/dist/cjs/test/fixtures/v2/basicBundle.js +8 -0
  21. package/dist/cjs/test/fixtures/v2/basicBundleNoExtension.js +8 -0
  22. package/dist/cjs/test/fixtures/v2/d3Import.js +10 -0
  23. package/dist/cjs/test/fixtures/v2/d3ImportPkg.js +22 -0
  24. package/dist/cjs/test/fixtures/v2/d3RosettaImportPkg.js +22 -0
  25. package/dist/cjs/test/fixtures/v2/es6Preserve.js +9 -0
  26. package/dist/cjs/test/fixtures/v2/generatorSupport.js +9 -0
  27. package/dist/cjs/test/fixtures/v2/index.js +45 -0
  28. package/dist/cjs/test/fixtures/v2/jsxTranspile.js +9 -0
  29. package/dist/cjs/test/fixtures/v2/reactDomImport.js +11 -0
  30. package/dist/cjs/test/fixtures/v2/reactDomImportPkg.js +27 -0
  31. package/dist/cjs/test/fixtures/v2/reactImport.js +10 -0
  32. package/dist/cjs/test/fixtures/v2/reactImportPkg.js +20 -0
  33. package/dist/cjs/test/fixtures/v2/sourceMapErrorFixture.js +22 -0
  34. package/dist/cjs/test/fixtures/v2/unicodeSupport.js +9 -0
  35. package/dist/cjs/test/fixtures/v3/basicIndexJS.js +10 -0
  36. package/dist/cjs/test/fixtures/v3/cssImport.js +12 -0
  37. package/dist/cjs/test/fixtures/v3/csvImport.js +14 -0
  38. package/dist/cjs/test/fixtures/v3/csvStrangeChars.js +15 -0
  39. package/dist/cjs/test/fixtures/v3/index.js +25 -0
  40. package/dist/cjs/test/fixtures/v3/jsExport.js +13 -0
  41. package/dist/cjs/test/fixtures/v3/sourcemap.js +13 -0
  42. package/dist/cjs/test/fixtures/v3/svelte.js +21 -0
  43. package/dist/cjs/test/fixtures/v3/vizContent.js +14 -0
  44. package/dist/cjs/test/fixtures/v3/vizImport.js +88 -0
  45. package/dist/cjs/test/fixtures/v4/d3Usage.js +34 -0
  46. package/dist/cjs/test/fixtures/v4/esmBuild.js +21 -0
  47. package/dist/cjs/test/fixtures/v4/fetchInterception.js +21 -0
  48. package/dist/cjs/test/fixtures/v4/index.js +21 -0
  49. package/dist/cjs/test/fixtures/v4/jsScriptTagTypeModule.js +21 -0
  50. package/dist/cjs/test/fixtures/v4/jsScriptTagTypeModules.js +15 -0
  51. package/dist/cjs/test/fixtures/v4/reactHooks.js +50 -0
  52. package/dist/cjs/test/fixtures/v4/reactJsx.js +37 -0
  53. package/dist/cjs/test/fixtures/v4/threeJsUsage.js +27 -0
  54. package/dist/cjs/test/fixtures/v4/typeScriptSupport.js +32 -0
  55. package/dist/cjs/test/testInBrowser.js +50 -0
  56. package/dist/cjs/test/testStackTrace.js +58 -0
  57. package/dist/cjs/test/v1.test.js +70 -0
  58. package/dist/cjs/test/v2.test.js +124 -0
  59. package/dist/cjs/test/v3.test.js +119 -0
  60. package/dist/cjs/test/v4.test.js +105 -0
  61. package/dist/cjs/types.js +2 -0
  62. package/dist/cjs/utils/vizContentToFileCollection.js +23 -0
  63. package/dist/cjs/utils/vizContentToFileCollection.test.js +77 -0
  64. package/dist/cjs/v2/computeBundleJSV2.js +33 -0
  65. package/dist/cjs/v2/getComputedIndexHtml.js +86 -0
  66. package/dist/cjs/v2/getComputedIndexHtml.test.js +136 -0
  67. package/dist/cjs/v2/index.js +5 -0
  68. package/dist/cjs/v2/v2Build.js +15 -0
  69. package/dist/cjs/v3/cleanRollupErrorMessage.js +13 -0
  70. package/dist/cjs/v3/computeBundleJSV3.js +52 -0
  71. package/dist/cjs/v3/createVizContent.js +27 -0
  72. package/dist/cjs/v3/extractVizImport.js +27 -0
  73. package/dist/cjs/v3/extractVizImport.test.js +32 -0
  74. package/dist/cjs/v3/htmlTemplate.js +93 -0
  75. package/dist/cjs/v3/index.js +5 -0
  76. package/dist/cjs/v3/parseId.js +9 -0
  77. package/dist/cjs/v3/parseId.test.js +27 -0
  78. package/dist/cjs/v3/setupV3Runtime.js +345 -0
  79. package/dist/cjs/v3/slugCache.js +39 -0
  80. package/dist/cjs/v3/slugCache.test.js +107 -0
  81. package/dist/cjs/v3/transformDSV/dsvParseSrc.js +124 -0
  82. package/dist/cjs/v3/transformDSV/index.js +44 -0
  83. package/dist/cjs/v3/transformSvelte.js +81 -0
  84. package/dist/cjs/v3/types.js +2 -0
  85. package/dist/cjs/v3/v3Build.js +34 -0
  86. package/dist/cjs/v3/vizCache.js +40 -0
  87. package/dist/cjs/v3/vizCache.test.js +100 -0
  88. package/dist/cjs/v3/vizLoad.js +50 -0
  89. package/dist/cjs/v3/vizResolve.js +73 -0
  90. package/dist/cjs/v4/index.js +138 -0
  91. package/dist/common/packageJson.d.ts +1 -1
  92. package/dist/common/virtualFileSystem.d.ts +1 -1
  93. package/dist/determineRuntimeVersion.d.ts +2 -1
  94. package/dist/esm/buildHTML.js +67 -0
  95. package/dist/esm/common/packageJson.js +71 -0
  96. package/dist/esm/common/packageJson.test.js +146 -0
  97. package/dist/esm/common/sucrasePlugin.js +24 -0
  98. package/dist/esm/common/virtualFileSystem.js +75 -0
  99. package/dist/esm/determineRuntimeVersion.js +22 -0
  100. package/dist/esm/determineRuntimeVersion.test.js +101 -0
  101. package/dist/esm/index.js +3 -0
  102. package/dist/esm/test/fixtures/v1/basicHTML.js +15 -0
  103. package/dist/esm/test/fixtures/v1/fetchProxy.js +12 -0
  104. package/dist/esm/test/fixtures/v1/index.js +6 -0
  105. package/dist/esm/test/fixtures/v1/jsScriptTag.js +9 -0
  106. package/dist/esm/test/fixtures/v1/protocolTest.js +15 -0
  107. package/dist/esm/test/fixtures/v1/styleTest.js +15 -0
  108. package/dist/esm/test/fixtures/v1/xmlTest.js +19 -0
  109. package/dist/esm/test/fixtures/v2/basicBundle.js +5 -0
  110. package/dist/esm/test/fixtures/v2/basicBundleNoExtension.js +5 -0
  111. package/dist/esm/test/fixtures/v2/d3Import.js +7 -0
  112. package/dist/esm/test/fixtures/v2/d3ImportPkg.js +19 -0
  113. package/dist/esm/test/fixtures/v2/d3RosettaImportPkg.js +19 -0
  114. package/dist/esm/test/fixtures/v2/es6Preserve.js +6 -0
  115. package/dist/esm/test/fixtures/v2/generatorSupport.js +6 -0
  116. package/dist/esm/test/fixtures/v2/index.js +15 -0
  117. package/dist/esm/test/fixtures/v2/jsxTranspile.js +6 -0
  118. package/dist/esm/test/fixtures/v2/reactDomImport.js +8 -0
  119. package/dist/esm/test/fixtures/v2/reactDomImportPkg.js +24 -0
  120. package/dist/esm/test/fixtures/v2/reactImport.js +7 -0
  121. package/dist/esm/test/fixtures/v2/reactImportPkg.js +17 -0
  122. package/dist/esm/test/fixtures/v2/sourceMapErrorFixture.js +19 -0
  123. package/dist/esm/test/fixtures/v2/unicodeSupport.js +6 -0
  124. package/dist/esm/test/fixtures/v3/basicIndexJS.js +7 -0
  125. package/dist/esm/test/fixtures/v3/cssImport.js +9 -0
  126. package/dist/esm/test/fixtures/v3/csvImport.js +11 -0
  127. package/dist/esm/test/fixtures/v3/csvStrangeChars.js +12 -0
  128. package/dist/esm/test/fixtures/v3/index.js +9 -0
  129. package/dist/esm/test/fixtures/v3/jsExport.js +10 -0
  130. package/dist/esm/test/fixtures/v3/sourcemap.js +10 -0
  131. package/dist/esm/test/fixtures/v3/svelte.js +18 -0
  132. package/dist/esm/test/fixtures/v3/vizContent.js +11 -0
  133. package/dist/esm/test/fixtures/v3/vizImport.js +85 -0
  134. package/dist/esm/test/fixtures/v4/d3Usage.js +31 -0
  135. package/dist/esm/test/fixtures/v4/esmBuild.js +18 -0
  136. package/dist/esm/test/fixtures/v4/fetchInterception.js +18 -0
  137. package/dist/esm/test/fixtures/v4/index.js +9 -0
  138. package/dist/esm/test/fixtures/v4/jsScriptTagTypeModule.js +18 -0
  139. package/dist/esm/test/fixtures/v4/jsScriptTagTypeModules.js +12 -0
  140. package/dist/esm/test/fixtures/v4/reactHooks.js +47 -0
  141. package/dist/esm/test/fixtures/v4/reactJsx.js +34 -0
  142. package/dist/esm/test/fixtures/v4/threeJsUsage.js +24 -0
  143. package/dist/esm/test/fixtures/v4/typeScriptSupport.js +29 -0
  144. package/dist/esm/test/testInBrowser.js +47 -0
  145. package/dist/esm/test/testStackTrace.js +55 -0
  146. package/dist/esm/test/v1.test.js +65 -0
  147. package/dist/esm/test/v2.test.js +121 -0
  148. package/dist/esm/test/v3.test.js +114 -0
  149. package/dist/esm/test/v4.test.js +100 -0
  150. package/dist/esm/types.js +1 -0
  151. package/dist/esm/utils/vizContentToFileCollection.js +19 -0
  152. package/dist/esm/utils/vizContentToFileCollection.test.js +75 -0
  153. package/dist/esm/v2/computeBundleJSV2.js +29 -0
  154. package/dist/esm/v2/getComputedIndexHtml.js +81 -0
  155. package/dist/esm/v2/getComputedIndexHtml.test.js +134 -0
  156. package/dist/esm/v2/index.js +1 -0
  157. package/dist/esm/v2/v2Build.js +11 -0
  158. package/dist/esm/v3/cleanRollupErrorMessage.js +9 -0
  159. package/dist/esm/v3/computeBundleJSV3.js +48 -0
  160. package/dist/esm/v3/createVizContent.js +23 -0
  161. package/dist/esm/v3/extractVizImport.js +23 -0
  162. package/dist/esm/v3/extractVizImport.test.js +30 -0
  163. package/dist/esm/v3/htmlTemplate.js +89 -0
  164. package/dist/esm/v3/index.js +1 -0
  165. package/dist/esm/v3/parseId.js +5 -0
  166. package/dist/esm/v3/parseId.test.js +25 -0
  167. package/dist/esm/v3/setupV3Runtime.js +341 -0
  168. package/dist/esm/v3/slugCache.js +35 -0
  169. package/dist/esm/v3/slugCache.test.js +105 -0
  170. package/dist/esm/v3/transformDSV/dsvParseSrc.js +121 -0
  171. package/dist/esm/v3/transformDSV/index.js +40 -0
  172. package/dist/esm/v3/transformSvelte.js +77 -0
  173. package/dist/esm/v3/types.js +1 -0
  174. package/dist/esm/v3/v3Build.js +30 -0
  175. package/dist/esm/v3/vizCache.js +36 -0
  176. package/dist/esm/v3/vizCache.test.js +98 -0
  177. package/dist/esm/v3/vizLoad.js +46 -0
  178. package/dist/esm/v3/vizResolve.js +69 -0
  179. package/dist/esm/v4/index.js +134 -0
  180. package/dist/index.d.ts +3 -14
  181. package/dist/index.js +3 -67
  182. package/dist/test/testInBrowser.d.ts +1 -2
  183. package/dist/test/testStackTrace.d.ts +1 -1
  184. package/dist/types/buildHTML.d.ts +14 -0
  185. package/dist/types/common/packageJson.d.ts +39 -0
  186. package/dist/types/common/packageJson.test.d.ts +1 -0
  187. package/dist/types/common/sucrasePlugin.d.ts +2 -0
  188. package/dist/types/common/virtualFileSystem.d.ts +3 -0
  189. package/dist/types/determineRuntimeVersion.d.ts +3 -0
  190. package/dist/types/determineRuntimeVersion.test.d.ts +1 -0
  191. package/dist/types/index.d.ts +3 -0
  192. package/dist/types/test/fixtures/v1/basicHTML.d.ts +3 -0
  193. package/dist/types/test/fixtures/v1/fetchProxy.d.ts +5 -0
  194. package/dist/types/test/fixtures/v1/index.d.ts +6 -0
  195. package/dist/types/test/fixtures/v1/jsScriptTag.d.ts +4 -0
  196. package/dist/types/test/fixtures/v1/protocolTest.d.ts +3 -0
  197. package/dist/types/test/fixtures/v1/styleTest.d.ts +4 -0
  198. package/dist/types/test/fixtures/v1/xmlTest.d.ts +4 -0
  199. package/dist/types/test/fixtures/v2/basicBundle.d.ts +5 -0
  200. package/dist/types/test/fixtures/v2/basicBundleNoExtension.d.ts +5 -0
  201. package/dist/types/test/fixtures/v2/d3Import.d.ts +4 -0
  202. package/dist/types/test/fixtures/v2/d3ImportPkg.d.ts +5 -0
  203. package/dist/types/test/fixtures/v2/d3RosettaImportPkg.d.ts +5 -0
  204. package/dist/types/test/fixtures/v2/es6Preserve.d.ts +4 -0
  205. package/dist/types/test/fixtures/v2/generatorSupport.d.ts +4 -0
  206. package/dist/types/test/fixtures/v2/index.d.ts +14 -0
  207. package/dist/types/test/fixtures/v2/jsxTranspile.d.ts +4 -0
  208. package/dist/types/test/fixtures/v2/reactDomImport.d.ts +4 -0
  209. package/dist/types/test/fixtures/v2/reactDomImportPkg.d.ts +5 -0
  210. package/dist/types/test/fixtures/v2/reactImport.d.ts +4 -0
  211. package/dist/types/test/fixtures/v2/reactImportPkg.d.ts +5 -0
  212. package/dist/types/test/fixtures/v2/sourceMapErrorFixture.d.ts +5 -0
  213. package/dist/types/test/fixtures/v2/unicodeSupport.d.ts +4 -0
  214. package/dist/types/test/fixtures/v3/basicIndexJS.d.ts +3 -0
  215. package/dist/types/test/fixtures/v3/cssImport.d.ts +4 -0
  216. package/dist/types/test/fixtures/v3/csvImport.d.ts +4 -0
  217. package/dist/types/test/fixtures/v3/csvStrangeChars.d.ts +4 -0
  218. package/dist/types/test/fixtures/v3/index.d.ts +9 -0
  219. package/dist/types/test/fixtures/v3/jsExport.d.ts +4 -0
  220. package/dist/types/test/fixtures/v3/sourcemap.d.ts +3 -0
  221. package/dist/types/test/fixtures/v3/svelte.d.ts +4 -0
  222. package/dist/types/test/fixtures/v3/vizContent.d.ts +2 -0
  223. package/dist/types/test/fixtures/v3/vizImport.d.ts +6 -0
  224. package/dist/types/test/fixtures/v4/d3Usage.d.ts +5 -0
  225. package/dist/types/test/fixtures/v4/esmBuild.d.ts +5 -0
  226. package/dist/types/test/fixtures/v4/fetchInterception.d.ts +5 -0
  227. package/dist/types/test/fixtures/v4/index.d.ts +9 -0
  228. package/dist/types/test/fixtures/v4/jsScriptTagTypeModule.d.ts +4 -0
  229. package/dist/types/test/fixtures/v4/jsScriptTagTypeModules.d.ts +5 -0
  230. package/dist/types/test/fixtures/v4/reactHooks.d.ts +6 -0
  231. package/dist/types/test/fixtures/v4/reactJsx.d.ts +6 -0
  232. package/dist/types/test/fixtures/v4/threeJsUsage.d.ts +4 -0
  233. package/dist/types/test/fixtures/v4/typeScriptSupport.d.ts +5 -0
  234. package/dist/types/test/testInBrowser.d.ts +15 -0
  235. package/dist/types/test/testStackTrace.d.ts +9 -0
  236. package/dist/types/test/v1.test.d.ts +1 -0
  237. package/dist/types/test/v2.test.d.ts +1 -0
  238. package/dist/types/test/v3.test.d.ts +1 -0
  239. package/dist/types/test/v4.test.d.ts +1 -0
  240. package/dist/types/types.d.ts +1 -0
  241. package/dist/types/utils/vizContentToFileCollection.d.ts +7 -0
  242. package/dist/types/utils/vizContentToFileCollection.test.d.ts +1 -0
  243. package/dist/types/v2/computeBundleJSV2.d.ts +7 -0
  244. package/dist/types/v2/getComputedIndexHtml.d.ts +4 -0
  245. package/dist/types/v2/getComputedIndexHtml.test.d.ts +1 -0
  246. package/dist/types/v2/index.d.ts +1 -0
  247. package/dist/types/v2/v2Build.d.ts +7 -0
  248. package/dist/types/v3/cleanRollupErrorMessage.d.ts +4 -0
  249. package/dist/types/v3/computeBundleJSV3.d.ts +17 -0
  250. package/dist/types/v3/createVizContent.d.ts +8 -0
  251. package/dist/types/v3/extractVizImport.d.ts +6 -0
  252. package/dist/types/v3/extractVizImport.test.d.ts +1 -0
  253. package/dist/types/v3/htmlTemplate.d.ts +5 -0
  254. package/dist/types/v3/index.d.ts +1 -0
  255. package/dist/types/v3/parseId.d.ts +6 -0
  256. package/dist/types/v3/parseId.test.d.ts +1 -0
  257. package/dist/types/v3/setupV3Runtime.d.ts +15 -0
  258. package/dist/types/v3/slugCache.d.ts +11 -0
  259. package/dist/types/v3/slugCache.test.d.ts +1 -0
  260. package/dist/types/v3/transformDSV/dsvParseSrc.d.ts +1 -0
  261. package/dist/types/v3/transformDSV/index.d.ts +2 -0
  262. package/dist/types/v3/transformSvelte.d.ts +8 -0
  263. package/dist/types/v3/types.d.ts +65 -0
  264. package/dist/types/v3/v3Build.d.ts +14 -0
  265. package/dist/types/v3/vizCache.d.ts +10 -0
  266. package/dist/types/v3/vizCache.test.d.ts +1 -0
  267. package/dist/types/v3/vizLoad.d.ts +7 -0
  268. package/dist/types/v3/vizResolve.d.ts +7 -0
  269. package/dist/types/v4/index.d.ts +10 -0
  270. package/dist/types.d.ts +0 -1
  271. package/dist/utils/vizContentToFileCollection.d.ts +1 -2
  272. package/dist/v2/computeBundleJSV2.d.ts +1 -1
  273. package/dist/v2/getComputedIndexHtml.d.ts +1 -1
  274. package/dist/v2/v2Build.d.ts +1 -1
  275. package/dist/v3/cleanRollupErrorMessage.d.ts +4 -0
  276. package/dist/v3/cleanRollupErrorMessage.js +9 -0
  277. package/dist/v3/computeBundleJSV3.d.ts +1 -2
  278. package/dist/v3/createVizContent.d.ts +1 -2
  279. package/dist/v3/setupV3Runtime.d.ts +15 -0
  280. package/dist/v3/setupV3Runtime.js +341 -0
  281. package/dist/v3/v3Build.d.ts +1 -2
  282. package/dist/v3/v3Build.js +1 -1
  283. package/dist/v3/vizLoad.js +1 -1
  284. package/dist/v4/index.d.ts +1 -1
  285. package/package.json +25 -16
@@ -0,0 +1,7 @@
1
+ import { InputPluginOption } from "rollup";
2
+ import { VizId } from "@vizhub/viz-types";
3
+ import { SlugCache } from "./slugCache";
4
+ export declare const vizResolve: ({ vizId, slugCache, }: {
5
+ vizId: VizId;
6
+ slugCache?: SlugCache;
7
+ }) => InputPluginOption;
@@ -0,0 +1,10 @@
1
+ import type { RollupBuild, RollupOptions } from "rollup";
2
+ import { FileCollection } from "@vizhub/viz-types";
3
+ /**
4
+ * Build for v4 runtime
5
+ */
6
+ export declare const v4Build: ({ files, rollup, enableSourcemap, }: {
7
+ files: FileCollection;
8
+ rollup: (options: RollupOptions) => Promise<RollupBuild>;
9
+ enableSourcemap?: boolean;
10
+ }) => Promise<FileCollection>;
package/dist/types.d.ts CHANGED
@@ -1,2 +1 @@
1
- export { FileCollection } from "magic-sandbox";
2
1
  export type runtimeVersion = "v1" | "v2" | "v3" | "v4";
@@ -1,5 +1,4 @@
1
- import type { VizContent } from "@vizhub/viz-types";
2
- import type { FileCollection } from "magic-sandbox";
1
+ import type { FileCollection, VizContent } from "@vizhub/viz-types";
3
2
  /**
4
3
  * Converts VizContent format to FileCollection format
5
4
  * VizContent has files as {id: {name, text}} structure
@@ -1,5 +1,5 @@
1
- import { FileCollection } from "../types";
2
1
  import type { RollupBuild, RollupOptions } from "rollup";
2
+ import { FileCollection } from "@vizhub/viz-types";
3
3
  export declare const computeBundleJSV2: ({ files, rollup, enableSourcemap, }: {
4
4
  files: FileCollection;
5
5
  rollup: (options: RollupOptions) => Promise<RollupBuild>;
@@ -1,4 +1,4 @@
1
+ import { FileCollection } from "@vizhub/viz-types";
1
2
  import type { JSDOM } from "jsdom";
2
- import { FileCollection } from "../types";
3
3
  export declare const setJSDOM: (JSDOMInstance: typeof JSDOM) => void;
4
4
  export declare const getComputedIndexHtml: (files: FileCollection) => string;
@@ -1,5 +1,5 @@
1
- import { FileCollection } from "magic-sandbox";
2
1
  import type { RollupBuild, RollupOptions } from "rollup";
2
+ import { FileCollection } from "@vizhub/viz-types";
3
3
  export declare const v2Build: ({ files, rollup, enableSourcemap, }: {
4
4
  files: FileCollection;
5
5
  rollup: (options: RollupOptions) => Promise<RollupBuild>;
@@ -0,0 +1,4 @@
1
+ export declare const cleanRollupErrorMessage: ({ rawMessage, vizId, }: {
2
+ rawMessage: string;
3
+ vizId: string;
4
+ }) => string;
@@ -0,0 +1,9 @@
1
+ // We want to remove the vizId from the error message
2
+ // to make it more user-friendly.
3
+ // Example error message before and after:
4
+ // Before: "7f0b69fcb754479699172d1887817027/index.js (14:8): Expected ';', '}' or <eof>"
5
+ // After: "./index.js (14:8): Expected ';', '}' or <eof>"
6
+ export const cleanRollupErrorMessage = ({ rawMessage, vizId, }) => {
7
+ const regex = new RegExp(vizId, "g");
8
+ return rawMessage?.replace(regex, ".");
9
+ };
@@ -1,7 +1,6 @@
1
- import { FileCollection } from "../types";
2
1
  import type { RollupBuild, RollupOptions } from "rollup";
3
2
  import { VizCache } from "./vizCache";
4
- import { VizId } from "@vizhub/viz-types";
3
+ import { FileCollection, VizId } from "@vizhub/viz-types";
5
4
  import { SlugCache } from "./slugCache";
6
5
  import { SvelteCompiler } from "./transformSvelte";
7
6
  export declare const computeBundleJSV3: ({ files, rollup, enableSourcemap, vizCache, vizId, slugCache, getSvelteCompiler, }: {
@@ -1,5 +1,4 @@
1
- import { VizContent } from "@vizhub/viz-types";
2
- import { FileCollection } from "magic-sandbox";
1
+ import { FileCollection, VizContent } from "@vizhub/viz-types";
3
2
  /**
4
3
  * Creates a VizContent object with the given files
5
4
  * @param files An object with file names as keys and file content as values
@@ -0,0 +1,15 @@
1
+ import { VizContent, VizId } from "@vizhub/viz-types";
2
+ export type V3Runtime = {
3
+ handleCodeChange: (content: VizContent) => void;
4
+ invalidateVizCache: (changedVizIds: Array<VizId>) => void;
5
+ resetSrcdoc: (changedVizIds: Array<VizId>) => void;
6
+ };
7
+ export declare const setupV3Runtime: ({ vizId, iframe, setSrcdocErrorMessage, getLatestContent, resolveSlugKey, writeFile, worker, }: {
8
+ vizId: VizId;
9
+ iframe: HTMLIFrameElement;
10
+ setSrcdocErrorMessage: (error: string | null) => void;
11
+ getLatestContent: (vizId: VizId) => Promise<VizContent>;
12
+ resolveSlugKey: (slugKey: string) => Promise<VizId>;
13
+ writeFile: (fileName: string, content: string) => void;
14
+ worker: Worker;
15
+ }) => V3Runtime;
@@ -0,0 +1,341 @@
1
+ import { parseId } from "./parseId";
2
+ import { cleanRollupErrorMessage } from "./cleanRollupErrorMessage";
3
+ import { getFileText } from "@vizhub/viz-utils";
4
+ // Flag for debugging.
5
+ const debug = false;
6
+ // Nothing happening.
7
+ const IDLE = "IDLE";
8
+ // An update has been enqueued
9
+ // via requestAnimationFrame.
10
+ const ENQUEUED = "ENQUEUED";
11
+ // An update (build and run) is pending,
12
+ // and the files have not changed.
13
+ const PENDING_CLEAN = "PENDING_CLEAN";
14
+ // An update (build and run) is pending,
15
+ // and the files have changed
16
+ // while this run is taking place.
17
+ const PENDING_DIRTY = "PENDING_DIRTY";
18
+ export const setupV3Runtime = ({ vizId, iframe, setSrcdocErrorMessage, getLatestContent, resolveSlugKey, writeFile, worker, }) => {
19
+ // Valid State Transitions:
20
+ //
21
+ // * IDLE --> ENQUEUED
22
+ // When the system is idle and files are changed.
23
+ //
24
+ // * ENQUEUED --> PENDING_CLEAN
25
+ // When the pending changes run.
26
+ //
27
+ // * PENDING_CLEAN --> IDLE
28
+ // When the pending update finishes running
29
+ // and files were not changed in the mean time.
30
+ //
31
+ // * PENDING_CLEAN --> PENDING_DIRTY
32
+ // When files are changed while an update is pending.
33
+ //
34
+ // * PENDING_DIRTY --> ENQUEUED
35
+ // When the pending update finishes running
36
+ // and files were changed in the mean time.
37
+ //
38
+ // When a build error happens, the state is set to IDLE.
39
+ // This is to prevent a build error from causing
40
+ // the whole system to stop working.
41
+ //
42
+ // Valid State Transitions (with build errors):
43
+ // TODO complete this section
44
+ let state = IDLE;
45
+ if (debug) {
46
+ setInterval(() => {
47
+ console.log("state", state);
48
+ }, 1000);
49
+ }
50
+ // Pending promise resolvers.
51
+ let pendingBuildPromise = null;
52
+ let pendingRunPromise = null;
53
+ // Logic around profiling build times.
54
+ const profileBuildTimes = debug;
55
+ let buildTimes = [];
56
+ const avg = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
57
+ const n = 100;
58
+ // This runs when the build worker sends a message.
59
+ worker.addEventListener("message", async ({ data }) => {
60
+ const message = data;
61
+ // Handle 'buildResponse' messages.
62
+ // These are sent by the build worker in response
63
+ // to a 'buildRequest' message.
64
+ if (message.type === "buildResponse") {
65
+ const buildResult = message.buildResult;
66
+ const error = message.error;
67
+ if (profileBuildTimes && buildResult) {
68
+ buildTimes.push(buildResult.time);
69
+ // Every n times, log the rolling average.
70
+ if (buildTimes.length % n === 0) {
71
+ console.log("Average build time: " +
72
+ avg(buildTimes) +
73
+ " ms");
74
+ buildTimes = [];
75
+ }
76
+ }
77
+ // Regardless of whether the build succeeded or failed,
78
+ // resolve the pending build promise,
79
+ // so that the system remains responsive.
80
+ if (pendingBuildPromise) {
81
+ pendingBuildPromise(buildResult);
82
+ pendingBuildPromise = null;
83
+ }
84
+ if (error) {
85
+ setSrcdocErrorMessage(cleanRollupErrorMessage({
86
+ rawMessage: error.message,
87
+ vizId,
88
+ }));
89
+ }
90
+ }
91
+ // Handle 'contentRequest' messages.
92
+ // These are sent by the worker when it needs
93
+ // to get the content of a file, in order to
94
+ // populate its VizCache.
95
+ if (message.type === "contentRequest") {
96
+ const { vizId } = message;
97
+ const content = await getLatestContent(vizId);
98
+ const contentResponseMessage = {
99
+ type: "contentResponse",
100
+ vizId: message.vizId,
101
+ content,
102
+ };
103
+ if (debug) {
104
+ console.log("[v3 runtime] received contentRequest, sending contentResponse", contentResponseMessage);
105
+ }
106
+ // Send the content back to the worker.
107
+ worker.postMessage(contentResponseMessage);
108
+ }
109
+ // Handle 'resolveSlugRequest' messages.
110
+ // These are sent by the worker when it needs
111
+ // to resolve a slug import to a viz ID.
112
+ if (message.type === "resolveSlugRequest") {
113
+ const { slugKey } = message;
114
+ const resolveSlugResponseMessage = {
115
+ type: "resolveSlugResponse",
116
+ slugKey,
117
+ requestId: message.requestId,
118
+ vizId: await resolveSlugKey(slugKey),
119
+ };
120
+ if (debug) {
121
+ console.log("[v3 runtime] received resolveSlugRequest, sending resolveSlugResponse", resolveSlugResponseMessage);
122
+ }
123
+ // Send the viz ID back to the worker.
124
+ worker.postMessage(resolveSlugResponseMessage);
125
+ }
126
+ // Handle 'invalidateVizCacheResponse' messages.
127
+ // These are sent by the worker in response to
128
+ // an 'invalidateVizCacheRequest' message.
129
+ if (message.type === "invalidateVizCacheResponse") {
130
+ if (debug) {
131
+ console.log("[v3 runtime] received invalidateVizCacheResponse", message);
132
+ }
133
+ // Leverage existing infra for executing the hot reloading.
134
+ handleCodeChange();
135
+ }
136
+ if (message.type === "resetSrcdocResponse") {
137
+ const srcdoc = message.srcdoc;
138
+ const error = message.error;
139
+ if (error) {
140
+ setSrcdocErrorMessage(cleanRollupErrorMessage({
141
+ rawMessage: error.message,
142
+ vizId,
143
+ }));
144
+ }
145
+ else {
146
+ setSrcdocErrorMessage(null);
147
+ // Really reset the srcdoc!
148
+ // console.log('Really reset the srcdoc!');
149
+ if (srcdoc) {
150
+ iframe.srcdoc = srcdoc;
151
+ }
152
+ }
153
+ }
154
+ });
155
+ // This runs when the IFrame sends a message.
156
+ window.addEventListener("message", ({ data }) => {
157
+ // Handle 'runDone' and 'runError' messages.
158
+ // These happen in response to sending a 'runJS' message.
159
+ if (data.type === "runDone" ||
160
+ data.type === "runError") {
161
+ // console.log('got ' + data.type);
162
+ if (pendingRunPromise) {
163
+ // TODO pass errors out for display
164
+ // pendingRunPromise(data as V3WindowMessage);
165
+ pendingRunPromise();
166
+ pendingRunPromise = null;
167
+ }
168
+ }
169
+ if (data.type === "runError") {
170
+ setSrcdocErrorMessage(data.error.message);
171
+ }
172
+ if (data.type === "writeFile") {
173
+ if (data.fileName && data.content) {
174
+ writeFile(data.fileName, data.content);
175
+ }
176
+ }
177
+ });
178
+ const handleCodeChange = () => {
179
+ if (state === IDLE) {
180
+ state = ENQUEUED;
181
+ update();
182
+ }
183
+ else if (state === PENDING_CLEAN) {
184
+ state = PENDING_DIRTY;
185
+ }
186
+ };
187
+ // This runs when one or more imported vizzes are changed.
188
+ const invalidateVizCache = (changedVizIds) => {
189
+ // Send a message to the worker to invalidate the cache.
190
+ const message = {
191
+ type: "invalidateVizCacheRequest",
192
+ changedVizIds,
193
+ };
194
+ worker.postMessage(message);
195
+ };
196
+ const profileHotReloadFPS = true;
197
+ let updateCount = 0;
198
+ if (profileHotReloadFPS) {
199
+ setInterval(() => {
200
+ if (debug && updateCount > 0) {
201
+ console.log(updateCount +
202
+ " hot reload" +
203
+ (updateCount !== 1 ? "s" : "") +
204
+ " in the last second");
205
+ }
206
+ updateCount = 0;
207
+ }, 1000);
208
+ }
209
+ const build = () => {
210
+ return new Promise((resolve) => {
211
+ pendingBuildPromise = resolve;
212
+ const message = {
213
+ type: "buildRequest",
214
+ vizId,
215
+ enableSourcemap: true,
216
+ };
217
+ worker.postMessage(message);
218
+ });
219
+ };
220
+ // Builds and runs the latest files.
221
+ const update = async () => {
222
+ state = PENDING_CLEAN;
223
+ if (debug) {
224
+ console.log("update: before run");
225
+ }
226
+ // Build the code. This may fail and return `undefined`.
227
+ const buildResult = await build();
228
+ // If the build was successful, run the code.
229
+ if (buildResult !== undefined) {
230
+ await run(buildResult);
231
+ }
232
+ if (debug) {
233
+ console.log("update: after run");
234
+ }
235
+ updateCount++;
236
+ // TypeScript can't comprehend that `state`
237
+ // may change during the await calls above.
238
+ // @ts-ignore
239
+ if (state === PENDING_DIRTY) {
240
+ requestAnimationFrame(update);
241
+ state = ENQUEUED;
242
+ }
243
+ else {
244
+ state = IDLE;
245
+ }
246
+ };
247
+ let previousCSSFiles = [];
248
+ const run = (buildResult) => {
249
+ return new Promise((resolve) => {
250
+ const { src, warnings, cssFiles } = buildResult;
251
+ // Sanity check.
252
+ // At this point, since there were no errors,
253
+ // we expect there to be a `src` property.
254
+ // This should never happen, but log & error just in case!
255
+ if (src === undefined) {
256
+ if (debug) {
257
+ console.log("[v3 runtime] src is undefined, but no errors!");
258
+ }
259
+ throw new Error("[v3 runtime] src is undefined, but no errors!");
260
+ }
261
+ // Set pendingRunPromise because at this point,
262
+ // we expect an asynchronous response when the run is done.
263
+ // The iframe should send either a `runDone` or `runError` message.
264
+ pendingRunPromise = resolve;
265
+ // Handle build warnings
266
+ if (warnings.length > 0) {
267
+ // TODO: Distinguish between warnings and errors in UI
268
+ setSrcdocErrorMessage(warnings
269
+ .map((warning) => warning.message)
270
+ .join("\n\n"));
271
+ }
272
+ else {
273
+ setSrcdocErrorMessage(null); // Clear error message if no warnings
274
+ }
275
+ if (iframe.contentWindow) {
276
+ // For each cssFiles
277
+ for (const cssFile of cssFiles) {
278
+ const { vizId, fileName } = parseId(cssFile);
279
+ getLatestContent(vizId).then((content) => {
280
+ const src = getFileText(content, fileName);
281
+ if (src === null) {
282
+ // The file doesn't exist.
283
+ // TODO surface this error to the user
284
+ // in a nicer way than this.
285
+ console.warn(`Imported CSS file ${fileName} doesn't exist.`);
286
+ return;
287
+ }
288
+ // TODO only inject CSS if it has changed.
289
+ const runCSSMessage = {
290
+ type: "runCSS",
291
+ id: cssFile,
292
+ src,
293
+ };
294
+ if (debug) {
295
+ console.log("runCSSMessage", runCSSMessage);
296
+ }
297
+ iframe.contentWindow?.postMessage(runCSSMessage, window.location.origin);
298
+ });
299
+ }
300
+ // Detect which CSS files have been removed
301
+ // and remove them from the iframe.
302
+ const removedCSSFiles = previousCSSFiles.filter((id) => !cssFiles.includes(id));
303
+ previousCSSFiles = cssFiles;
304
+ if (debug) {
305
+ console.log("removedCSSFiles", removedCSSFiles);
306
+ }
307
+ for (const id of removedCSSFiles) {
308
+ const removeCSSMessage = {
309
+ type: "runCSS",
310
+ id,
311
+ src: "",
312
+ };
313
+ iframe.contentWindow?.postMessage(removeCSSMessage, window.location.origin);
314
+ }
315
+ // Clear the console before each run.
316
+ console.clear();
317
+ const runJSMessage = {
318
+ type: "runJS",
319
+ src,
320
+ };
321
+ iframe.contentWindow.postMessage(runJSMessage, window.location.origin);
322
+ }
323
+ });
324
+ };
325
+ const resetSrcdoc = (changedVizIds) => {
326
+ state = IDLE;
327
+ pendingBuildPromise = null;
328
+ pendingRunPromise = null;
329
+ const message = {
330
+ type: "resetSrcdocRequest",
331
+ vizId,
332
+ changedVizIds,
333
+ };
334
+ worker.postMessage(message);
335
+ };
336
+ return {
337
+ handleCodeChange,
338
+ invalidateVizCache,
339
+ resetSrcdoc,
340
+ };
341
+ };
@@ -1,7 +1,6 @@
1
1
  import { RollupBuild, RollupOptions } from "rollup";
2
- import { FileCollection } from "../types";
3
2
  import { VizCache } from "./vizCache";
4
- import { VizId } from "@vizhub/viz-types";
3
+ import { FileCollection, VizId } from "@vizhub/viz-types";
5
4
  import { SlugCache } from "./slugCache";
6
5
  import { SvelteCompiler } from "./transformSvelte";
7
6
  export declare const v3Build: ({ files, rollup, enableSourcemap, vizCache, vizId, slugCache, getSvelteCompiler, }: {
@@ -1,7 +1,7 @@
1
1
  import { computeBundleJSV3 } from "./computeBundleJSV3";
2
2
  import { htmlTemplate } from "./htmlTemplate";
3
3
  import { parseId } from "./parseId";
4
- import { getFileText } from "../utils/getFileText";
4
+ import { getFileText } from "@vizhub/viz-utils";
5
5
  export const v3Build = async ({ files, rollup, enableSourcemap = true, vizCache, vizId, slugCache, getSvelteCompiler, }) => {
6
6
  const { src, cssFiles } = await computeBundleJSV3({
7
7
  files,
@@ -1,5 +1,5 @@
1
1
  import { parseId } from "./parseId";
2
- import { getFileText } from "../utils/getFileText";
2
+ import { getFileText } from "@vizhub/viz-utils";
3
3
  const debug = false;
4
4
  // Responsible for loading all imports and
5
5
  // tracking which CSS files are imported.
@@ -1,5 +1,5 @@
1
- import { FileCollection } from "magic-sandbox";
2
1
  import type { RollupBuild, RollupOptions } from "rollup";
2
+ import { FileCollection } from "@vizhub/viz-types";
3
3
  /**
4
4
  * Build for v4 runtime
5
5
  */
package/package.json CHANGED
@@ -1,47 +1,56 @@
1
1
  {
2
2
  "name": "@vizhub/runtime",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Flexible runtime environment for data visualization sandboxes",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/types/index.d.ts",
8
9
  "exports": {
9
10
  ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
11
+ "import": "./dist/esm/index.js",
12
+ "require": "./dist/cjs/index.js",
13
+ "default": "./dist/esm/index.js",
14
+ "types": "./dist/types/index.d.ts"
12
15
  }
13
16
  },
14
17
  "files": [
15
- "dist"
18
+ "dist",
19
+ "README.md"
16
20
  ],
17
21
  "scripts": {
18
- "build": "tsc",
22
+ "build": "npm run build:esm && npm run build:cjs && npm run build:types",
23
+ "build:esm": "tsc -p tsconfig.esm.json",
24
+ "build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
25
+ "build:types": "tsc --emitDeclarationOnly --declarationDir ./dist/types",
19
26
  "prepublishOnly": "npm run build",
20
27
  "test": "vitest run",
21
28
  "test:watch": "vitest",
22
29
  "typecheck": "tsc --noEmit",
23
- "prettier": "prettier {*.*,**/*.*} --write"
30
+ "prettier": "prettier {*.*,**/*.*} --write",
31
+ "upgrade": "ncu -x svelte -u"
24
32
  },
25
33
  "keywords": [],
26
34
  "author": "",
27
35
  "license": "MIT",
28
36
  "devDependencies": {
29
37
  "@types/jsdom": "^21.1.7",
30
- "@types/node": "^22.13.13",
38
+ "@types/node": "^22.14.0",
31
39
  "@types/uuid": "^10.0.0",
32
40
  "jsdom": "^26.0.0",
41
+ "npm-check-updates": "^17.1.16",
33
42
  "prettier": "^3.5.3",
34
- "puppeteer": "^24.4.0",
35
- "rollup": "^4.37.0",
43
+ "puppeteer": "^24.6.0",
44
+ "rollup": "^4.39.0",
36
45
  "sucrase": "^3.35.0",
37
46
  "svelte": "4.2.9",
38
- "typescript": "^5.8.2",
39
- "vitest": "^3.0.9"
47
+ "typescript": "^5.8.3",
48
+ "vitest": "^3.1.1"
40
49
  },
41
50
  "dependencies": {
42
- "@vizhub/viz-types": "^0.0.2",
43
- "@vizhub/viz-utils": "^0.0.1",
44
- "magic-sandbox": "^2.1.0",
51
+ "@vizhub/viz-types": "^0.1.0",
52
+ "@vizhub/viz-utils": "^0.1.0",
53
+ "magic-sandbox": "^2.2.0",
45
54
  "uuid": "^11.1.0"
46
55
  }
47
56
  }