@vizhub/runtime 0.0.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 (52) hide show
  1. package/README.md +173 -0
  2. package/package.json +37 -0
  3. package/src/computeSrcDoc.ts +68 -0
  4. package/src/index.ts +7 -0
  5. package/src/useRuntime.ts +394 -0
  6. package/src/v2Runtime/bundle/bubleJSXOnly.ts +34 -0
  7. package/src/v2Runtime/bundle/getLibraries.js +31 -0
  8. package/src/v2Runtime/bundle/hypothetical.js +232 -0
  9. package/src/v2Runtime/bundle/index.js +88 -0
  10. package/src/v2Runtime/bundle/packageJson.ts +49 -0
  11. package/src/v2Runtime/bundle/rollup.browser.js +28414 -0
  12. package/src/v2Runtime/bundle.test.js +151 -0
  13. package/src/v2Runtime/computeSrcDocV2.test.ts +163 -0
  14. package/src/v2Runtime/computeSrcDocV2.ts +34 -0
  15. package/src/v2Runtime/getComputedIndexHtml.test.ts +33 -0
  16. package/src/v2Runtime/getComputedIndexHtml.ts +106 -0
  17. package/src/v2Runtime/getText.ts +19 -0
  18. package/src/v2Runtime/magicSandbox.js +291 -0
  19. package/src/v2Runtime/packageJson.js +42 -0
  20. package/src/v2Runtime/transformFiles.test.js +18 -0
  21. package/src/v2Runtime/transformFiles.ts +15 -0
  22. package/src/v2Runtime/v3FilesToV2Files.test.ts +20 -0
  23. package/src/v2Runtime/v3FilesToV2Files.ts +14 -0
  24. package/src/v3Runtime/build.test.ts +474 -0
  25. package/src/v3Runtime/build.ts +270 -0
  26. package/src/v3Runtime/cleanRollupErrorMessage.ts +15 -0
  27. package/src/v3Runtime/computeSrcDocV3.ts +151 -0
  28. package/src/v3Runtime/extractVizImport.test.ts +41 -0
  29. package/src/v3Runtime/extractVizImport.ts +34 -0
  30. package/src/v3Runtime/generateRollupErrorMessage.ts +84 -0
  31. package/src/v3Runtime/importFromViz.ts +36 -0
  32. package/src/v3Runtime/index.ts +1 -0
  33. package/src/v3Runtime/parseId.ts +14 -0
  34. package/src/v3Runtime/setupV3Runtime.ts +478 -0
  35. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified-src.js +121 -0
  36. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified.js +121 -0
  37. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle.js +239 -0
  38. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/index.js +1 -0
  39. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package-lock.json +475 -0
  40. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package.json +19 -0
  41. package/src/v3Runtime/transformDSV/d3-dsv-custom-build/rollup.config.js +9 -0
  42. package/src/v3Runtime/transformDSV/index.ts +71 -0
  43. package/src/v3Runtime/transformSvelte.ts +111 -0
  44. package/src/v3Runtime/types.ts +158 -0
  45. package/src/v3Runtime/urlLoad.ts +33 -0
  46. package/src/v3Runtime/virtual.ts +27 -0
  47. package/src/v3Runtime/vizCache.test.ts +126 -0
  48. package/src/v3Runtime/vizCache.ts +60 -0
  49. package/src/v3Runtime/vizLoad.ts +68 -0
  50. package/src/v3Runtime/vizLoadSvelte.ts +46 -0
  51. package/src/v3Runtime/vizResolve.ts +100 -0
  52. package/src/v3Runtime/worker.ts +231 -0
@@ -0,0 +1,158 @@
1
+ import { Content, V3PackageJson, VizId } from 'entities';
2
+
3
+ // The result of a build.
4
+ export type V3BuildResult = {
5
+ // Could be undefined if there's no index.js.
6
+ src: string | undefined;
7
+ pkg: V3PackageJson | undefined;
8
+ warnings: Array<V3BuildError>;
9
+ time: number;
10
+
11
+ // A list of CSS files to be injected into the IFrame.
12
+ // e.g. `['./styles.css']`
13
+ // TODO e.g. `['@curran/scatter-plot/styles.css']`
14
+ cssFiles: Array<ResolvedVizFileId>;
15
+ };
16
+
17
+ // The shape of a build error.
18
+ export type V3BuildError = {
19
+ code: string;
20
+ message: string;
21
+ };
22
+
23
+ // A resolved viz file id is of the form
24
+ // `{idOrSlug}/{fileName}`
25
+ export type ResolvedVizFileId = string;
26
+
27
+ // Messages sent to and from the build worker.
28
+ export type V3WorkerMessage =
29
+ // `contentRequest`
30
+ // * Sent from the worker to the main thread.
31
+ // * When the worker requests the content of an imported viz.
32
+ // * The main thread should respond with a `getContentResponse` message.
33
+ // * This supports the worker's VizCache when it has a cache miss.
34
+ | { type: 'contentRequest'; vizId: VizId }
35
+
36
+ // `contentResponse`
37
+ // * Sent from the main thread to the worker.
38
+ // * When the main thread responds to a `contentRequest` message.
39
+ | {
40
+ type: 'contentResponse';
41
+ vizId: VizId;
42
+ content: Content;
43
+ }
44
+
45
+ // `buildRequest`
46
+ // * Sent from the main thread to the worker.
47
+ // * When the main thread requests a build.
48
+ | {
49
+ type: 'buildRequest';
50
+ vizId: VizId;
51
+ enableSourcemap: boolean;
52
+ }
53
+
54
+ // `buildResponse`
55
+ // * Sent from the worker to the main thread.
56
+ // * When the worker responds to a `buildRequest` message.
57
+ // * This message is sent to the main thread.
58
+ // * This message includes
59
+ // * EITHER `buildResult` the result of the build
60
+ // * OR `error` if the build failed.
61
+ | {
62
+ type: 'buildResponse';
63
+ buildResult?: V3BuildResult;
64
+ error?: Error;
65
+ }
66
+
67
+ // `invalidateVizCache`
68
+ // * Sent from the main thread to the worker.
69
+ // * When the main thread wants to invalidate the VizCache.
70
+ // * This happens when an imported viz changes.
71
+ | {
72
+ type: 'invalidateVizCacheRequest';
73
+ changedVizIds: Array<VizId>;
74
+ }
75
+
76
+ // `invalidateVizCacheResponse`
77
+ // * Sent from the worker to the main thread.
78
+ // * When the worker responds to a `invalidateVizCacheRequest` message.
79
+ | {
80
+ type: 'invalidateVizCacheResponse';
81
+ }
82
+
83
+ // `resolveSlugRequest`
84
+ // * Sent from the worker to the main thread.
85
+ // * When the worker requests a viz ID for a slug.
86
+ | {
87
+ type: 'resolveSlugRequest';
88
+ slugKey: string;
89
+ requestId: string;
90
+ }
91
+
92
+ // `resolveSlugResponse`
93
+ // * Sent from the main thread to the worker.
94
+ // * When the main thread responds to a `resolveSlugRequest` message.
95
+ | {
96
+ type: 'resolveSlugResponse';
97
+ slugKey: string;
98
+ vizId: VizId;
99
+ requestId: string;
100
+ }
101
+
102
+ // `resetRequest`
103
+ // * Sent from the main thread to the worker.
104
+ // * When the main thread wants to reset the runtime
105
+ // * This happens when an error occurs.
106
+ | {
107
+ type: 'resetSrcdocRequest';
108
+ vizId: VizId;
109
+ changedVizIds: Array<VizId>;
110
+ }
111
+
112
+ // `resetResponse`
113
+ // * Sent from the worker to the main thread.
114
+ // * When the worker responds to a `resetRequest` message.
115
+ // * Provides:
116
+ // * EITHER a fresh `srcdoc` for the iframe
117
+ // * OR an `error` if the build failed.
118
+ | {
119
+ type: 'resetSrcdocResponse';
120
+ srcdoc?: string;
121
+ error?: Error;
122
+ };
123
+
124
+ // Messages sent to and from the IFrame window.
125
+ export type V3WindowMessage =
126
+ // `runJS`
127
+ // * Sent from the main thread to the IFrame.
128
+ // * Triggers hot reloading within the V3 runtime.
129
+ | {
130
+ type: 'runJS';
131
+ src: string;
132
+ }
133
+
134
+ // `runCSS`
135
+ // * Sent from the main thread to the IFrame.
136
+ // * Triggers hot reloading of CSS within the V3 runtime.
137
+ | {
138
+ type: 'runCSS';
139
+ src: string;
140
+ id: ResolvedVizFileId;
141
+ }
142
+
143
+ // `runDone`
144
+ // * Sent from the IFrame to the main thread.
145
+ // * Indicates that the V3 runtime has finished running the JS.
146
+ // * If this was sent, there were no immediate runtime errors.
147
+ | {
148
+ type: 'runDone';
149
+ }
150
+
151
+ // `runError`
152
+ // * Sent from the IFrame to the main thread.
153
+ // * Indicates that the V3 runtime has finished running the JS.
154
+ // * If this was sent, there was an immediate runtime error.
155
+ | {
156
+ type: 'runError';
157
+ error: Error;
158
+ };
@@ -0,0 +1,33 @@
1
+ // import { InputPluginOption } from 'rollup';
2
+ // const debug = false;
3
+
4
+ // const fetchedFileCache = new Map<string, string>();
5
+
6
+ // // Responsible for loading Svelte internal imports.
7
+ // // Inspired by:
8
+ // // https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/bundler/index.js#L345C3-L345C25
9
+ // export const urlLoad = (): InputPluginOption => ({
10
+ // name: 'urlLoad',
11
+
12
+ // // `id` here is of the form
13
+ // // `{vizId}/{fileName}`
14
+ // load: async (resolved: string) => {
15
+ // if (!resolved.startsWith('https://')) {
16
+ // return;
17
+ // }
18
+ // if (debug) {
19
+ // console.log('[urlLoad]: load() ' + resolved);
20
+ // }
21
+
22
+ // const cachedFile = fetchedFileCache.get(resolved);
23
+ // if (cachedFile) return cachedFile;
24
+
25
+ // const fetchedFile = await fetch(resolved).then((res) =>
26
+ // res.text(),
27
+ // );
28
+
29
+ // fetchedFileCache.set(resolved, fetchedFile);
30
+
31
+ // return fetchedFile;
32
+ // },
33
+ // });
@@ -0,0 +1,27 @@
1
+ // // Virtual file system for Rollup
2
+ // // A Rollup plugin for a virtual file system.
3
+ // // Inspired by https://github.com/Permutatrix/rollup-plugin-hypothetical/blob/master/index.js
4
+
5
+ // import { InputPluginOption } from 'rollup';
6
+ // import { V3RuntimeFiles } from '.';
7
+
8
+ // const js = (name: string) =>
9
+ // name.endsWith('.js') ? name : name + '.js';
10
+
11
+ // export const virtual = (
12
+ // files: V3RuntimeFiles,
13
+ // ): InputPluginOption => ({
14
+ // name: 'virtual',
15
+ // // If the id starts with './', then it's a relative path,
16
+ // // and is the responsibility of the virtual file system.
17
+ // resolveId: (id: string, importer: string) => {
18
+ // console.log('virtual: resolveId() ' + id);
19
+ // console.log(' importer: ' + importer);
20
+ // // id.startsWith('./') ? id : null,
21
+ // if (id.startsWith('./')) {
22
+ // return id;
23
+ // }
24
+ // return null;
25
+ // },
26
+ // load: (id: string) => files[js(id.substring(2))],
27
+ // });
@@ -0,0 +1,126 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { Content } from 'entities';
3
+ import { sampleContent } from 'entities/test/fixtures';
4
+ import { createVizCache } from './vizCache';
5
+
6
+ describe('VizCache', () => {
7
+ describe('VizCache - get method', () => {
8
+ it('should return content from cache if available', async () => {
9
+ const vizCache = createVizCache({
10
+ initialContents: [sampleContent],
11
+ handleCacheMiss: vi.fn(),
12
+ });
13
+ const content = await vizCache.get(sampleContent.id);
14
+ expect(content).toEqual(sampleContent);
15
+ expect(vi.fn()).toHaveBeenCalledTimes(0); // handleCacheMiss should not be called
16
+ });
17
+
18
+ it('should fetch content on cache miss and store it', async () => {
19
+ const handleCacheMissMock = vi
20
+ .fn()
21
+ .mockResolvedValue(sampleContent);
22
+ const vizCache = createVizCache({
23
+ initialContents: [],
24
+ handleCacheMiss: handleCacheMissMock,
25
+ });
26
+
27
+ const content = await vizCache.get(sampleContent.id);
28
+ expect(handleCacheMissMock).toHaveBeenCalledWith(
29
+ sampleContent.id,
30
+ );
31
+ expect(content).toEqual(sampleContent);
32
+ // Verify that the cache now contains the fetched content
33
+ const cachedContent = await vizCache.get(
34
+ sampleContent.id,
35
+ );
36
+ expect(cachedContent).toEqual(sampleContent);
37
+ });
38
+
39
+ it('should throw an error if handleCacheMiss does not return content', async () => {
40
+ const handleCacheMissMock = vi
41
+ .fn()
42
+ .mockResolvedValue(undefined);
43
+ const vizCache = createVizCache({
44
+ initialContents: [],
45
+ handleCacheMiss: handleCacheMissMock,
46
+ });
47
+
48
+ await expect(
49
+ vizCache.get('nonexistentId'),
50
+ ).rejects.toThrow(
51
+ 'Unresolved import from vizId nonexistentId',
52
+ );
53
+ });
54
+
55
+ // Add tests for set
56
+ });
57
+ describe('VizCache - set method', () => {
58
+ it('should add new content to the cache', async () => {
59
+ const vizCache = createVizCache({
60
+ initialContents: [],
61
+ handleCacheMiss: vi.fn(),
62
+ });
63
+
64
+ const newContent: Content = {
65
+ id: 'newContent',
66
+ files: {},
67
+ title: 'New Content',
68
+ };
69
+
70
+ vizCache.set(newContent);
71
+
72
+ // Verify new content is added
73
+ const content = await vizCache.get(newContent.id);
74
+ expect(content).toEqual(newContent);
75
+ });
76
+
77
+ it('should update existing content in the cache', async () => {
78
+ const updatedContent: Content = {
79
+ ...sampleContent,
80
+ title: 'Updated Content Title',
81
+ };
82
+
83
+ const vizCache = createVizCache({
84
+ initialContents: [sampleContent],
85
+ handleCacheMiss: vi.fn(),
86
+ });
87
+
88
+ // Update existing content
89
+ vizCache.set(updatedContent);
90
+
91
+ // Verify content is updated
92
+ const content = await vizCache.get(updatedContent.id);
93
+ expect(content).toEqual(updatedContent);
94
+ });
95
+
96
+ it('should keep the cache consistent after multiple set operations', async () => {
97
+ const vizCache = createVizCache({
98
+ initialContents: [],
99
+ handleCacheMiss: vi.fn(),
100
+ });
101
+
102
+ // Adding multiple contents
103
+ const contentA: Content = {
104
+ id: 'contentA',
105
+ files: {},
106
+ title: 'Content A',
107
+ };
108
+
109
+ const contentB: Content = {
110
+ id: 'contentB',
111
+ files: {},
112
+ title: 'Content B',
113
+ };
114
+
115
+ vizCache.set(contentA);
116
+ vizCache.set(contentB);
117
+
118
+ // Verify both contents are retrievable
119
+ const retrievedA = await vizCache.get(contentA.id);
120
+ const retrievedB = await vizCache.get(contentB.id);
121
+
122
+ expect(retrievedA).toEqual(contentA);
123
+ expect(retrievedB).toEqual(contentB);
124
+ });
125
+ });
126
+ });
@@ -0,0 +1,60 @@
1
+ import { Content, VizId } from 'entities';
2
+
3
+ export type VizCache = {
4
+ get: (vizId: string) => Promise<Content>;
5
+ set: (content: Content) => void;
6
+ invalidate: (vizId: string) => void;
7
+ };
8
+
9
+ // A cache of viz content.
10
+ // For use in resolving imports from other vizzes.
11
+ // Runs both on the server and in the browser.
12
+ export const createVizCache = ({
13
+ initialContents,
14
+ handleCacheMiss,
15
+ }: {
16
+ initialContents: Array<Content>;
17
+ handleCacheMiss: (vizId: VizId) => Promise<Content>;
18
+ }): VizCache => {
19
+ // Track the content of cached vizzes.
20
+ const contentMap = new Map<VizId, Content>(
21
+ initialContents.map((content) => [content.id, content]),
22
+ );
23
+
24
+ // Gets the content of a viz.
25
+ // Returns the cached content if it exists.
26
+ // Otherwise, calls handleCacheMiss to fetch the content.
27
+ const get = async (vizId: string): Promise<Content> => {
28
+ const cachedContent: Content | undefined =
29
+ contentMap.get(vizId);
30
+
31
+ // Cache hit
32
+ if (cachedContent !== undefined) {
33
+ return cachedContent;
34
+ }
35
+
36
+ // Cache miss
37
+ const freshContent = await handleCacheMiss(vizId);
38
+
39
+ if (freshContent) {
40
+ contentMap.set(vizId, freshContent);
41
+ return freshContent;
42
+ }
43
+
44
+ // TODO surface this error to the user
45
+ throw new Error(
46
+ `Unresolved import from vizId ${vizId}`,
47
+ );
48
+ };
49
+
50
+ // Updates the content of a viz in the cache.
51
+ const set = (content: Content) => {
52
+ contentMap.set(content.id, content);
53
+ };
54
+
55
+ const invalidate = (vizId: string) => {
56
+ contentMap.delete(vizId);
57
+ };
58
+
59
+ return { get, set, invalidate };
60
+ };
@@ -0,0 +1,68 @@
1
+ import { InputPluginOption } from 'rollup';
2
+ import { ResolvedVizFileId } from './types';
3
+ import { parseId } from './parseId';
4
+ import { Content, getFileText } from 'entities';
5
+ import { VizCache } from './vizCache';
6
+
7
+ const debug = false;
8
+
9
+ // Responsible for loading all imports and
10
+ // tracking which CSS files are imported.
11
+ // Throws an error if a file is imported but not found.
12
+ export const vizLoad = ({
13
+ vizCache,
14
+ trackCSSImport,
15
+ }: {
16
+ vizCache: VizCache;
17
+ trackCSSImport: (cssFile: ResolvedVizFileId) => void;
18
+ }): InputPluginOption => ({
19
+ name: 'vizLoad',
20
+
21
+ // `id` here is of the form
22
+ // `{vizId}/{fileName}`
23
+ load: async (id: ResolvedVizFileId) => {
24
+ if (debug) {
25
+ console.log('[vizLoadCSS]: load() ' + id);
26
+ }
27
+
28
+ const { vizId, fileName } = parseId(id);
29
+
30
+ if (debug) {
31
+ console.log(' [vizLoadCSS] vizId: ' + vizId);
32
+ console.log(' [vizLoadCSS] fileName: ' + fileName);
33
+ }
34
+
35
+ // For CSS imports, all we need to do here is
36
+ // keep track of them so they can be injected
37
+ // into the IFrame later.
38
+ if (fileName.endsWith('.css')) {
39
+ if (debug) {
40
+ console.log(
41
+ ' [vizResolve] tracking CSS import for ' + id,
42
+ );
43
+ }
44
+ // The import is tracked here so that it can be
45
+ // injected into the IFrame later, external to the
46
+ // Rollup build.
47
+ trackCSSImport(id);
48
+ // TODO consider using Rollup's `emitFile` to emit a CSS file.
49
+ return '';
50
+ }
51
+
52
+ const content: Content = await vizCache.get(vizId);
53
+ const fileText = getFileText(content, fileName);
54
+
55
+ // If a file is imported but not found, throw an error.
56
+ if (fileText === null) {
57
+ throw new Error(
58
+ `Imported file "${fileName}" not found.`,
59
+ );
60
+ // TODO ideally show username/slug instead of vizId
61
+ // `Imported file "${fileName}" not found in viz ${vizId}`,
62
+ // `Imported file "${fileName}" not found.`,
63
+ // );
64
+ }
65
+
66
+ return fileText;
67
+ },
68
+ });
@@ -0,0 +1,46 @@
1
+ // import { InputPluginOption } from 'rollup';
2
+ // import { VizCache } from './vizCache';
3
+ // import { ResolvedVizFileId } from './types';
4
+ // import { parseId } from './parseId';
5
+ // import { Content, getFileText } from 'entities';
6
+
7
+ // const debug = true;
8
+
9
+ // export const vizLoadSvelte = ({
10
+ // vizCache,
11
+ // }: {
12
+ // vizCache: VizCache;
13
+ // }): InputPluginOption => ({
14
+ // name: 'vizLoadSvelte',
15
+
16
+ // // `id` here is of the form
17
+ // // `{vizId}/{fileName}`
18
+ // load: async (id: ResolvedVizFileId) => {
19
+ // if (debug) {
20
+ // console.log('vizLoadSvelte: load() ' + id);
21
+ // }
22
+
23
+ // const { vizId, fileName } = parseId(id);
24
+
25
+ // if (debug) {
26
+ // console.log(' [vizLoadSvelte] vizId: ' + vizId);
27
+ // console.log(
28
+ // ' [vizLoadSvelte] fileName: ' + fileName,
29
+ // );
30
+ // }
31
+
32
+ // if (fileName.endsWith('.svelte')) {
33
+ // if (debug) {
34
+ // console.log(
35
+ // ' [vizLoadSvelte] tracking Svelte import for ' +
36
+ // id,
37
+ // );
38
+ // }
39
+
40
+ // // For Svelte imports, we need to recursively resolve
41
+ // // the imports of the imported viz.
42
+ // const content: Content = await vizCache.get(vizId);
43
+ // return getFileText(content, fileName);
44
+ // }
45
+ // },
46
+ // });
@@ -0,0 +1,100 @@
1
+ // A custom Rollup plugin to:
2
+ // * Implement a virtual file system
3
+ // * Support importing across vizzes
4
+ // Unified Rollup plugin for virtual file system and viz imports
5
+ // Combines functionalities of 'virtual' and 'importFromViz' plugins
6
+ import { InputPluginOption } from 'rollup';
7
+ import { extractVizImport } from './extractVizImport';
8
+ import { VizId, isId } from 'entities';
9
+ import { ResolvedVizFileId } from './types';
10
+ import { parseId } from './parseId';
11
+
12
+ const debug = false;
13
+
14
+ export const vizResolve = ({
15
+ vizId,
16
+ resolveSlug,
17
+ }: {
18
+ vizId: VizId;
19
+ resolveSlug?: ({
20
+ userName,
21
+ slug,
22
+ }: {
23
+ userName: string;
24
+ slug: string;
25
+ }) => Promise<VizId>;
26
+ }): InputPluginOption => ({
27
+ name: 'vizResolve',
28
+ resolveId: async (
29
+ id: string,
30
+ importer: string | undefined,
31
+ ): Promise<ResolvedVizFileId | undefined> => {
32
+ if (debug) {
33
+ console.log('[vizIdResolve] resolveId() ' + id);
34
+ console.log(' importer: ' + importer);
35
+ }
36
+
37
+ // Handle virtual file system resolution
38
+ // .e.g. `import { foo } from './foo.js'`
39
+ // .e.g. `import { foo } from './foo'`
40
+ if (
41
+ id.startsWith('./') &&
42
+ !importer?.startsWith('https://')
43
+ ) {
44
+ // const fileName = js(id.substring(2));
45
+
46
+ let fileName = id.substring(2);
47
+
48
+ // Handle CSS files
49
+ // e.g. `import './styles.css'`
50
+ // Handle JS files
51
+ // e.g. `import { foo } from './foo.js'`
52
+ // e.g. `import { foo } from './foo'`
53
+ if (
54
+ !fileName.endsWith('.js') &&
55
+ !fileName.endsWith('.css') &&
56
+ !fileName.endsWith('.csv') &&
57
+ !fileName.endsWith('.svelte')
58
+ ) {
59
+ fileName += '.js';
60
+ }
61
+
62
+ // const js = (name: string) =>
63
+ // name.endsWith('.js') ? name : name + '.js';
64
+
65
+ // If there is an importer, then the file not part of
66
+ // the entry point, so it should be resolved relative
67
+ // to the importer viz.
68
+ if (importer) {
69
+ const { vizId: importerVizId } = parseId(importer);
70
+ return importerVizId + '/' + fileName;
71
+ }
72
+ return vizId + '/' + fileName;
73
+ }
74
+
75
+ // Handle viz import resolution
76
+ // e.g. `import { foo } from '@curran/98e6d6509a1e407897d4f238a330efec'`
77
+ // e.g. `import { foo } from '@curran/scatter-plot'`
78
+ const vizImport = extractVizImport(id);
79
+ if (vizImport) {
80
+ let vizId: VizId;
81
+ if (isId(vizImport.idOrSlug)) {
82
+ vizId = vizImport.idOrSlug;
83
+ } else {
84
+ if (!resolveSlug) {
85
+ throw new Error(
86
+ 'resolveSlug is required to import by slug',
87
+ );
88
+ }
89
+ vizId = await resolveSlug({
90
+ userName: vizImport.userName,
91
+ slug: vizImport.idOrSlug,
92
+ });
93
+ }
94
+ return vizId + '/index.js';
95
+ }
96
+
97
+ // If neither condition is met, return undefined.
98
+ return undefined;
99
+ },
100
+ });