@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,71 @@
1
+ const EMPTY_PKG_JSON = {
2
+ dependencies: {},
3
+ vizhub: {},
4
+ license: "MIT",
5
+ };
6
+ const DEBUG = false;
7
+ export const packageJSON = (files) => {
8
+ const packageJsonText = files["package.json"];
9
+ DEBUG &&
10
+ console.log("[packageJSON] packageJsonText:", packageJsonText);
11
+ try {
12
+ const pkg = packageJsonText
13
+ ? JSON.parse(packageJsonText)
14
+ : EMPTY_PKG_JSON;
15
+ DEBUG &&
16
+ console.log("[packageJSON] pkg:", JSON.stringify(pkg, null, 2));
17
+ return pkg;
18
+ }
19
+ catch {
20
+ DEBUG &&
21
+ console.log("[packageJSON] Error parsing package.json");
22
+ return EMPTY_PKG_JSON;
23
+ }
24
+ };
25
+ export const dependencies = (files) => packageJSON(files).dependencies || {};
26
+ export const getConfiguredLibraries = (files) => {
27
+ const vizhubConfig = packageJSON(files).vizhub || {};
28
+ return vizhubConfig.libraries || {};
29
+ };
30
+ export const dependencySource = ({ name, version }, libraries, cdn = "jsdelivr") => {
31
+ const path = libraries[name]
32
+ ? libraries[name].path || ""
33
+ : "";
34
+ const src = cdn === "jsdelivr"
35
+ ? `https://cdn.jsdelivr.net/npm/${name}@${version}${path}`
36
+ : `https://unpkg.com/${name}@${version}${path}`;
37
+ return src;
38
+ };
39
+ export const getLicense = (files) => {
40
+ const license = packageJSON(files).license;
41
+ if (typeof license === "object" &&
42
+ license !== null &&
43
+ "type" in license) {
44
+ return license.type;
45
+ }
46
+ return license || EMPTY_PKG_JSON.license;
47
+ };
48
+ export const getConfiguredGlobals = (pkg) => {
49
+ const libraries = pkg?.vizhub?.libraries;
50
+ if (libraries) {
51
+ return Object.entries(libraries).reduce((accumulator, [packageName, config]) => {
52
+ if (config.global) {
53
+ accumulator[packageName] = config.global;
54
+ }
55
+ return accumulator;
56
+ }, {});
57
+ }
58
+ return {};
59
+ };
60
+ const defaultGlobals = {
61
+ d3: "d3",
62
+ react: "React",
63
+ "react-dom": "ReactDOM",
64
+ };
65
+ export const getGlobals = (pkg) => {
66
+ const configuredGlobals = getConfiguredGlobals(pkg);
67
+ return {
68
+ ...defaultGlobals,
69
+ ...configuredGlobals,
70
+ };
71
+ };
@@ -0,0 +1,146 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { packageJSON, dependencies, getConfiguredLibraries, dependencySource, getLicense, } from "./packageJson";
3
+ describe("packageJson", () => {
4
+ const mockFiles = {
5
+ "package.json": JSON.stringify({
6
+ dependencies: {
7
+ react: "17.0.2",
8
+ d3: "7.0.0",
9
+ },
10
+ vizhub: {
11
+ libraries: {
12
+ d3: { path: "/dist/d3.min.js" },
13
+ },
14
+ },
15
+ license: "Apache-2.0",
16
+ }),
17
+ };
18
+ const emptyFiles = {};
19
+ const invalidFiles = {
20
+ "package.json": "invalid json",
21
+ };
22
+ describe("packageJSON", () => {
23
+ it("should parse valid package.json", () => {
24
+ const result = packageJSON(mockFiles);
25
+ expect(result).toHaveProperty("dependencies");
26
+ expect(result.dependencies).toHaveProperty("react", "17.0.2");
27
+ });
28
+ it("should return empty object for missing package.json", () => {
29
+ const result = packageJSON(emptyFiles);
30
+ expect(result).toEqual({
31
+ dependencies: {},
32
+ vizhub: {},
33
+ license: "MIT",
34
+ });
35
+ });
36
+ it("should handle invalid JSON", () => {
37
+ const result = packageJSON(invalidFiles);
38
+ expect(result).toEqual({
39
+ dependencies: {},
40
+ vizhub: {},
41
+ license: "MIT",
42
+ });
43
+ });
44
+ });
45
+ describe("dependencies", () => {
46
+ it("should return dependencies from package.json", () => {
47
+ const result = dependencies(mockFiles);
48
+ expect(result).toEqual({
49
+ react: "17.0.2",
50
+ d3: "7.0.0",
51
+ });
52
+ });
53
+ it("should return empty object when no dependencies", () => {
54
+ const result = dependencies(emptyFiles);
55
+ expect(result).toEqual({});
56
+ });
57
+ });
58
+ describe("getConfiguredLibraries", () => {
59
+ it("should return configured libraries", () => {
60
+ const result = getConfiguredLibraries(mockFiles);
61
+ expect(result).toEqual({
62
+ d3: { path: "/dist/d3.min.js" },
63
+ });
64
+ });
65
+ it("should return empty object when no libraries configured", () => {
66
+ const result = getConfiguredLibraries(emptyFiles);
67
+ expect(result).toEqual({});
68
+ });
69
+ });
70
+ describe("dependencySource", () => {
71
+ it("should generate correct URL with path - jsdelivr", () => {
72
+ const dependency = { name: "d3", version: "7.0.0" };
73
+ const libraries = { d3: { path: "/dist/d3.min.js" } };
74
+ const result = dependencySource(dependency, libraries);
75
+ expect(result).toBe("https://cdn.jsdelivr.net/npm/d3@7.0.0/dist/d3.min.js");
76
+ });
77
+ it("should generate correct URL without path - jsdelivr", () => {
78
+ const dependency = {
79
+ name: "react",
80
+ version: "17.0.2",
81
+ };
82
+ const libraries = {};
83
+ const result = dependencySource(dependency, libraries);
84
+ expect(result).toBe("https://cdn.jsdelivr.net/npm/react@17.0.2");
85
+ });
86
+ it("should generate correct URL with path - unpkg", () => {
87
+ const dependency = { name: "d3", version: "7.0.0" };
88
+ const libraries = { d3: { path: "/dist/d3.min.js" } };
89
+ const result = dependencySource(dependency, libraries, "unpkg");
90
+ expect(result).toBe("https://unpkg.com/d3@7.0.0/dist/d3.min.js");
91
+ });
92
+ it("should generate correct URL without path - unpkg", () => {
93
+ const dependency = {
94
+ name: "react",
95
+ version: "17.0.2",
96
+ };
97
+ const libraries = {};
98
+ const result = dependencySource(dependency, libraries, "unpkg");
99
+ expect(result).toBe("https://unpkg.com/react@17.0.2");
100
+ });
101
+ });
102
+ describe("getLicense", () => {
103
+ it("should return license from package.json", () => {
104
+ const result = getLicense(mockFiles);
105
+ expect(result).toBe("Apache-2.0");
106
+ });
107
+ it("should return default MIT license when not specified", () => {
108
+ const result = getLicense(emptyFiles);
109
+ expect(result).toBe("MIT");
110
+ });
111
+ });
112
+ describe("Edge cases", () => {
113
+ it("should handle malformed library configuration", () => {
114
+ const malformedFiles = {
115
+ "package.json": JSON.stringify({
116
+ dependencies: { d3: "7.0.0" },
117
+ vizhub: { libraries: { d3: null } },
118
+ }),
119
+ };
120
+ const result = getConfiguredLibraries(malformedFiles);
121
+ expect(result).toEqual({ d3: null });
122
+ });
123
+ it("should handle empty version strings", () => {
124
+ const dependency = { name: "react", version: "" };
125
+ const result = dependencySource(dependency, {});
126
+ expect(result).toBe("https://cdn.jsdelivr.net/npm/react@");
127
+ });
128
+ it("should handle special characters in dependency names", () => {
129
+ const specialCharDep = {
130
+ name: "@angular/core",
131
+ version: "14.0.0",
132
+ };
133
+ const result = dependencySource(specialCharDep, {});
134
+ expect(result).toBe("https://cdn.jsdelivr.net/npm/@angular/core@14.0.0");
135
+ });
136
+ it("should handle unusual license formats", () => {
137
+ const unusualLicenseFiles = {
138
+ "package.json": JSON.stringify({
139
+ license: { type: "MIT" },
140
+ }),
141
+ };
142
+ const result = getLicense(unusualLicenseFiles);
143
+ expect(result).toBe("MIT");
144
+ });
145
+ });
146
+ });
@@ -0,0 +1,24 @@
1
+ import { transform } from "sucrase";
2
+ export function sucrasePlugin() {
3
+ return {
4
+ name: "sucrase",
5
+ transform(code, id) {
6
+ // Transform .js, .jsx, and .tsx files
7
+ if (!id.match(/\.(?:js|[jt]sx)$/)) {
8
+ return null;
9
+ }
10
+ const result = transform(code, {
11
+ transforms: ["jsx", "typescript"],
12
+ filePath: id, // For source maps
13
+ sourceMapOptions: {
14
+ compiledFilename: "bundle.js",
15
+ },
16
+ production: true,
17
+ });
18
+ return {
19
+ code: result.code,
20
+ map: result.sourceMap,
21
+ };
22
+ },
23
+ };
24
+ }
@@ -0,0 +1,75 @@
1
+ const normalizePath = (filepath) => {
2
+ // Remove leading ./
3
+ let normalized = filepath.replace(/^\.\//, "");
4
+ // Replace multiple slashes with single slash
5
+ normalized = normalized.replace(/\/+/g, "/");
6
+ // Remove trailing slash
7
+ normalized = normalized.replace(/\/$/, "");
8
+ return normalized;
9
+ };
10
+ const joinPaths = (base, relative) => {
11
+ // Get directory name of base path
12
+ const baseDir = base.includes("/")
13
+ ? base.slice(0, base.lastIndexOf("/"))
14
+ : "";
15
+ // Handle .. by removing one directory level
16
+ const parts = relative.split("/");
17
+ const resultParts = baseDir
18
+ ? baseDir.split("/")
19
+ : [];
20
+ for (const part of parts) {
21
+ if (part === "..") {
22
+ resultParts.pop();
23
+ }
24
+ else if (part !== "." && part !== "") {
25
+ resultParts.push(part);
26
+ }
27
+ }
28
+ return resultParts.join("/");
29
+ };
30
+ export const virtualFileSystem = (files) => {
31
+ return {
32
+ name: "virtual-file-system",
33
+ resolveId(source, importer) {
34
+ // Handle relative paths
35
+ if (source.startsWith("./") ||
36
+ source.startsWith("../")) {
37
+ const resolvedPath = importer
38
+ ? normalizePath(joinPaths(importer, source))
39
+ : normalizePath(source);
40
+ // Try exact match first
41
+ if (files[resolvedPath]) {
42
+ return resolvedPath;
43
+ }
44
+ // Try with extensions if no exact match is found
45
+ const extensions = [".js", ".jsx", ".ts", ".tsx"];
46
+ for (const ext of extensions) {
47
+ const pathWithExt = resolvedPath + ext;
48
+ if (files[pathWithExt]) {
49
+ return pathWithExt;
50
+ }
51
+ }
52
+ }
53
+ // Handle bare module imports
54
+ if (files[source]) {
55
+ return source;
56
+ }
57
+ // Try bare imports with extensions
58
+ const extensions = [".js", ".jsx", ".ts", ".tsx"];
59
+ for (const ext of extensions) {
60
+ const pathWithExt = source + ext;
61
+ if (files[pathWithExt]) {
62
+ return pathWithExt;
63
+ }
64
+ }
65
+ return null;
66
+ },
67
+ load(id) {
68
+ // Return the contents if the file exists in our virtual system
69
+ if (files[id]) {
70
+ return files[id];
71
+ }
72
+ return null;
73
+ },
74
+ };
75
+ };
@@ -0,0 +1,22 @@
1
+ export const determineRuntimeVersion = (files) => {
2
+ if (Object.keys(files).length === 0) {
3
+ return null;
4
+ }
5
+ const hasIndexHTML = "index.html" in files;
6
+ const hasIndexJS = "index.js" in files;
7
+ const hasIndexJSX = "index.jsx" in files;
8
+ if (hasIndexHTML) {
9
+ const hasModuleScript = files["index.html"].includes('type="module"');
10
+ if (hasModuleScript) {
11
+ return "v4";
12
+ }
13
+ if (hasIndexJS || hasIndexJSX) {
14
+ return "v2";
15
+ }
16
+ return "v1";
17
+ }
18
+ if (!hasIndexHTML && hasIndexJS) {
19
+ return "v3";
20
+ }
21
+ return null;
22
+ };
@@ -0,0 +1,101 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { determineRuntimeVersion } from "./determineRuntimeVersion";
3
+ import { jsScriptTagTypeModule, jsScriptTagTypeModules, fetchInterception, esmBuild, } from "./test/fixtures/v4";
4
+ describe("determineRuntimeVersion", () => {
5
+ it("should return null when there are no files", () => {
6
+ const mockFiles = {};
7
+ const result = determineRuntimeVersion(mockFiles);
8
+ expect(result).toBe(null);
9
+ });
10
+ it("should return v1 when only index.html exists", () => {
11
+ const mockFiles = {
12
+ "index.html": "<html></html>",
13
+ };
14
+ const result = determineRuntimeVersion(mockFiles);
15
+ expect(result).toBe("v1");
16
+ });
17
+ it("should return v2 when both index.html and index.js exist", () => {
18
+ const mockFiles = {
19
+ "index.html": "<html></html>",
20
+ "index.js": 'console.log("Hello");',
21
+ };
22
+ const result = determineRuntimeVersion(mockFiles);
23
+ expect(result).toBe("v2");
24
+ });
25
+ it("should return v2 when both index.html and index.jsx exist", () => {
26
+ const mockFiles = {
27
+ "index.html": "<html></html>",
28
+ "index.jsx": 'console.log("Hello");',
29
+ };
30
+ const result = determineRuntimeVersion(mockFiles);
31
+ expect(result).toBe("v2");
32
+ });
33
+ it("should return v3 when only index.js exists", () => {
34
+ const mockFiles = {
35
+ "index.js": 'console.log("Hello");',
36
+ };
37
+ const result = determineRuntimeVersion(mockFiles);
38
+ expect(result).toBe("v3");
39
+ });
40
+ it("should return null when only index.jsx exists (no v3 support for JSX)", () => {
41
+ const mockFiles = {
42
+ "index.jsx": 'console.log("Hello");',
43
+ };
44
+ const result = determineRuntimeVersion(mockFiles);
45
+ expect(result).toBe(null);
46
+ });
47
+ it("should return v4 when index.html contains script type=module with importmap", () => {
48
+ const mockFiles = {
49
+ "index.html": `
50
+ <!DOCTYPE html>
51
+ <html>
52
+ <head>
53
+ <script type="importmap">
54
+ { "imports": { "test": "./test.js" } }
55
+ </script>
56
+ </head>
57
+ <body>
58
+ <script type="module" src="index.js"></script>
59
+ </body>
60
+ </html>
61
+ `,
62
+ "index.js": 'import { hello } from "test"; console.log(hello);',
63
+ "test.js": 'export const hello = "world";',
64
+ };
65
+ const result = determineRuntimeVersion(mockFiles);
66
+ expect(result).toBe("v4");
67
+ });
68
+ it("should return v4 when multiple module scripts are present", () => {
69
+ const mockFiles = {
70
+ "index.html": `
71
+ <!DOCTYPE html>
72
+ <html>
73
+ <body>
74
+ <script type="module" src="index.js"></script>
75
+ </body>
76
+ </html>
77
+ `,
78
+ "index.js": 'import { hello } from "./utils.js"; console.log(hello);',
79
+ "utils.js": 'export const hello = "world";',
80
+ };
81
+ const result = determineRuntimeVersion(mockFiles);
82
+ expect(result).toBe("v4");
83
+ });
84
+ // Test v4 fixtures
85
+ it("should detect v4 for jsScriptTagTypeModule fixture", () => {
86
+ const result = determineRuntimeVersion(jsScriptTagTypeModule);
87
+ expect(result).toBe("v4");
88
+ });
89
+ it("should detect v4 for jsScriptTagTypeModules fixture", () => {
90
+ const result = determineRuntimeVersion(jsScriptTagTypeModules);
91
+ expect(result).toBe("v4");
92
+ });
93
+ it("should detect v4 for fetchInterception fixture", () => {
94
+ const result = determineRuntimeVersion(fetchInterception);
95
+ expect(result).toBe("v4");
96
+ });
97
+ it("should detect v4 for esmBuild fixture", () => {
98
+ const result = determineRuntimeVersion(esmBuild);
99
+ expect(result).toBe("v4");
100
+ });
101
+ });
@@ -0,0 +1,3 @@
1
+ export { buildHTML } from "./buildHTML.js";
2
+ export { createVizCache } from "./v3/vizCache.js";
3
+ export { createSlugCache, } from "./v3/slugCache.js";
@@ -0,0 +1,15 @@
1
+ export const basicHTML = {
2
+ "index.html": `<!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <title>My HTML Document</title>
6
+ </head>
7
+ <body>
8
+ <h1>Hello, World!</h1>
9
+ <p>This is my first HTML document.</p>
10
+ <script>
11
+ console.log('Hello, World!');
12
+ </script>
13
+ </body>
14
+ </html>`,
15
+ };
@@ -0,0 +1,12 @@
1
+ export const fetchProxy = {
2
+ "index.html": `<!DOCTYPE html>
3
+ <html>
4
+ <body>
5
+ <script src="index.js"></script>
6
+ </body>
7
+ </html>`,
8
+ "index.js": `fetch("data.csv")
9
+ .then((response) => response.text())
10
+ .then(console.log);`,
11
+ "data.csv": `Hello, Fetch!`,
12
+ };
@@ -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,9 @@
1
+ export const jsScriptTag = {
2
+ "index.html": `<!DOCTYPE html>
3
+ <html>
4
+ <body>
5
+ <script src="index.js"></script>
6
+ </body>
7
+ </html>`,
8
+ "index.js": `console.log('Hello, JS!');`,
9
+ };
@@ -0,0 +1,15 @@
1
+ export const protocolTest = {
2
+ "index.html": `<!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <link href="//fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
6
+ <script src="//code.jquery.com/jquery-3.6.0.min.js"></script>
7
+ </head>
8
+ <body>
9
+ <div>Protocol Test</div>
10
+ <script>
11
+ console.log('Protocol test loaded');
12
+ </script>
13
+ </body>
14
+ </html>`,
15
+ };
@@ -0,0 +1,15 @@
1
+ export const styleTest = {
2
+ "index.html": `<!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <link rel="stylesheet" href="styles.css">
6
+ </head>
7
+ <body>
8
+ <div id="test">Test</div>
9
+ <script>
10
+ console.log(window.getComputedStyle(document.getElementById('test')).color);
11
+ </script>
12
+ </body>
13
+ </html>`,
14
+ "styles.css": `#test { color: rgb(255, 0, 0); }`,
15
+ };
@@ -0,0 +1,19 @@
1
+ export const xmlTest = {
2
+ "index.html": `<!DOCTYPE html>
3
+ <html>
4
+ <body>
5
+ <script>
6
+ const xhr = new XMLHttpRequest();
7
+ xhr.open('GET', 'data.xml');
8
+ xhr.onload = () => {
9
+ console.log(xhr.responseXML.documentElement.nodeName);
10
+ };
11
+ xhr.send();
12
+ </script>
13
+ </body>
14
+ </html>`,
15
+ "data.xml": `<?xml version="1.0" encoding="UTF-8"?>
16
+ <root>
17
+ <item>Test</item>
18
+ </root>`,
19
+ };
@@ -0,0 +1,5 @@
1
+ export const basicBundle = {
2
+ "index.html": "<script src='bundle.js'></script>",
3
+ "index.js": 'import { foo } from "./foo.js"; console.log(foo);',
4
+ "foo.js": 'export const foo = "bar";',
5
+ };
@@ -0,0 +1,5 @@
1
+ export const basicBundleNoExtension = {
2
+ "index.html": "<script src='bundle.js'></script>",
3
+ "index.js": 'import { foo } from "./foo"; console.log(foo);',
4
+ "foo.js": 'export const foo = "bar";',
5
+ };
@@ -0,0 +1,7 @@
1
+ export const d3Import = {
2
+ "index.html": `
3
+ <script src='https://cdn.jsdelivr.net/npm/d3@6.7.0/dist/d3.min.js'></script>
4
+ <script src='bundle.js'></script>
5
+ `,
6
+ "index.js": 'import { select } from "d3"; console.log(typeof select);',
7
+ };
@@ -0,0 +1,19 @@
1
+ export const d3ImportPkg = {
2
+ "index.html": `
3
+ <script src='bundle.js'></script>
4
+ `,
5
+ "index.js": 'import { select } from "d3"; console.log(typeof select);',
6
+ "package.json": `{
7
+ "dependencies": {
8
+ "d3": "6.7.0"
9
+ },
10
+ "vizhub": {
11
+ "libraries": {
12
+ "d3": {
13
+ "global": "d3",
14
+ "path": "/dist/d3.min.js"
15
+ }
16
+ }
17
+ }
18
+ }`,
19
+ };
@@ -0,0 +1,19 @@
1
+ export const d3RosettaImportPkg = {
2
+ "index.html": `
3
+ <script src='bundle.js'></script>
4
+ `,
5
+ "index.js": 'import { one } from "d3-rosetta"; console.log(typeof one);',
6
+ "package.json": `{
7
+ "dependencies": {
8
+ "d3-rosetta": "1.0.0"
9
+ },
10
+ "vizhub": {
11
+ "libraries": {
12
+ "d3-rosetta": {
13
+ "global": "d3Rosetta",
14
+ "path": "/dist/d3-rosetta.umd.js"
15
+ }
16
+ }
17
+ }
18
+ }`,
19
+ };
@@ -0,0 +1,6 @@
1
+ export const es6Preserve = {
2
+ "index.html": `
3
+ <script src='bundle.js'></script>
4
+ `,
5
+ "index.js": "const fn = a => a * a; console.log(fn(4));",
6
+ };
@@ -0,0 +1,6 @@
1
+ export const generatorSupport = {
2
+ "index.html": `
3
+ <script src='bundle.js'></script>
4
+ `,
5
+ "index.js": "console.log(function* () { yield 5; }().next().value)",
6
+ };
@@ -0,0 +1,15 @@
1
+ export { basicBundle } from "./basicBundle";
2
+ export { basicBundleNoExtension } from "./basicBundleNoExtension";
3
+ export { d3Import } from "./d3Import";
4
+ export { d3ImportPkg } from "./d3ImportPkg";
5
+ export { reactImport } from "./reactImport";
6
+ export { reactImportPkg } from "./reactImportPkg";
7
+ export { reactDomImport } from "./reactDomImport";
8
+ export { reactDomImportPkg } from "./reactDomImportPkg";
9
+ export { jsxTranspile } from "./jsxTranspile";
10
+ export { es6Preserve } from "./es6Preserve";
11
+ export { generatorSupport } from "./generatorSupport";
12
+ export { unicodeSupport } from "./unicodeSupport";
13
+ export { d3RosettaImportPkg } from "./d3RosettaImportPkg";
14
+ // Make sure to add the sourceMapErrorFixture to the exports
15
+ export * from "./sourceMapErrorFixture";
@@ -0,0 +1,6 @@
1
+ export const jsxTranspile = {
2
+ "index.html": `
3
+ <script src='bundle.js'></script>
4
+ `,
5
+ "index.js": "<div>Hello JSX!</div>",
6
+ };
@@ -0,0 +1,8 @@
1
+ export const reactDomImport = {
2
+ "index.html": `
3
+ <script src='https://cdn.jsdelivr.net/npm/react@18.3.1/umd/react.production.min.js'></script>
4
+ <script src='https://cdn.jsdelivr.net/npm/react-dom@18.3.1/umd/react-dom.production.min.js'></script>
5
+ <script src='bundle.js'></script>
6
+ `,
7
+ "index.js": 'import ReactDOM from "react-dom"; console.log(typeof ReactDOM);',
8
+ };