@vizhub/runtime 0.0.1 → 0.0.2

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 (52) hide show
  1. package/dist/assets/setupV3Runtime-BVr5lyKp.js +240 -0
  2. package/{src/v2Runtime/bundle/rollup.browser.js → dist/index.js} +9286 -12810
  3. package/package.json +12 -6
  4. package/src/computeSrcDoc.ts +0 -68
  5. package/src/index.ts +0 -7
  6. package/src/useRuntime.ts +0 -394
  7. package/src/v2Runtime/bundle/bubleJSXOnly.ts +0 -34
  8. package/src/v2Runtime/bundle/getLibraries.js +0 -31
  9. package/src/v2Runtime/bundle/hypothetical.js +0 -232
  10. package/src/v2Runtime/bundle/index.js +0 -88
  11. package/src/v2Runtime/bundle/packageJson.ts +0 -49
  12. package/src/v2Runtime/bundle.test.js +0 -151
  13. package/src/v2Runtime/computeSrcDocV2.test.ts +0 -163
  14. package/src/v2Runtime/computeSrcDocV2.ts +0 -34
  15. package/src/v2Runtime/getComputedIndexHtml.test.ts +0 -33
  16. package/src/v2Runtime/getComputedIndexHtml.ts +0 -106
  17. package/src/v2Runtime/getText.ts +0 -19
  18. package/src/v2Runtime/magicSandbox.js +0 -291
  19. package/src/v2Runtime/packageJson.js +0 -42
  20. package/src/v2Runtime/transformFiles.test.js +0 -18
  21. package/src/v2Runtime/transformFiles.ts +0 -15
  22. package/src/v2Runtime/v3FilesToV2Files.test.ts +0 -20
  23. package/src/v2Runtime/v3FilesToV2Files.ts +0 -14
  24. package/src/v3Runtime/build.test.ts +0 -474
  25. package/src/v3Runtime/build.ts +0 -270
  26. package/src/v3Runtime/cleanRollupErrorMessage.ts +0 -15
  27. package/src/v3Runtime/computeSrcDocV3.ts +0 -151
  28. package/src/v3Runtime/extractVizImport.test.ts +0 -41
  29. package/src/v3Runtime/extractVizImport.ts +0 -34
  30. package/src/v3Runtime/generateRollupErrorMessage.ts +0 -84
  31. package/src/v3Runtime/importFromViz.ts +0 -36
  32. package/src/v3Runtime/index.ts +0 -1
  33. package/src/v3Runtime/parseId.ts +0 -14
  34. package/src/v3Runtime/setupV3Runtime.ts +0 -478
  35. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified-src.js +0 -121
  36. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified.js +0 -121
  37. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle.js +0 -239
  38. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/index.js +0 -1
  39. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package-lock.json +0 -475
  40. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package.json +0 -19
  41. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/rollup.config.js +0 -9
  42. package/src/v3Runtime/transformDSV/index.ts +0 -71
  43. package/src/v3Runtime/transformSvelte.ts +0 -111
  44. package/src/v3Runtime/types.ts +0 -158
  45. package/src/v3Runtime/urlLoad.ts +0 -33
  46. package/src/v3Runtime/virtual.ts +0 -27
  47. package/src/v3Runtime/vizCache.test.ts +0 -126
  48. package/src/v3Runtime/vizCache.ts +0 -60
  49. package/src/v3Runtime/vizLoad.ts +0 -68
  50. package/src/v3Runtime/vizLoadSvelte.ts +0 -46
  51. package/src/v3Runtime/vizResolve.ts +0 -100
  52. package/src/v3Runtime/worker.ts +0 -231
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@vizhub/runtime",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
- "main": "src/index",
5
+ "main": "dist/index",
6
6
  "scripts": {
7
7
  "tsc": "tsc --noEmit",
8
8
  "test": "vitest run --reporter verbose",
9
- "prettier": "prettier {*.*,**/*.*} --single-quote --write"
9
+ "build": "vite build --ssr src/index.ts --outDir dist",
10
+ "prettier": "prettier {*.*,**/*.*} --single-quote --write",
11
+ "prepublishOnly": "npm run build"
10
12
  },
11
13
  "repository": {
12
14
  "type": "git",
@@ -25,13 +27,17 @@
25
27
  "svelte": "^4.2.19"
26
28
  },
27
29
  "devDependencies": {
30
+ "@rollup/plugin-node-resolve": "^15.3.0",
31
+ "@rollup/plugin-typescript": "^12.1.1",
28
32
  "@types/d3-dsv": "^3.0.7",
29
- "i": "^0.3.7",
30
- "npm": "^10.9.0",
31
33
  "typescript": "^5.6.3",
34
+ "vite": "^5.4.10",
32
35
  "vitest": "^2.1.2"
33
36
  },
34
37
  "publishConfig": {
35
38
  "access": "public"
36
- }
39
+ },
40
+ "files": [
41
+ "dist"
42
+ ]
37
43
  }
@@ -1,68 +0,0 @@
1
- // import { rollup } from 'rollup';
2
- import {
3
- Content,
4
- VizId,
5
- getRuntimeVersion,
6
- } from 'entities';
7
- import { computeSrcDocV2 } from './v2Runtime/computeSrcDocV2';
8
- import { computeSrcDocV3 } from './v3Runtime/computeSrcDocV3';
9
- import { build } from './v3Runtime/build';
10
- import { VizCache } from './v3Runtime/vizCache';
11
-
12
- const debug = false;
13
- export const computeSrcDoc = async ({
14
- rollup,
15
- content,
16
- vizCache,
17
- resolveSlug,
18
- getSvelteCompiler,
19
- }: {
20
- rollup: any;
21
- content: Content;
22
- vizCache: VizCache;
23
- // Resolves a slug import to a viz ID.
24
- resolveSlug: ({ userName, slug }) => Promise<VizId>;
25
- getSvelteCompiler: () => Promise<any>;
26
- }) => {
27
- // `runtimeVersion` is used to determine which runtime
28
- // to use. It's either 2 or 3.
29
- const runtimeVersion: number = getRuntimeVersion(content);
30
-
31
- let initialSrcdoc = '';
32
- let initialSrcdocError: string | null = null;
33
-
34
- if (debug) {
35
- console.log('computeSrcDoc.ts: computeSrcDoc()');
36
- console.log(' runtimeVersion:', runtimeVersion);
37
- console.log(' content:', content);
38
- }
39
-
40
- try {
41
- if (runtimeVersion === 2) {
42
- initialSrcdoc = await computeSrcDocV2(content);
43
- } else if (runtimeVersion === 3) {
44
- const buildResult = await build({
45
- vizId: content.id,
46
- enableSourcemap: true,
47
- rollup,
48
- vizCache,
49
- resolveSlug,
50
- getSvelteCompiler,
51
- });
52
- initialSrcdoc = await computeSrcDocV3({
53
- vizCache,
54
- buildResult,
55
- });
56
- }
57
- } catch (error) {
58
- // initialSrcdocError = e.toString();
59
- initialSrcdocError = error.message;
60
- }
61
-
62
- return {
63
- // Escape ending tags in strings like "</script>",
64
- // so that they don't break the HTML.
65
- initialSrcdoc,
66
- initialSrcdocError,
67
- };
68
- };
package/src/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export type { V3BuildResult } from './v3Runtime';
2
- export type { VizCache } from './v3Runtime/vizCache';
3
- export { computeSrcDoc } from './computeSrcDoc';
4
- export { setJSDOM } from './v2Runtime/getComputedIndexHtml';
5
- export { useRuntime } from './useRuntime';
6
- export { createVizCache } from './v3Runtime/vizCache';
7
- export { cleanRollupErrorMessage } from './v3Runtime/cleanRollupErrorMessage';
package/src/useRuntime.ts DELETED
@@ -1,394 +0,0 @@
1
- import {
2
- RefObject,
3
- useEffect,
4
- useMemo,
5
- useRef,
6
- useCallback,
7
- } from 'react';
8
- import {
9
- Content,
10
- FileId,
11
- SlugKey,
12
- VizId,
13
- generateFileId,
14
- getRuntimeVersion,
15
- } from 'entities';
16
- import { V3Runtime } from './v3Runtime/setupV3Runtime';
17
-
18
- const debug = false;
19
-
20
- // Sets up either the v2 or v3 runtime environment.
21
- // Meant to support dynamic switching between the two.
22
- export const useRuntime = ({
23
- content,
24
- iframeRef,
25
- srcdocErrorMessage,
26
- setSrcdocErrorMessage,
27
- vizCacheContents,
28
- isVisual,
29
- slugResolutionCache,
30
- submitContentOperation,
31
- }: {
32
- content: Content;
33
- iframeRef: RefObject<HTMLIFrameElement>;
34
- srcdocErrorMessage: string | null;
35
- setSrcdocErrorMessage: (error: string | null) => void;
36
- vizCacheContents: Record<string, Content>;
37
-
38
- // If this is false, there is no iframeRef.current.
39
- isVisual: boolean;
40
- slugResolutionCache: Record<SlugKey, VizId>;
41
- submitContentOperation: (
42
- next: (content: Content) => Content,
43
- ) => void;
44
- }) => {
45
- // This ref is used to skip the first mount.
46
- const initialMount = useRef(true);
47
-
48
- // `runtimeVersion` is used to determine which runtime
49
- // to use. It's either 2 or 3.
50
- const runtimeVersion: number = useMemo(
51
- () => getRuntimeVersion(content),
52
- [content],
53
- );
54
-
55
- const v3RuntimeRef = useRef<V3Runtime | null>(null);
56
-
57
- const getV3Runtime = useCallback(async () => {
58
- if (v3RuntimeRef.current === null) {
59
- // throw new Error('v3Runtime.current is null');
60
- // Poll for this to be defined.
61
- // const interval = setInterval(() => {
62
- // if (debug) {
63
- // console.log('polling for v3Runtime.current...');
64
- // }
65
- // if (v3RuntimeRef.current !== null) {
66
- // clearInterval(interval);
67
- // return v3RuntimeRef.current;
68
- // }
69
- // }, 100);
70
- return new Promise<V3Runtime>((resolve) => {
71
- const interval = setInterval(() => {
72
- if (debug) {
73
- console.log('polling for v3Runtime.current...');
74
- }
75
- if (v3RuntimeRef.current !== null) {
76
- clearInterval(interval);
77
- resolve(v3RuntimeRef.current);
78
- }
79
- }, 100);
80
- });
81
- }
82
- return v3RuntimeRef.current;
83
- }, []);
84
-
85
- const vizCacheContentsRef = useRef(vizCacheContents);
86
-
87
- useEffect(() => {
88
- vizCacheContentsRef.current = vizCacheContents;
89
- }, [vizCacheContents]);
90
-
91
- // Handles cache misses for viz content,
92
- // when a viz imports from another viz.
93
- const getLatestContent = useCallback(
94
- async (vizId: VizId): Promise<Content> => {
95
- // Sanity check, should never happen.
96
- if (!vizCacheContentsRef.current) {
97
- throw new Error(
98
- 'vizCacheContentsRef.current is null',
99
- );
100
- }
101
-
102
- const content = vizCacheContentsRef.current[vizId];
103
-
104
- // If the viz content for this import is already tracked,
105
- // then return it.
106
- if (content) {
107
- return content;
108
- } else {
109
- // TODO make this happen by:
110
- // * Fetching the viz content from the server
111
- // * Using a new API endpoint that returns the viz content
112
- // * Ingesting the snapshot and incorporating it into the vizCacheContents
113
- throw new Error(
114
- `TODO client-side fetching of newly imported vizzes. Current workaround: refresh the page`,
115
- );
116
- }
117
- },
118
- [],
119
- );
120
-
121
- // Handles cache misses for slug resolution,
122
- // when a viz imports from another viz.
123
- const resolveSlugKey = useCallback(
124
- async (slugKey: SlugKey): Promise<VizId> => {
125
- // Sanity check, should never happen.
126
- if (!slugResolutionCache) {
127
- throw new Error('slugResolutionCache is null');
128
- }
129
-
130
- const vizId = slugResolutionCache[slugKey];
131
-
132
- // If the viz ID for this slug is already tracked,
133
- // then return it.
134
- if (vizId) {
135
- return vizId;
136
- } else {
137
- // TODO make this happen by:
138
- // * Resolving the slug from the server
139
- // * Using a new API endpoint that returns the viz id for a slugKey
140
- throw new Error(
141
- `TODO client-side resolution of newly imported vizzes. Current workaround: refresh the page`,
142
- );
143
- }
144
- },
145
- [],
146
- );
147
-
148
- // Set up the v3 runtime.
149
- // TODO QA the following:
150
- // * Adding and removing index.js
151
- // * Adding and removing index.html
152
- // * Switching between versions of the runtime
153
- useEffect(() => {
154
- // If the viz is not visual (README.md only), then
155
- // we don't need to set up the v3 runtime.
156
- if (isVisual === false) {
157
- return;
158
- }
159
- if (runtimeVersion === 3) {
160
- // Load the v3 runtime.
161
- import('./v3Runtime/setupV3Runtime').then(
162
- ({ setupV3Runtime }) => {
163
- const iframe = iframeRef.current;
164
-
165
- // Should never happen. Added to pacify TypeScript.
166
- if (iframe === null) {
167
- throw new Error('iframe is null');
168
- }
169
-
170
- if (!content.id) {
171
- throw new Error('content.id is not defined');
172
- }
173
-
174
- // Writes the content of a file.
175
- const writeFile = (
176
- name: string,
177
- text: string,
178
- ) => {
179
- submitContentOperation((content: Content) => {
180
- // For new files, generate a fileId.
181
- let fileId: FileId = generateFileId();
182
-
183
- // For existing files, get the fileId.
184
- const { files } = content;
185
- if (files !== undefined) {
186
- const fileIds = Object.keys(files);
187
- const foundFileId = fileIds.find(
188
- (fileId) => files[fileId].name === name,
189
- );
190
- if (foundFileId) {
191
- fileId = foundFileId;
192
- }
193
- }
194
-
195
- return {
196
- ...content,
197
- files: {
198
- ...content.files,
199
- [fileId]: {
200
- name,
201
- text,
202
- },
203
- },
204
- // Trigger a re-run.
205
- isInteracting: true,
206
- };
207
- });
208
-
209
- // Clear the `isInteracting` property.
210
- setTimeout(() => {
211
- // This somewhat cryptic logic
212
- // deletes the `isInteracting` property
213
- // from the document.
214
- submitContentOperation(
215
- ({ isInteracting, ...newDocument }) =>
216
- newDocument,
217
- );
218
- }, 0);
219
- };
220
-
221
- v3RuntimeRef.current = setupV3Runtime({
222
- vizId: content.id,
223
- iframe,
224
- setSrcdocErrorMessage,
225
- getLatestContent,
226
- resolveSlugKey,
227
- writeFile,
228
- });
229
- },
230
- );
231
- }
232
- }, [runtimeVersion, isVisual, submitContentOperation]);
233
-
234
- // Send updates of imported vizzes to the V3 runtime.
235
- const previousVizCacheContents = useRef(vizCacheContents);
236
-
237
- // Track the srcdoc error message, but only check it
238
- // when we are attempting to run (do not re-run when
239
- // the error message changes or is cleared).
240
- const srcdocErrorMessageRef = useRef(srcdocErrorMessage);
241
- useEffect(() => {
242
- srcdocErrorMessageRef.current = srcdocErrorMessage;
243
- }, [srcdocErrorMessage]);
244
-
245
- useEffect(() => {
246
- // Don't crash for v2 runtime!
247
- if (runtimeVersion !== 3) {
248
- return;
249
- }
250
-
251
- // Don't run on first render.
252
- if (initialMount.current === true) {
253
- return;
254
- }
255
-
256
- // Don't run if the viz is not visual (README.md only).
257
- if (isVisual === false) {
258
- return;
259
- }
260
-
261
- // console.log(
262
- // 'TODO update the v3 runtime when imported vizzes change',
263
- // );
264
-
265
- // Find the imported vizzes that have changed.
266
- const changedVizIds = Object.keys(
267
- vizCacheContents,
268
- ).filter((vizId) => {
269
- return (
270
- previousVizCacheContents.current[vizId] !==
271
- vizCacheContents[vizId]
272
- );
273
- });
274
- previousVizCacheContents.current = vizCacheContents;
275
-
276
- if (debug) {
277
- console.log(
278
- '[useRuntime] changedVizIds',
279
- changedVizIds,
280
- );
281
- }
282
-
283
- if (changedVizIds.length === 0) {
284
- return;
285
- }
286
-
287
- // // See if any of the vizzes we import from are interacting.
288
- let isInteracting = false;
289
- for (const vizId of changedVizIds) {
290
- if (vizCacheContents[vizId].isInteracting) {
291
- isInteracting = true;
292
- break;
293
- }
294
- }
295
- if (debug) {
296
- console.log('isInteracting', isInteracting);
297
- }
298
-
299
- const update = async () => {
300
- const v3Runtime = await getV3Runtime();
301
- // Sanity check, should never happen.
302
- if (v3Runtime === null) {
303
- throw new Error('v3Runtime is null');
304
- }
305
- if (isInteracting) {
306
- // console.log('Running the code!');
307
- // console.log(
308
- // 'srcdocErrorMessageRef.current',
309
- // srcdocErrorMessageRef.current,
310
- // );
311
-
312
- // If we are recovering from an error,
313
- // clear the error message, and run the code
314
- // totally fresh by re-computing the srcdoc.
315
- if (srcdocErrorMessageRef.current) {
316
- v3Runtime.resetSrcdoc(changedVizIds);
317
- // try {
318
- // const srcdoc = await computeSrcDocV3(content);
319
- // if (iframeRef.current) {
320
- // iframeRef.current.srcdoc = srcdoc;
321
- // }
322
- // } catch (error) {
323
- // console.error(error);
324
- // setSrcdocErrorMessage(error.message);
325
- // }
326
- } else {
327
- // Re-run the code with hot reloading.
328
- v3Runtime.invalidateVizCache(changedVizIds);
329
- }
330
- }
331
- };
332
- update();
333
- }, [vizCacheContents, runtimeVersion]);
334
-
335
- // Compute V2 updates on the main thread.
336
- useEffect(() => {
337
- // We don't need to execute a "run" on first render,
338
- // because SSR handles the initial run by injecting
339
- // the srcdoc into the page server-side.
340
- if (initialMount.current) {
341
- return;
342
- }
343
-
344
- // The following code only runs after the user
345
- // has edited the code and the files have changed.
346
- if (runtimeVersion === 2) {
347
- // // Debounce the updates.
348
- const v2Run = async () => {
349
- // Clear the console before each run.
350
- console.clear();
351
-
352
- // Clear out the old error.
353
- setSrcdocErrorMessage(null);
354
-
355
- // v2RuntimeWorker.current.postMessage({ content });
356
-
357
- // Set process on global scope so computeSrcDoc doesn't break.
358
- // @ts-ignore
359
- globalThis.process = {};
360
-
361
- // Lazy load computeSrcDoc because it's a large chunk.
362
- const { computeSrcDocV2 } = await import(
363
- './v2Runtime/computeSrcDocV2'
364
- );
365
-
366
- // console.log(computeSrcDoc);
367
- try {
368
- const srcdoc = await computeSrcDocV2(content);
369
- if (iframeRef.current) {
370
- iframeRef.current.srcdoc = srcdoc;
371
- }
372
- } catch (error) {
373
- console.error(error);
374
- setSrcdocErrorMessage(error.message);
375
- }
376
- };
377
- if (content.isInteracting) {
378
- v2Run();
379
- }
380
- }
381
- }, [
382
- content.files,
383
- content.isInteracting,
384
- runtimeVersion,
385
- ]);
386
-
387
- // Track the initial mount.
388
- useEffect(() => {
389
- if (initialMount.current) {
390
- initialMount.current = false;
391
- return;
392
- }
393
- }, []);
394
- };
@@ -1,34 +0,0 @@
1
- // Derived from https://github.com/rollup/rollup-plugin-buble/blob/master/src/index.js
2
- // import { transform } from 'buble-jsx-only';
3
-
4
- import { transform } from 'buble';
5
- // import { createFilter } from 'rollup-pluginutils';
6
-
7
- export default function buble(options) {
8
- if (!options) options = {};
9
- // var filter = createFilter(
10
- // options.include,
11
- // options.exclude,
12
- // );
13
-
14
- if (!options.transforms) options.transforms = {};
15
- options.transforms.modules = false;
16
-
17
- return {
18
- name: 'buble',
19
-
20
- transform: function (code, id) {
21
- // if (!filter(id)) return null;
22
-
23
- try {
24
- return transform(code, options);
25
- } catch (e) {
26
- e.plugin = 'buble';
27
- if (!e.loc) e.loc = {};
28
- e.loc.file = id;
29
- e.frame = e.snippet;
30
- throw e;
31
- }
32
- },
33
- };
34
- }
@@ -1,31 +0,0 @@
1
- import vizhubLibraries from 'vizhub-libraries';
2
- import { getConfiguredLibraries } from './packageJson';
3
-
4
- export const getLibraries = (files) => {
5
- const configuredLibraries = getConfiguredLibraries(files);
6
- const userLibrariesNames = configuredLibraries
7
- ? Object.keys(configuredLibraries)
8
- : [];
9
-
10
- const userLibraries = userLibrariesNames.reduce(
11
- (globals, packageName) => {
12
- // in case if user created settings but not provide global, stub global with vizhub known global name
13
- const globalName =
14
- configuredLibraries[packageName].global ||
15
- vizhubLibraries[packageName];
16
-
17
- if (globalName) {
18
- globals[packageName] = globalName;
19
- } else {
20
- console.warn(
21
- `There is no global name for ${packageName}.\n Please add it to "vizhub.${packageName}.global" section in package.json.`,
22
- );
23
- }
24
-
25
- return globals;
26
- },
27
- {},
28
- );
29
-
30
- return { ...vizhubLibraries, ...userLibraries };
31
- };