@vizhub/runtime 0.3.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 (260) hide show
  1. package/dist/cjs/buildHTML.js +71 -0
  2. package/dist/cjs/common/packageJson.js +81 -0
  3. package/dist/cjs/common/packageJson.test.js +148 -0
  4. package/dist/cjs/common/sucrasePlugin.js +27 -0
  5. package/dist/cjs/common/virtualFileSystem.js +79 -0
  6. package/dist/cjs/determineRuntimeVersion.js +26 -0
  7. package/dist/cjs/determineRuntimeVersion.test.js +103 -0
  8. package/dist/cjs/index.js +9 -0
  9. package/dist/cjs/package.json +1 -0
  10. package/dist/cjs/test/fixtures/v1/basicHTML.js +18 -0
  11. package/dist/cjs/test/fixtures/v1/fetchProxy.js +15 -0
  12. package/dist/cjs/test/fixtures/v1/index.js +15 -0
  13. package/dist/cjs/test/fixtures/v1/jsScriptTag.js +12 -0
  14. package/dist/cjs/test/fixtures/v1/protocolTest.js +18 -0
  15. package/dist/cjs/test/fixtures/v1/styleTest.js +18 -0
  16. package/dist/cjs/test/fixtures/v1/xmlTest.js +22 -0
  17. package/dist/cjs/test/fixtures/v2/basicBundle.js +8 -0
  18. package/dist/cjs/test/fixtures/v2/basicBundleNoExtension.js +8 -0
  19. package/dist/cjs/test/fixtures/v2/d3Import.js +10 -0
  20. package/dist/cjs/test/fixtures/v2/d3ImportPkg.js +22 -0
  21. package/dist/cjs/test/fixtures/v2/d3RosettaImportPkg.js +22 -0
  22. package/dist/cjs/test/fixtures/v2/es6Preserve.js +9 -0
  23. package/dist/cjs/test/fixtures/v2/generatorSupport.js +9 -0
  24. package/dist/cjs/test/fixtures/v2/index.js +45 -0
  25. package/dist/cjs/test/fixtures/v2/jsxTranspile.js +9 -0
  26. package/dist/cjs/test/fixtures/v2/reactDomImport.js +11 -0
  27. package/dist/cjs/test/fixtures/v2/reactDomImportPkg.js +27 -0
  28. package/dist/cjs/test/fixtures/v2/reactImport.js +10 -0
  29. package/dist/cjs/test/fixtures/v2/reactImportPkg.js +20 -0
  30. package/dist/cjs/test/fixtures/v2/sourceMapErrorFixture.js +22 -0
  31. package/dist/cjs/test/fixtures/v2/unicodeSupport.js +9 -0
  32. package/dist/cjs/test/fixtures/v3/basicIndexJS.js +10 -0
  33. package/dist/cjs/test/fixtures/v3/cssImport.js +12 -0
  34. package/dist/cjs/test/fixtures/v3/csvImport.js +14 -0
  35. package/dist/cjs/test/fixtures/v3/csvStrangeChars.js +15 -0
  36. package/dist/cjs/test/fixtures/v3/index.js +25 -0
  37. package/dist/cjs/test/fixtures/v3/jsExport.js +13 -0
  38. package/dist/cjs/test/fixtures/v3/sourcemap.js +13 -0
  39. package/dist/cjs/test/fixtures/v3/svelte.js +21 -0
  40. package/dist/cjs/test/fixtures/v3/vizContent.js +14 -0
  41. package/dist/cjs/test/fixtures/v3/vizImport.js +88 -0
  42. package/dist/cjs/test/fixtures/v4/d3Usage.js +34 -0
  43. package/dist/cjs/test/fixtures/v4/esmBuild.js +21 -0
  44. package/dist/cjs/test/fixtures/v4/fetchInterception.js +21 -0
  45. package/dist/cjs/test/fixtures/v4/index.js +21 -0
  46. package/dist/cjs/test/fixtures/v4/jsScriptTagTypeModule.js +21 -0
  47. package/dist/cjs/test/fixtures/v4/jsScriptTagTypeModules.js +15 -0
  48. package/dist/cjs/test/fixtures/v4/reactHooks.js +50 -0
  49. package/dist/cjs/test/fixtures/v4/reactJsx.js +37 -0
  50. package/dist/cjs/test/fixtures/v4/threeJsUsage.js +27 -0
  51. package/dist/cjs/test/fixtures/v4/typeScriptSupport.js +32 -0
  52. package/dist/cjs/test/testInBrowser.js +50 -0
  53. package/dist/cjs/test/testStackTrace.js +58 -0
  54. package/dist/cjs/test/v1.test.js +70 -0
  55. package/dist/cjs/test/v2.test.js +124 -0
  56. package/dist/cjs/test/v3.test.js +119 -0
  57. package/dist/cjs/test/v4.test.js +105 -0
  58. package/dist/cjs/types.js +2 -0
  59. package/dist/cjs/utils/vizContentToFileCollection.js +23 -0
  60. package/dist/cjs/utils/vizContentToFileCollection.test.js +77 -0
  61. package/dist/cjs/v2/computeBundleJSV2.js +33 -0
  62. package/dist/cjs/v2/getComputedIndexHtml.js +86 -0
  63. package/dist/cjs/v2/getComputedIndexHtml.test.js +136 -0
  64. package/dist/cjs/v2/index.js +5 -0
  65. package/dist/cjs/v2/v2Build.js +15 -0
  66. package/dist/cjs/v3/cleanRollupErrorMessage.js +13 -0
  67. package/dist/cjs/v3/computeBundleJSV3.js +52 -0
  68. package/dist/cjs/v3/createVizContent.js +27 -0
  69. package/dist/cjs/v3/extractVizImport.js +27 -0
  70. package/dist/cjs/v3/extractVizImport.test.js +32 -0
  71. package/dist/cjs/v3/htmlTemplate.js +93 -0
  72. package/dist/cjs/v3/index.js +5 -0
  73. package/dist/cjs/v3/parseId.js +9 -0
  74. package/dist/cjs/v3/parseId.test.js +27 -0
  75. package/dist/cjs/v3/setupV3Runtime.js +345 -0
  76. package/dist/cjs/v3/slugCache.js +39 -0
  77. package/dist/cjs/v3/slugCache.test.js +107 -0
  78. package/dist/cjs/v3/transformDSV/dsvParseSrc.js +124 -0
  79. package/dist/cjs/v3/transformDSV/index.js +44 -0
  80. package/dist/cjs/v3/transformSvelte.js +81 -0
  81. package/dist/cjs/v3/types.js +2 -0
  82. package/dist/cjs/v3/v3Build.js +34 -0
  83. package/dist/cjs/v3/vizCache.js +40 -0
  84. package/dist/cjs/v3/vizCache.test.js +100 -0
  85. package/dist/cjs/v3/vizLoad.js +50 -0
  86. package/dist/cjs/v3/vizResolve.js +73 -0
  87. package/dist/cjs/v4/index.js +138 -0
  88. package/dist/esm/buildHTML.js +67 -0
  89. package/dist/esm/common/packageJson.js +71 -0
  90. package/dist/esm/common/packageJson.test.js +146 -0
  91. package/dist/esm/common/sucrasePlugin.js +24 -0
  92. package/dist/esm/common/virtualFileSystem.js +75 -0
  93. package/dist/esm/determineRuntimeVersion.js +22 -0
  94. package/dist/esm/determineRuntimeVersion.test.js +101 -0
  95. package/dist/esm/index.js +3 -0
  96. package/dist/esm/test/fixtures/v1/basicHTML.js +15 -0
  97. package/dist/esm/test/fixtures/v1/fetchProxy.js +12 -0
  98. package/dist/esm/test/fixtures/v1/index.js +6 -0
  99. package/dist/esm/test/fixtures/v1/jsScriptTag.js +9 -0
  100. package/dist/esm/test/fixtures/v1/protocolTest.js +15 -0
  101. package/dist/esm/test/fixtures/v1/styleTest.js +15 -0
  102. package/dist/esm/test/fixtures/v1/xmlTest.js +19 -0
  103. package/dist/esm/test/fixtures/v2/basicBundle.js +5 -0
  104. package/dist/esm/test/fixtures/v2/basicBundleNoExtension.js +5 -0
  105. package/dist/esm/test/fixtures/v2/d3Import.js +7 -0
  106. package/dist/esm/test/fixtures/v2/d3ImportPkg.js +19 -0
  107. package/dist/esm/test/fixtures/v2/d3RosettaImportPkg.js +19 -0
  108. package/dist/esm/test/fixtures/v2/es6Preserve.js +6 -0
  109. package/dist/esm/test/fixtures/v2/generatorSupport.js +6 -0
  110. package/dist/esm/test/fixtures/v2/index.js +15 -0
  111. package/dist/esm/test/fixtures/v2/jsxTranspile.js +6 -0
  112. package/dist/esm/test/fixtures/v2/reactDomImport.js +8 -0
  113. package/dist/esm/test/fixtures/v2/reactDomImportPkg.js +24 -0
  114. package/dist/esm/test/fixtures/v2/reactImport.js +7 -0
  115. package/dist/esm/test/fixtures/v2/reactImportPkg.js +17 -0
  116. package/dist/esm/test/fixtures/v2/sourceMapErrorFixture.js +19 -0
  117. package/dist/esm/test/fixtures/v2/unicodeSupport.js +6 -0
  118. package/dist/esm/test/fixtures/v3/basicIndexJS.js +7 -0
  119. package/dist/esm/test/fixtures/v3/cssImport.js +9 -0
  120. package/dist/esm/test/fixtures/v3/csvImport.js +11 -0
  121. package/dist/esm/test/fixtures/v3/csvStrangeChars.js +12 -0
  122. package/dist/esm/test/fixtures/v3/index.js +9 -0
  123. package/dist/esm/test/fixtures/v3/jsExport.js +10 -0
  124. package/dist/esm/test/fixtures/v3/sourcemap.js +10 -0
  125. package/dist/esm/test/fixtures/v3/svelte.js +18 -0
  126. package/dist/esm/test/fixtures/v3/vizContent.js +11 -0
  127. package/dist/esm/test/fixtures/v3/vizImport.js +85 -0
  128. package/dist/esm/test/fixtures/v4/d3Usage.js +31 -0
  129. package/dist/esm/test/fixtures/v4/esmBuild.js +18 -0
  130. package/dist/esm/test/fixtures/v4/fetchInterception.js +18 -0
  131. package/dist/esm/test/fixtures/v4/index.js +9 -0
  132. package/dist/esm/test/fixtures/v4/jsScriptTagTypeModule.js +18 -0
  133. package/dist/esm/test/fixtures/v4/jsScriptTagTypeModules.js +12 -0
  134. package/dist/esm/test/fixtures/v4/reactHooks.js +47 -0
  135. package/dist/esm/test/fixtures/v4/reactJsx.js +34 -0
  136. package/dist/esm/test/fixtures/v4/threeJsUsage.js +24 -0
  137. package/dist/esm/test/fixtures/v4/typeScriptSupport.js +29 -0
  138. package/dist/esm/test/testInBrowser.js +47 -0
  139. package/dist/esm/test/testStackTrace.js +55 -0
  140. package/dist/esm/test/v1.test.js +65 -0
  141. package/dist/esm/test/v2.test.js +121 -0
  142. package/dist/esm/test/v3.test.js +114 -0
  143. package/dist/esm/test/v4.test.js +100 -0
  144. package/dist/esm/types.js +1 -0
  145. package/dist/esm/utils/vizContentToFileCollection.js +19 -0
  146. package/dist/esm/utils/vizContentToFileCollection.test.js +75 -0
  147. package/dist/esm/v2/computeBundleJSV2.js +29 -0
  148. package/dist/esm/v2/getComputedIndexHtml.js +81 -0
  149. package/dist/esm/v2/getComputedIndexHtml.test.js +134 -0
  150. package/dist/esm/v2/index.js +1 -0
  151. package/dist/esm/v2/v2Build.js +11 -0
  152. package/dist/esm/v3/cleanRollupErrorMessage.js +9 -0
  153. package/dist/esm/v3/computeBundleJSV3.js +48 -0
  154. package/dist/esm/v3/createVizContent.js +23 -0
  155. package/dist/esm/v3/extractVizImport.js +23 -0
  156. package/dist/esm/v3/extractVizImport.test.js +30 -0
  157. package/dist/esm/v3/htmlTemplate.js +89 -0
  158. package/dist/esm/v3/index.js +1 -0
  159. package/dist/esm/v3/parseId.js +5 -0
  160. package/dist/esm/v3/parseId.test.js +25 -0
  161. package/dist/esm/v3/setupV3Runtime.js +341 -0
  162. package/dist/esm/v3/slugCache.js +35 -0
  163. package/dist/esm/v3/slugCache.test.js +105 -0
  164. package/dist/esm/v3/transformDSV/dsvParseSrc.js +121 -0
  165. package/dist/esm/v3/transformDSV/index.js +40 -0
  166. package/dist/esm/v3/transformSvelte.js +77 -0
  167. package/dist/esm/v3/types.js +1 -0
  168. package/dist/esm/v3/v3Build.js +30 -0
  169. package/dist/esm/v3/vizCache.js +36 -0
  170. package/dist/esm/v3/vizCache.test.js +98 -0
  171. package/dist/esm/v3/vizLoad.js +46 -0
  172. package/dist/esm/v3/vizResolve.js +69 -0
  173. package/dist/esm/v4/index.js +134 -0
  174. package/dist/types/buildHTML.d.ts +14 -0
  175. package/dist/types/common/packageJson.d.ts +39 -0
  176. package/dist/types/common/packageJson.test.d.ts +1 -0
  177. package/dist/types/common/sucrasePlugin.d.ts +2 -0
  178. package/dist/types/common/virtualFileSystem.d.ts +3 -0
  179. package/dist/types/determineRuntimeVersion.d.ts +3 -0
  180. package/dist/types/determineRuntimeVersion.test.d.ts +1 -0
  181. package/dist/types/index.d.ts +3 -0
  182. package/dist/types/test/fixtures/v1/basicHTML.d.ts +3 -0
  183. package/dist/types/test/fixtures/v1/fetchProxy.d.ts +5 -0
  184. package/dist/types/test/fixtures/v1/index.d.ts +6 -0
  185. package/dist/types/test/fixtures/v1/jsScriptTag.d.ts +4 -0
  186. package/dist/types/test/fixtures/v1/protocolTest.d.ts +3 -0
  187. package/dist/types/test/fixtures/v1/styleTest.d.ts +4 -0
  188. package/dist/types/test/fixtures/v1/xmlTest.d.ts +4 -0
  189. package/dist/types/test/fixtures/v2/basicBundle.d.ts +5 -0
  190. package/dist/types/test/fixtures/v2/basicBundleNoExtension.d.ts +5 -0
  191. package/dist/types/test/fixtures/v2/d3Import.d.ts +4 -0
  192. package/dist/types/test/fixtures/v2/d3ImportPkg.d.ts +5 -0
  193. package/dist/types/test/fixtures/v2/d3RosettaImportPkg.d.ts +5 -0
  194. package/dist/types/test/fixtures/v2/es6Preserve.d.ts +4 -0
  195. package/dist/types/test/fixtures/v2/generatorSupport.d.ts +4 -0
  196. package/dist/types/test/fixtures/v2/index.d.ts +14 -0
  197. package/dist/types/test/fixtures/v2/jsxTranspile.d.ts +4 -0
  198. package/dist/types/test/fixtures/v2/reactDomImport.d.ts +4 -0
  199. package/dist/types/test/fixtures/v2/reactDomImportPkg.d.ts +5 -0
  200. package/dist/types/test/fixtures/v2/reactImport.d.ts +4 -0
  201. package/dist/types/test/fixtures/v2/reactImportPkg.d.ts +5 -0
  202. package/dist/types/test/fixtures/v2/sourceMapErrorFixture.d.ts +5 -0
  203. package/dist/types/test/fixtures/v2/unicodeSupport.d.ts +4 -0
  204. package/dist/types/test/fixtures/v3/basicIndexJS.d.ts +3 -0
  205. package/dist/types/test/fixtures/v3/cssImport.d.ts +4 -0
  206. package/dist/types/test/fixtures/v3/csvImport.d.ts +4 -0
  207. package/dist/types/test/fixtures/v3/csvStrangeChars.d.ts +4 -0
  208. package/dist/types/test/fixtures/v3/index.d.ts +9 -0
  209. package/dist/types/test/fixtures/v3/jsExport.d.ts +4 -0
  210. package/dist/types/test/fixtures/v3/sourcemap.d.ts +3 -0
  211. package/dist/types/test/fixtures/v3/svelte.d.ts +4 -0
  212. package/dist/types/test/fixtures/v3/vizContent.d.ts +2 -0
  213. package/dist/types/test/fixtures/v3/vizImport.d.ts +6 -0
  214. package/dist/types/test/fixtures/v4/d3Usage.d.ts +5 -0
  215. package/dist/types/test/fixtures/v4/esmBuild.d.ts +5 -0
  216. package/dist/types/test/fixtures/v4/fetchInterception.d.ts +5 -0
  217. package/dist/types/test/fixtures/v4/index.d.ts +9 -0
  218. package/dist/types/test/fixtures/v4/jsScriptTagTypeModule.d.ts +4 -0
  219. package/dist/types/test/fixtures/v4/jsScriptTagTypeModules.d.ts +5 -0
  220. package/dist/types/test/fixtures/v4/reactHooks.d.ts +6 -0
  221. package/dist/types/test/fixtures/v4/reactJsx.d.ts +6 -0
  222. package/dist/types/test/fixtures/v4/threeJsUsage.d.ts +4 -0
  223. package/dist/types/test/fixtures/v4/typeScriptSupport.d.ts +5 -0
  224. package/dist/types/test/testInBrowser.d.ts +15 -0
  225. package/dist/types/test/testStackTrace.d.ts +9 -0
  226. package/dist/types/test/v1.test.d.ts +1 -0
  227. package/dist/types/test/v2.test.d.ts +1 -0
  228. package/dist/types/test/v3.test.d.ts +1 -0
  229. package/dist/types/test/v4.test.d.ts +1 -0
  230. package/dist/types/types.d.ts +1 -0
  231. package/dist/types/utils/vizContentToFileCollection.d.ts +7 -0
  232. package/dist/types/utils/vizContentToFileCollection.test.d.ts +1 -0
  233. package/dist/types/v2/computeBundleJSV2.d.ts +7 -0
  234. package/dist/types/v2/getComputedIndexHtml.d.ts +4 -0
  235. package/dist/types/v2/getComputedIndexHtml.test.d.ts +1 -0
  236. package/dist/types/v2/index.d.ts +1 -0
  237. package/dist/types/v2/v2Build.d.ts +7 -0
  238. package/dist/types/v3/cleanRollupErrorMessage.d.ts +4 -0
  239. package/dist/types/v3/computeBundleJSV3.d.ts +17 -0
  240. package/dist/types/v3/createVizContent.d.ts +8 -0
  241. package/dist/types/v3/extractVizImport.d.ts +6 -0
  242. package/dist/types/v3/extractVizImport.test.d.ts +1 -0
  243. package/dist/types/v3/htmlTemplate.d.ts +5 -0
  244. package/dist/types/v3/index.d.ts +1 -0
  245. package/dist/types/v3/parseId.d.ts +6 -0
  246. package/dist/types/v3/parseId.test.d.ts +1 -0
  247. package/dist/types/v3/setupV3Runtime.d.ts +15 -0
  248. package/dist/types/v3/slugCache.d.ts +11 -0
  249. package/dist/types/v3/slugCache.test.d.ts +1 -0
  250. package/dist/types/v3/transformDSV/dsvParseSrc.d.ts +1 -0
  251. package/dist/types/v3/transformDSV/index.d.ts +2 -0
  252. package/dist/types/v3/transformSvelte.d.ts +8 -0
  253. package/dist/types/v3/types.d.ts +65 -0
  254. package/dist/types/v3/v3Build.d.ts +14 -0
  255. package/dist/types/v3/vizCache.d.ts +10 -0
  256. package/dist/types/v3/vizCache.test.d.ts +1 -0
  257. package/dist/types/v3/vizLoad.d.ts +7 -0
  258. package/dist/types/v3/vizResolve.d.ts +7 -0
  259. package/dist/types/v4/index.d.ts +10 -0
  260. package/package.json +14 -7
@@ -0,0 +1,77 @@
1
+ import { parseId } from "./parseId";
2
+ const debug = false;
3
+ // The cache of fetched files.
4
+ const fetchedFileCache = new Map();
5
+ let compile;
6
+ const svelteURL = "https://cdn.jsdelivr.net/npm/svelte@4.2.9";
7
+ export const svelteCompilerUrl = `${svelteURL}/compiler.cjs`;
8
+ // Responsible for transforming Svelte files.
9
+ // Inspired by:
10
+ // * https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/compiler/index.js#L2
11
+ // * https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/bundler/index.js#L358
12
+ // * https://github.com/sveltejs/rollup-plugin-svelte/blob/master/index.js#L146C4-L146C51
13
+ export const transformSvelte = ({ getSvelteCompiler, }) => ({
14
+ name: "transformSvelte",
15
+ load: async (resolved) => {
16
+ if (!resolved.startsWith(svelteURL)) {
17
+ return;
18
+ }
19
+ if (debug) {
20
+ console.log("[transformSvelte]: load() " + resolved);
21
+ }
22
+ const cachedFile = fetchedFileCache.get(resolved);
23
+ if (cachedFile)
24
+ return cachedFile;
25
+ const fetchedFile = await fetch(resolved).then((res) => res.text());
26
+ fetchedFileCache.set(resolved, fetchedFile);
27
+ return fetchedFile;
28
+ },
29
+ // From https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/bundler/index.js#L255C2-L271C5
30
+ resolveId: async (importee, importer) => {
31
+ if (debug) {
32
+ console.log("[transformSvelte] resolveId() " + importee);
33
+ console.log("importee: " + importee);
34
+ console.log("importer: " + importer);
35
+ }
36
+ // importing from Svelte
37
+ if (importee === `svelte`) {
38
+ return `${svelteURL}/src/runtime/index.js`;
39
+ }
40
+ if (importee.startsWith(`svelte/`)) {
41
+ const sub_path = importee.slice(7);
42
+ return `${svelteURL}/src/runtime/${sub_path}/index.js`;
43
+ }
44
+ // importing from a URL
45
+ if (/^https?:/.test(importee))
46
+ return importee;
47
+ // Relative imports
48
+ if (importee.startsWith(".")) {
49
+ if (importer && importer.startsWith(svelteURL)) {
50
+ const resolved = new URL(importee, importer).href;
51
+ const url = new URL(importee, importer).href;
52
+ if (debug) {
53
+ console.log("[transformSvelte] resolveId() " + resolved);
54
+ }
55
+ return resolved;
56
+ }
57
+ }
58
+ },
59
+ transform: async (code, id) => {
60
+ const { fileName } = parseId(id);
61
+ const isSvelte = fileName.endsWith(".svelte");
62
+ if (isSvelte) {
63
+ if (!compile) {
64
+ if (!getSvelteCompiler) {
65
+ throw new Error("Svelte compiler not available");
66
+ }
67
+ compile = await getSvelteCompiler();
68
+ }
69
+ const compiled = compile(code, {
70
+ filename: fileName,
71
+ hydratable: true,
72
+ });
73
+ return compiled.js;
74
+ }
75
+ return undefined;
76
+ },
77
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ import { computeBundleJSV3 } from "./computeBundleJSV3";
2
+ import { htmlTemplate } from "./htmlTemplate";
3
+ import { parseId } from "./parseId";
4
+ import { getFileText } from "@vizhub/viz-utils";
5
+ export const v3Build = async ({ files, rollup, enableSourcemap = true, vizCache, vizId, slugCache, getSvelteCompiler, }) => {
6
+ const { src, cssFiles } = await computeBundleJSV3({
7
+ files,
8
+ rollup,
9
+ enableSourcemap,
10
+ vizCache,
11
+ vizId,
12
+ slugCache,
13
+ getSvelteCompiler,
14
+ });
15
+ // Generate CSS styles from imported CSS files
16
+ let styles = "";
17
+ // Inject CSS files.
18
+ if (cssFiles.length > 0) {
19
+ for (let i = 0; i < cssFiles.length; i++) {
20
+ const id = cssFiles[i];
21
+ const indent = i > 0 ? " " : "\n ";
22
+ const styleElementId = "injected-style" + id;
23
+ const { vizId, fileName } = parseId(id);
24
+ const content = await vizCache.get(vizId);
25
+ const src = getFileText(content, fileName);
26
+ styles += `${indent}<style id="${styleElementId}">${src}</style>`;
27
+ }
28
+ }
29
+ return htmlTemplate({ cdn: "", src, styles });
30
+ };
@@ -0,0 +1,36 @@
1
+ // A cache of viz content.
2
+ // For use in resolving imports from other vizzes.
3
+ // Runs both on the server and in the browser.
4
+ export const createVizCache = ({ initialContents, handleCacheMiss, }) => {
5
+ // Track the content of cached vizzes.
6
+ const contentMap = new Map(initialContents.map((content) => [content.id, content]));
7
+ // Gets the content of a viz.
8
+ // Returns the cached content if it exists.
9
+ // Otherwise, calls handleCacheMiss to fetch the content.
10
+ const get = async (vizId) => {
11
+ const cachedContent = contentMap.get(vizId);
12
+ // Cache hit
13
+ if (cachedContent !== undefined) {
14
+ return cachedContent;
15
+ }
16
+ // Cache miss
17
+ if (!handleCacheMiss) {
18
+ throw new Error(`Unresolved import from vizId ${vizId}, cache miss handler not provided.`);
19
+ }
20
+ const freshContent = await handleCacheMiss(vizId);
21
+ if (freshContent) {
22
+ contentMap.set(vizId, freshContent);
23
+ return freshContent;
24
+ }
25
+ // TODO surface this error to the user
26
+ throw new Error(`Unresolved import from vizId ${vizId}`);
27
+ };
28
+ // Updates the content of a viz in the cache.
29
+ const set = (content) => {
30
+ contentMap.set(content.id, content);
31
+ };
32
+ const invalidate = (vizId) => {
33
+ contentMap.delete(vizId);
34
+ };
35
+ return { get, set, invalidate };
36
+ };
@@ -0,0 +1,98 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+ import { createVizCache } from "./vizCache";
3
+ import { sampleVizContent } from "../test/fixtures/v3";
4
+ describe("VizCache", () => {
5
+ describe("VizCache - get method", () => {
6
+ it("should return content from cache if available", async () => {
7
+ const vizCache = createVizCache({
8
+ initialContents: [sampleVizContent],
9
+ handleCacheMiss: vi.fn(),
10
+ });
11
+ const content = await vizCache.get(sampleVizContent.id);
12
+ expect(content).toEqual(sampleVizContent);
13
+ expect(vi.fn()).toHaveBeenCalledTimes(0); // handleCacheMiss should not be called
14
+ });
15
+ it("should fetch content on cache miss and store it", async () => {
16
+ const handleCacheMissMock = vi
17
+ .fn()
18
+ .mockResolvedValue(sampleVizContent);
19
+ const vizCache = createVizCache({
20
+ initialContents: [],
21
+ handleCacheMiss: handleCacheMissMock,
22
+ });
23
+ const content = await vizCache.get(sampleVizContent.id);
24
+ expect(handleCacheMissMock).toHaveBeenCalledWith(sampleVizContent.id);
25
+ expect(content).toEqual(sampleVizContent);
26
+ // Verify that the cache now contains the fetched content
27
+ const cachedContent = await vizCache.get(sampleVizContent.id);
28
+ expect(cachedContent).toEqual(sampleVizContent);
29
+ });
30
+ it("should throw an error if handleCacheMiss does not return content", async () => {
31
+ const handleCacheMissMock = vi
32
+ .fn()
33
+ .mockResolvedValue(undefined);
34
+ const vizCache = createVizCache({
35
+ initialContents: [],
36
+ handleCacheMiss: handleCacheMissMock,
37
+ });
38
+ await expect(vizCache.get("nonexistentId")).rejects.toThrow("Unresolved import from vizId nonexistentId");
39
+ });
40
+ // Add tests for set
41
+ });
42
+ describe("VizCache - set method", () => {
43
+ it("should add new content to the cache", async () => {
44
+ const vizCache = createVizCache({
45
+ initialContents: [],
46
+ handleCacheMiss: vi.fn(),
47
+ });
48
+ const newContent = {
49
+ id: "newContent",
50
+ files: {},
51
+ title: "New Content",
52
+ };
53
+ vizCache.set(newContent);
54
+ // Verify new content is added
55
+ const content = await vizCache.get(newContent.id);
56
+ expect(content).toEqual(newContent);
57
+ });
58
+ it("should update existing content in the cache", async () => {
59
+ const updatedContent = {
60
+ ...sampleVizContent,
61
+ title: "Updated Content Title",
62
+ };
63
+ const vizCache = createVizCache({
64
+ initialContents: [sampleVizContent],
65
+ handleCacheMiss: vi.fn(),
66
+ });
67
+ // Update existing content
68
+ vizCache.set(updatedContent);
69
+ // Verify content is updated
70
+ const content = await vizCache.get(updatedContent.id);
71
+ expect(content).toEqual(updatedContent);
72
+ });
73
+ it("should keep the cache consistent after multiple set operations", async () => {
74
+ const vizCache = createVizCache({
75
+ initialContents: [],
76
+ handleCacheMiss: vi.fn(),
77
+ });
78
+ // Adding multiple contents
79
+ const contentA = {
80
+ id: "contentA",
81
+ files: {},
82
+ title: "Content A",
83
+ };
84
+ const contentB = {
85
+ id: "contentB",
86
+ files: {},
87
+ title: "Content B",
88
+ };
89
+ vizCache.set(contentA);
90
+ vizCache.set(contentB);
91
+ // Verify both contents are retrievable
92
+ const retrievedA = await vizCache.get(contentA.id);
93
+ const retrievedB = await vizCache.get(contentB.id);
94
+ expect(retrievedA).toEqual(contentA);
95
+ expect(retrievedB).toEqual(contentB);
96
+ });
97
+ });
98
+ });
@@ -0,0 +1,46 @@
1
+ import { parseId } from "./parseId";
2
+ import { getFileText } from "@vizhub/viz-utils";
3
+ const debug = false;
4
+ // Responsible for loading all imports and
5
+ // tracking which CSS files are imported.
6
+ // Throws an error if a file is imported but not found.
7
+ export const vizLoad = ({ vizCache, trackCSSImport, }) => ({
8
+ name: "vizLoad",
9
+ // `id` here is of the form
10
+ // `{vizId}/{fileName}`
11
+ load: async (id) => {
12
+ if (debug) {
13
+ console.log("[vizLoadCSS]: load() " + id);
14
+ }
15
+ const { vizId, fileName } = parseId(id);
16
+ if (debug) {
17
+ console.log(" [vizLoadCSS] vizId: " + vizId);
18
+ console.log(" [vizLoadCSS] fileName: " + fileName);
19
+ }
20
+ // For CSS imports, all we need to do here is
21
+ // keep track of them so they can be injected
22
+ // into the IFrame later.
23
+ if (fileName.endsWith(".css")) {
24
+ if (debug) {
25
+ console.log(" [vizResolve] tracking CSS import for " + id);
26
+ }
27
+ // The import is tracked here so that it can be
28
+ // injected into the IFrame later, external to the
29
+ // Rollup build.
30
+ trackCSSImport(id);
31
+ // TODO consider using Rollup's `emitFile` to emit a CSS file.
32
+ return "";
33
+ }
34
+ const content = await vizCache.get(vizId);
35
+ const fileText = getFileText(content, fileName);
36
+ // If a file is imported but not found, throw an error.
37
+ if (fileText === null) {
38
+ throw new Error(`Imported file "${fileName}" not found.`);
39
+ // TODO ideally show username/slug instead of vizId
40
+ // `Imported file "${fileName}" not found in viz ${vizId}`,
41
+ // `Imported file "${fileName}" not found.`,
42
+ // );
43
+ }
44
+ return fileText;
45
+ },
46
+ });
@@ -0,0 +1,69 @@
1
+ import { extractVizImport } from "./extractVizImport";
2
+ import { parseId } from "./parseId";
3
+ import { isVizId } from "@vizhub/viz-utils";
4
+ const debug = false;
5
+ export const vizResolve = ({ vizId, slugCache, }) => ({
6
+ name: "vizResolve",
7
+ resolveId: async (id, importer) => {
8
+ if (debug) {
9
+ console.log("[vizIdResolve] resolveId() " + id);
10
+ console.log(" importer: " + importer);
11
+ }
12
+ // Handle virtual file system resolution
13
+ // .e.g. `import { foo } from './foo.js'`
14
+ // .e.g. `import { foo } from './foo'`
15
+ if (id.startsWith("./") &&
16
+ !importer?.startsWith("https://")) {
17
+ let fileName = id.substring(2);
18
+ // Handle CSS files
19
+ // e.g. `import './styles.css'`
20
+ // Handle JS files
21
+ // e.g. `import { foo } from './foo.js'`
22
+ // e.g. `import { foo } from './foo'`
23
+ if (!fileName.endsWith(".js") &&
24
+ !fileName.endsWith(".css") &&
25
+ !fileName.endsWith(".csv") &&
26
+ !fileName.endsWith(".svelte")) {
27
+ fileName += ".js";
28
+ }
29
+ // const js = (name: string) =>
30
+ // name.endsWith('.js') ? name : name + '.js';
31
+ // If there is an importer, then the file is not part of
32
+ // the entry point, so it should be resolved relative
33
+ // to the importer's directory
34
+ if (importer) {
35
+ const { vizId: importerVizId, fileName: importerFileName, } = parseId(importer);
36
+ // Get the directory of the importing file
37
+ const importerDir = importerFileName
38
+ .split("/")
39
+ .slice(0, -1)
40
+ .join("/");
41
+ // Combine the directory with the imported file name
42
+ const resolvedFileName = importerDir
43
+ ? `${importerDir}/${fileName}`
44
+ : fileName;
45
+ return `${importerVizId}/${resolvedFileName}`;
46
+ }
47
+ return vizId + "/" + fileName;
48
+ }
49
+ // Handle viz import resolution
50
+ // e.g. `import { foo } from '@curran/98e6d6509a1e407897d4f238a330efec'`
51
+ // e.g. `import { foo } from '@curran/scatter-plot'`
52
+ const vizImport = extractVizImport(id);
53
+ if (vizImport) {
54
+ let vizId;
55
+ if (isVizId(vizImport.idOrSlug)) {
56
+ vizId = vizImport.idOrSlug;
57
+ }
58
+ else {
59
+ if (!slugCache) {
60
+ throw new Error("slugCache is required to import by slug in v3 runtime");
61
+ }
62
+ vizId = await slugCache.get(`${vizImport.userName}/${vizImport.idOrSlug}`);
63
+ }
64
+ return vizId + "/index.js";
65
+ }
66
+ // If neither condition is met, return undefined.
67
+ return undefined;
68
+ },
69
+ });
@@ -0,0 +1,134 @@
1
+ import { JSDOM } from "jsdom";
2
+ import { virtualFileSystem } from "../common/virtualFileSystem";
3
+ import { sucrasePlugin } from "../common/sucrasePlugin";
4
+ import { dependencies, getConfiguredLibraries, dependencySource, } from "../common/packageJson";
5
+ const DEBUG = false;
6
+ /**
7
+ * Extract module script entry points from index.html
8
+ */
9
+ const extractModuleEntryPoints = (html) => {
10
+ const dom = new JSDOM(html);
11
+ const document = dom.window.document;
12
+ const moduleScripts = document.querySelectorAll('script[type="module"]');
13
+ const entryPoints = [];
14
+ moduleScripts.forEach((script) => {
15
+ const src = script.getAttribute("src");
16
+ if (src) {
17
+ entryPoints.push(src);
18
+ }
19
+ });
20
+ return entryPoints;
21
+ };
22
+ /**
23
+ * Bundle an ES module entry point
24
+ */
25
+ const bundleESModule = async ({ entryPoint, files, rollup, enableSourcemap = true, }) => {
26
+ const inputOptions = {
27
+ input: `./${entryPoint}`,
28
+ plugins: [virtualFileSystem(files), sucrasePlugin()],
29
+ onwarn(warning, warn) {
30
+ // Suppress "treating module as external dependency" warnings
31
+ if (warning.code === "UNRESOLVED_IMPORT")
32
+ return;
33
+ warn(warning);
34
+ },
35
+ };
36
+ const bundle = await rollup(inputOptions);
37
+ const outputOptions = {
38
+ format: "es",
39
+ sourcemap: enableSourcemap,
40
+ };
41
+ const { output } = await bundle.generate(outputOptions);
42
+ return output[0].code;
43
+ };
44
+ /**
45
+ * Generate an import map for all dependencies
46
+ */
47
+ const generateImportMap = (files) => {
48
+ const deps = dependencies(files);
49
+ if (Object.keys(deps).length === 0) {
50
+ return null;
51
+ }
52
+ const libraries = getConfiguredLibraries(files);
53
+ const imports = {};
54
+ Object.entries(deps).forEach(([name, version]) => {
55
+ const url = dependencySource({ name, version }, libraries);
56
+ imports[name] = url;
57
+ });
58
+ return JSON.stringify({ imports }, null, 2);
59
+ };
60
+ /**
61
+ * Update HTML to include import map and bundled modules
62
+ */
63
+ const updateHTML = (files, bundledModules) => {
64
+ const html = files["index.html"] || "";
65
+ if (!html) {
66
+ return "";
67
+ }
68
+ const dom = new JSDOM(html);
69
+ const document = dom.window.document;
70
+ // Replace script src with bundled content
71
+ bundledModules.forEach((code, src) => {
72
+ const scripts = document.querySelectorAll(`script[src="${src}"]`);
73
+ scripts.forEach((script) => {
74
+ const newScript = document.createElement("script");
75
+ newScript.setAttribute("type", "module");
76
+ newScript.textContent = code;
77
+ script.parentNode?.replaceChild(newScript, script);
78
+ });
79
+ });
80
+ // Add import map if needed
81
+ const importMap = generateImportMap(files);
82
+ if (importMap) {
83
+ // Check if import map already exists
84
+ const existingImportMap = document.querySelector('script[type="importmap"]');
85
+ if (!existingImportMap) {
86
+ const importMapScript = document.createElement("script");
87
+ importMapScript.setAttribute("type", "importmap");
88
+ importMapScript.textContent = importMap;
89
+ const head = document.head || document.querySelector("head");
90
+ if (head) {
91
+ head.prepend(importMapScript);
92
+ }
93
+ else {
94
+ const html = document.documentElement;
95
+ const newHead = document.createElement("head");
96
+ newHead.appendChild(importMapScript);
97
+ html.insertBefore(newHead, html.firstChild);
98
+ }
99
+ }
100
+ }
101
+ return `<!DOCTYPE html>${document.documentElement.outerHTML}`;
102
+ };
103
+ /**
104
+ * Build for v4 runtime
105
+ */
106
+ export const v4Build = async ({ files, rollup, enableSourcemap = true, }) => {
107
+ // Extract entry points
108
+ const html = files["index.html"] || "";
109
+ const entryPoints = extractModuleEntryPoints(html);
110
+ if (entryPoints.length === 0) {
111
+ DEBUG &&
112
+ console.log("[v4Build] No module entry points found");
113
+ // Fall back to a default approach if no module entry points
114
+ // Just return the HTML as-is if there are no module scripts
115
+ return files;
116
+ }
117
+ // Bundle each entry point
118
+ const bundledModules = new Map();
119
+ for (const entryPoint of entryPoints) {
120
+ const code = await bundleESModule({
121
+ entryPoint,
122
+ files,
123
+ rollup,
124
+ enableSourcemap,
125
+ });
126
+ bundledModules.set(entryPoint, code);
127
+ }
128
+ // Update HTML with bundled modules and import map
129
+ const updatedHTML = updateHTML(files, bundledModules);
130
+ return {
131
+ ...files,
132
+ "index.html": updatedHTML,
133
+ };
134
+ };
@@ -0,0 +1,14 @@
1
+ import { FileCollection } from "@vizhub/viz-types";
2
+ import type { RollupBuild, RollupOptions } from "rollup";
3
+ import { VizCache } from "./v3/vizCache";
4
+ import { SlugCache } from "./v3/slugCache";
5
+ import { SvelteCompiler } from "./v3/transformSvelte";
6
+ export declare const buildHTML: ({ files, rollup, enableSourcemap, vizCache, vizId, slugCache, getSvelteCompiler, }: {
7
+ files?: FileCollection;
8
+ rollup?: (options: RollupOptions) => Promise<RollupBuild>;
9
+ enableSourcemap?: boolean;
10
+ vizCache?: VizCache;
11
+ vizId?: string;
12
+ slugCache?: SlugCache;
13
+ getSvelteCompiler?: () => Promise<SvelteCompiler>;
14
+ }) => Promise<string>;
@@ -0,0 +1,39 @@
1
+ import { FileCollection } from "@vizhub/viz-types";
2
+ export type Licence = string;
3
+ export interface PackageJson {
4
+ dependencies?: {
5
+ [key: string]: string;
6
+ };
7
+ vizhub?: {
8
+ libraries?: {
9
+ [key: string]: VizHubLibraryConfig;
10
+ };
11
+ };
12
+ license?: Licence | {
13
+ type: string;
14
+ };
15
+ }
16
+ export interface Dependency {
17
+ name: string;
18
+ version: string;
19
+ }
20
+ export interface VizHubLibraryConfig {
21
+ path?: string;
22
+ global?: string;
23
+ }
24
+ export interface VizHubLibraryConfigs {
25
+ [key: string]: VizHubLibraryConfig;
26
+ }
27
+ export declare const packageJSON: (files: FileCollection) => PackageJson;
28
+ export declare const dependencies: (files: FileCollection) => {
29
+ [key: string]: string;
30
+ };
31
+ export declare const getConfiguredLibraries: (files: FileCollection) => {
32
+ [key: string]: VizHubLibraryConfig;
33
+ };
34
+ export declare const dependencySource: ({ name, version }: Dependency, libraries: VizHubLibraryConfigs, cdn?: string) => string;
35
+ export declare const getLicense: (files: FileCollection) => string | {
36
+ type: string;
37
+ } | undefined;
38
+ export declare const getConfiguredGlobals: (pkg: PackageJson) => Record<string, string>;
39
+ export declare const getGlobals: (pkg: PackageJson) => Record<string, string>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { Plugin } from "rollup";
2
+ export declare function sucrasePlugin(): Plugin;
@@ -0,0 +1,3 @@
1
+ import { FileCollection } from "@vizhub/viz-types";
2
+ import { Plugin } from "rollup";
3
+ export declare const virtualFileSystem: (files: FileCollection) => Plugin;
@@ -0,0 +1,3 @@
1
+ import { FileCollection } from "@vizhub/viz-types";
2
+ import { runtimeVersion } from "./types";
3
+ export declare const determineRuntimeVersion: (files: FileCollection) => runtimeVersion | null;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export { buildHTML } from "./buildHTML.js";
2
+ export { createVizCache, VizCache } from "./v3/vizCache.js";
3
+ export { createSlugCache, SlugCache, } from "./v3/slugCache.js";
@@ -0,0 +1,3 @@
1
+ export declare const basicHTML: {
2
+ "index.html": string;
3
+ };
@@ -0,0 +1,5 @@
1
+ export declare const fetchProxy: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ "data.csv": string;
5
+ };
@@ -0,0 +1,6 @@
1
+ export { basicHTML } from "./basicHTML";
2
+ export { jsScriptTag } from "./jsScriptTag";
3
+ export { fetchProxy } from "./fetchProxy";
4
+ export { styleTest } from "./styleTest";
5
+ export { xmlTest } from "./xmlTest";
6
+ export { protocolTest } from "./protocolTest";
@@ -0,0 +1,4 @@
1
+ export declare const jsScriptTag: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ };
@@ -0,0 +1,3 @@
1
+ export declare const protocolTest: {
2
+ "index.html": string;
3
+ };
@@ -0,0 +1,4 @@
1
+ export declare const styleTest: {
2
+ "index.html": string;
3
+ "styles.css": string;
4
+ };
@@ -0,0 +1,4 @@
1
+ export declare const xmlTest: {
2
+ "index.html": string;
3
+ "data.xml": string;
4
+ };
@@ -0,0 +1,5 @@
1
+ export declare const basicBundle: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ "foo.js": string;
5
+ };
@@ -0,0 +1,5 @@
1
+ export declare const basicBundleNoExtension: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ "foo.js": string;
5
+ };
@@ -0,0 +1,4 @@
1
+ export declare const d3Import: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ };
@@ -0,0 +1,5 @@
1
+ export declare const d3ImportPkg: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ "package.json": string;
5
+ };
@@ -0,0 +1,5 @@
1
+ export declare const d3RosettaImportPkg: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ "package.json": string;
5
+ };
@@ -0,0 +1,4 @@
1
+ export declare const es6Preserve: {
2
+ "index.html": string;
3
+ "index.js": string;
4
+ };