@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.
- package/dist/assets/setupV3Runtime-BVr5lyKp.js +240 -0
- package/{src/v2Runtime/bundle/rollup.browser.js → dist/index.js} +9286 -12810
- package/package.json +12 -6
- package/src/computeSrcDoc.ts +0 -68
- package/src/index.ts +0 -7
- package/src/useRuntime.ts +0 -394
- package/src/v2Runtime/bundle/bubleJSXOnly.ts +0 -34
- package/src/v2Runtime/bundle/getLibraries.js +0 -31
- package/src/v2Runtime/bundle/hypothetical.js +0 -232
- package/src/v2Runtime/bundle/index.js +0 -88
- package/src/v2Runtime/bundle/packageJson.ts +0 -49
- package/src/v2Runtime/bundle.test.js +0 -151
- package/src/v2Runtime/computeSrcDocV2.test.ts +0 -163
- package/src/v2Runtime/computeSrcDocV2.ts +0 -34
- package/src/v2Runtime/getComputedIndexHtml.test.ts +0 -33
- package/src/v2Runtime/getComputedIndexHtml.ts +0 -106
- package/src/v2Runtime/getText.ts +0 -19
- package/src/v2Runtime/magicSandbox.js +0 -291
- package/src/v2Runtime/packageJson.js +0 -42
- package/src/v2Runtime/transformFiles.test.js +0 -18
- package/src/v2Runtime/transformFiles.ts +0 -15
- package/src/v2Runtime/v3FilesToV2Files.test.ts +0 -20
- package/src/v2Runtime/v3FilesToV2Files.ts +0 -14
- package/src/v3Runtime/build.test.ts +0 -474
- package/src/v3Runtime/build.ts +0 -270
- package/src/v3Runtime/cleanRollupErrorMessage.ts +0 -15
- package/src/v3Runtime/computeSrcDocV3.ts +0 -151
- package/src/v3Runtime/extractVizImport.test.ts +0 -41
- package/src/v3Runtime/extractVizImport.ts +0 -34
- package/src/v3Runtime/generateRollupErrorMessage.ts +0 -84
- package/src/v3Runtime/importFromViz.ts +0 -36
- package/src/v3Runtime/index.ts +0 -1
- package/src/v3Runtime/parseId.ts +0 -14
- package/src/v3Runtime/setupV3Runtime.ts +0 -478
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified-src.js +0 -121
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle-modified.js +0 -121
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/bundle.js +0 -239
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/index.js +0 -1
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package-lock.json +0 -475
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/package.json +0 -19
- package/src/v3Runtime/transformDSV/d3-dsv-custom-build/rollup.config.js +0 -9
- package/src/v3Runtime/transformDSV/index.ts +0 -71
- package/src/v3Runtime/transformSvelte.ts +0 -111
- package/src/v3Runtime/types.ts +0 -158
- package/src/v3Runtime/urlLoad.ts +0 -33
- package/src/v3Runtime/virtual.ts +0 -27
- package/src/v3Runtime/vizCache.test.ts +0 -126
- package/src/v3Runtime/vizCache.ts +0 -60
- package/src/v3Runtime/vizLoad.ts +0 -68
- package/src/v3Runtime/vizLoadSvelte.ts +0 -46
- package/src/v3Runtime/vizResolve.ts +0 -100
- package/src/v3Runtime/worker.ts +0 -231
    
        package/src/v3Runtime/build.ts
    DELETED
    
    | @@ -1,270 +0,0 @@ | |
| 1 | 
            -
            import {
         | 
| 2 | 
            -
              OutputOptions,
         | 
| 3 | 
            -
              RollupOptions,
         | 
| 4 | 
            -
              RollupCache,
         | 
| 5 | 
            -
              RollupLog,
         | 
| 6 | 
            -
              RollupBuild,
         | 
| 7 | 
            -
            } from 'rollup';
         | 
| 8 | 
            -
            import { V3BuildError, V3BuildResult } from './types';
         | 
| 9 | 
            -
            import {
         | 
| 10 | 
            -
              Content,
         | 
| 11 | 
            -
              V3PackageJson,
         | 
| 12 | 
            -
              VizId,
         | 
| 13 | 
            -
              getFileText,
         | 
| 14 | 
            -
            } from 'entities';
         | 
| 15 | 
            -
            import { vizResolve } from './vizResolve';
         | 
| 16 | 
            -
            import { VizCache } from './vizCache';
         | 
| 17 | 
            -
            import { vizLoad } from './vizLoad';
         | 
| 18 | 
            -
            import { transformDSV } from './transformDSV';
         | 
| 19 | 
            -
            import { transformSvelte } from './transformSvelte';
         | 
| 20 | 
            -
            import { missingIndexJSError } from 'gateways';
         | 
| 21 | 
            -
            import {
         | 
| 22 | 
            -
              invalidPackageJSONError,
         | 
| 23 | 
            -
              missingImportError,
         | 
| 24 | 
            -
              rollupError,
         | 
| 25 | 
            -
            } from 'gateways/src/errors';
         | 
| 26 | 
            -
             | 
| 27 | 
            -
            const debug = false;
         | 
| 28 | 
            -
             | 
| 29 | 
            -
            // From https://github.com/Rich-Harris/magic-string/blob/master/src/SourceMap.js
         | 
| 30 | 
            -
            // Modified to support Web Workers
         | 
| 31 | 
            -
            // Upstream PR: https://github.com/Rich-Harris/magic-string/pull/269
         | 
| 32 | 
            -
            function getBtoa() {
         | 
| 33 | 
            -
              if (typeof btoa === 'function') {
         | 
| 34 | 
            -
                return (str) => btoa(unescape(encodeURIComponent(str)));
         | 
| 35 | 
            -
              } else if (typeof Buffer === 'function') {
         | 
| 36 | 
            -
                return (str) =>
         | 
| 37 | 
            -
                  Buffer.from(str, 'utf-8').toString('base64');
         | 
| 38 | 
            -
              } else {
         | 
| 39 | 
            -
                return () => {
         | 
| 40 | 
            -
                  throw new Error(
         | 
| 41 | 
            -
                    'Unsupported environment: `window.btoa` or `Buffer` should be supported.',
         | 
| 42 | 
            -
                  );
         | 
| 43 | 
            -
                };
         | 
| 44 | 
            -
              }
         | 
| 45 | 
            -
            }
         | 
| 46 | 
            -
             | 
| 47 | 
            -
            const niceBTOA = getBtoa();
         | 
| 48 | 
            -
             | 
| 49 | 
            -
            const getGlobals = (pkg: V3PackageJson) => {
         | 
| 50 | 
            -
              const libraries = pkg?.vizhub?.libraries;
         | 
| 51 | 
            -
              if (libraries) {
         | 
| 52 | 
            -
                return Object.entries(libraries).reduce(
         | 
| 53 | 
            -
                  (accumulator, [packageName, config]) => {
         | 
| 54 | 
            -
                    accumulator[packageName] = config.global;
         | 
| 55 | 
            -
                    return accumulator;
         | 
| 56 | 
            -
                  },
         | 
| 57 | 
            -
                  {},
         | 
| 58 | 
            -
                );
         | 
| 59 | 
            -
              }
         | 
| 60 | 
            -
              return null;
         | 
| 61 | 
            -
            };
         | 
| 62 | 
            -
             | 
| 63 | 
            -
            // Rollup cache for incremental builds!
         | 
| 64 | 
            -
            // See https://rollupjs.org/guide/en/#cache
         | 
| 65 | 
            -
            // Manual benchmarks for scatter plot example:
         | 
| 66 | 
            -
            // Without cache: avg = 5.9 ms
         | 
| 67 | 
            -
            // With cache: avg = 5.2 ms
         | 
| 68 | 
            -
            let cache: RollupCache | undefined;
         | 
| 69 | 
            -
             | 
| 70 | 
            -
            const isWebWorker =
         | 
| 71 | 
            -
              typeof self !== 'undefined' && self.window === self;
         | 
| 72 | 
            -
             | 
| 73 | 
            -
            // Builds the code.
         | 
| 74 | 
            -
            // Throws errors if the build fails.
         | 
| 75 | 
            -
            export const build = async ({
         | 
| 76 | 
            -
              vizId,
         | 
| 77 | 
            -
              enableSourcemap = false,
         | 
| 78 | 
            -
              enableCache = isWebWorker,
         | 
| 79 | 
            -
              rollup,
         | 
| 80 | 
            -
              vizCache,
         | 
| 81 | 
            -
              resolveSlug,
         | 
| 82 | 
            -
              getSvelteCompiler,
         | 
| 83 | 
            -
            }: {
         | 
| 84 | 
            -
              // The ID of the viz being built.
         | 
| 85 | 
            -
              vizId: VizId;
         | 
| 86 | 
            -
              enableSourcemap?: boolean;
         | 
| 87 | 
            -
              enableCache?: boolean;
         | 
| 88 | 
            -
              rollup: (options: RollupOptions) => Promise<RollupBuild>;
         | 
| 89 | 
            -
             | 
| 90 | 
            -
              // The viz cache, prepopulated with at least the viz being built.
         | 
| 91 | 
            -
              vizCache: VizCache;
         | 
| 92 | 
            -
             | 
| 93 | 
            -
              // Resolves a slug import to a viz ID.
         | 
| 94 | 
            -
              resolveSlug?: ({ userName, slug }) => Promise<VizId>;
         | 
| 95 | 
            -
             | 
| 96 | 
            -
              // Gets the Svelte compiler.
         | 
| 97 | 
            -
              // This is synchronous because it either gets loaded
         | 
| 98 | 
            -
              // from a CDN with importScripts (which is synchronous)
         | 
| 99 | 
            -
              // or from a local node package (imported statically).
         | 
| 100 | 
            -
              getSvelteCompiler?: () => Promise<any>;
         | 
| 101 | 
            -
            }): Promise<V3BuildResult> => {
         | 
| 102 | 
            -
              if (debug) {
         | 
| 103 | 
            -
                console.log('  build.ts: build()');
         | 
| 104 | 
            -
                console.log('  vizId:', vizId);
         | 
| 105 | 
            -
              }
         | 
| 106 | 
            -
              const startTime = Date.now();
         | 
| 107 | 
            -
              const warnings: Array<V3BuildError> = [];
         | 
| 108 | 
            -
              let src: string | undefined;
         | 
| 109 | 
            -
              let pkg: V3PackageJson | undefined;
         | 
| 110 | 
            -
             | 
| 111 | 
            -
              const content: Content = await vizCache.get(vizId);
         | 
| 112 | 
            -
             | 
| 113 | 
            -
              const indexJSContent = getFileText(content, 'index.js');
         | 
| 114 | 
            -
              const cssFilesSet = new Set<string>();
         | 
| 115 | 
            -
              if (!indexJSContent) {
         | 
| 116 | 
            -
                throw missingIndexJSError();
         | 
| 117 | 
            -
              } else {
         | 
| 118 | 
            -
                const trackCSSImport = (cssFile: string) => {
         | 
| 119 | 
            -
                  cssFilesSet.add(cssFile);
         | 
| 120 | 
            -
                };
         | 
| 121 | 
            -
                const inputOptions: RollupOptions = {
         | 
| 122 | 
            -
                  input: './index.js',
         | 
| 123 | 
            -
                  plugins: [
         | 
| 124 | 
            -
                    vizResolve({ vizId, resolveSlug }),
         | 
| 125 | 
            -
                    // urlLoad(),
         | 
| 126 | 
            -
                    transformDSV(),
         | 
| 127 | 
            -
                    transformSvelte({ getSvelteCompiler }),
         | 
| 128 | 
            -
                    vizLoad({ vizCache, trackCSSImport }),
         | 
| 129 | 
            -
                    // vizLoadJS({ vizCache }),
         | 
| 130 | 
            -
                    // cssResolve
         | 
| 131 | 
            -
                    // csvResolve
         | 
| 132 | 
            -
                    // jsonResolve
         | 
| 133 | 
            -
                    // defaultResolve - protect against file system access
         | 
| 134 | 
            -
                  ],
         | 
| 135 | 
            -
                  onwarn: (warning: RollupLog) => {
         | 
| 136 | 
            -
                    warnings.push(JSON.parse(JSON.stringify(warning)));
         | 
| 137 | 
            -
                  },
         | 
| 138 | 
            -
                };
         | 
| 139 | 
            -
             | 
| 140 | 
            -
                // If cache is enabled AND there is a cache
         | 
| 141 | 
            -
                // from the previous build, use it.
         | 
| 142 | 
            -
                if (enableCache && cache) {
         | 
| 143 | 
            -
                  inputOptions.cache = cache;
         | 
| 144 | 
            -
                }
         | 
| 145 | 
            -
             | 
| 146 | 
            -
                const outputOptions: OutputOptions = {
         | 
| 147 | 
            -
                  format: 'umd',
         | 
| 148 | 
            -
                  name: 'Viz',
         | 
| 149 | 
            -
                  // TODO disable sourcemaps during hot reloading
         | 
| 150 | 
            -
                  sourcemap: enableSourcemap ? true : false,
         | 
| 151 | 
            -
                  // sourcemap: false,
         | 
| 152 | 
            -
                  compact: true,
         | 
| 153 | 
            -
                };
         | 
| 154 | 
            -
             | 
| 155 | 
            -
                const packageJSONContent = getFileText(
         | 
| 156 | 
            -
                  content,
         | 
| 157 | 
            -
                  'package.json',
         | 
| 158 | 
            -
                );
         | 
| 159 | 
            -
             | 
| 160 | 
            -
                if (packageJSONContent) {
         | 
| 161 | 
            -
                  try {
         | 
| 162 | 
            -
                    pkg = JSON.parse(packageJSONContent);
         | 
| 163 | 
            -
                  } catch (error) {
         | 
| 164 | 
            -
                    throw invalidPackageJSONError(error.message);
         | 
| 165 | 
            -
                  }
         | 
| 166 | 
            -
                }
         | 
| 167 | 
            -
             | 
| 168 | 
            -
                if (pkg) {
         | 
| 169 | 
            -
                  const globals = getGlobals(pkg);
         | 
| 170 | 
            -
                  if (globals) {
         | 
| 171 | 
            -
                    inputOptions.external = Object.keys(globals);
         | 
| 172 | 
            -
                    outputOptions.globals = globals;
         | 
| 173 | 
            -
                  }
         | 
| 174 | 
            -
                }
         | 
| 175 | 
            -
             | 
| 176 | 
            -
                try {
         | 
| 177 | 
            -
                  if (debug) {
         | 
| 178 | 
            -
                    console.log('  Invoking `rollup`');
         | 
| 179 | 
            -
                  }
         | 
| 180 | 
            -
                  const bundle = await rollup(inputOptions);
         | 
| 181 | 
            -
                  if (enableCache) {
         | 
| 182 | 
            -
                    cache = bundle.cache;
         | 
| 183 | 
            -
                  }
         | 
| 184 | 
            -
                  if (debug) {
         | 
| 185 | 
            -
                    console.log('  Invoking `bundle.generate`');
         | 
| 186 | 
            -
                  }
         | 
| 187 | 
            -
                  const { code, map } = (
         | 
| 188 | 
            -
                    await bundle.generate(outputOptions)
         | 
| 189 | 
            -
                  ).output[0];
         | 
| 190 | 
            -
             | 
| 191 | 
            -
                  // TODO benchmark performance and build size with vs. without sourcemaps
         | 
| 192 | 
            -
                  // Idea: no sourcemaps when interacting, sourcemaps after interact is done.
         | 
| 193 | 
            -
                  // Idea: cache builds on the server, don't store builds in ShareDB at all
         | 
| 194 | 
            -
             | 
| 195 | 
            -
                  // Escape script ending tags that may appear within strings,
         | 
| 196 | 
            -
                  // so that they don't break the server-rendered HTML.
         | 
| 197 | 
            -
                  src = code.replace('</script>', '<\\/script>');
         | 
| 198 | 
            -
             | 
| 199 | 
            -
                  // If sourcemaps are enabled, tack them onto the end inline.
         | 
| 200 | 
            -
                  if (enableSourcemap && map !== null && !debug) {
         | 
| 201 | 
            -
                    // Note that map.toUrl breaks in Web Worker as window.btoa is not defined.
         | 
| 202 | 
            -
                    // Inspired by https://github.com/Rich-Harris/magic-string/blob/abf373f2ed53d00e184ab236828853dd35a62763/src/SourceMap.js#L31
         | 
| 203 | 
            -
                    src +=
         | 
| 204 | 
            -
                      '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,' +
         | 
| 205 | 
            -
                      niceBTOA(map.toString());
         | 
| 206 | 
            -
                    // src += '\n//# sourceMappingURL=' + map.toUrl();
         | 
| 207 | 
            -
                  }
         | 
| 208 | 
            -
                  if (debug) {
         | 
| 209 | 
            -
                    console.log('  Built with Rollup');
         | 
| 210 | 
            -
                    console.log(src?.slice(0, 200));
         | 
| 211 | 
            -
                  }
         | 
| 212 | 
            -
                } catch (error) {
         | 
| 213 | 
            -
                  if (debug) {
         | 
| 214 | 
            -
                    console.log('  Caught error in build.ts:');
         | 
| 215 | 
            -
                    console.log(error);
         | 
| 216 | 
            -
                  }
         | 
| 217 | 
            -
                  // Detect missing import error from vizLoad plugin.
         | 
| 218 | 
            -
                  if (
         | 
| 219 | 
            -
                    error.code === 'PLUGIN_ERROR' &&
         | 
| 220 | 
            -
                    error.plugin === 'vizLoad' &&
         | 
| 221 | 
            -
                    error.hook === 'load'
         | 
| 222 | 
            -
                  ) {
         | 
| 223 | 
            -
                    // Remove unwieldy vizId from warning message,
         | 
| 224 | 
            -
                    // but keep slugs.
         | 
| 225 | 
            -
                    // Example:
         | 
| 226 | 
            -
                    // Missing import: Could not load 7f0b69fcb754479699172d1887817027/missing.js (imported by 7f0b69fcb754479699172d1887817027/index.js): Imported file "missing.js" not found.
         | 
| 227 | 
            -
             | 
| 228 | 
            -
                    const cleanMessage = error.message.replace(
         | 
| 229 | 
            -
                      /[0-9a-f]{32}\//g,
         | 
| 230 | 
            -
                      '',
         | 
| 231 | 
            -
                    );
         | 
| 232 | 
            -
                    throw missingImportError(cleanMessage);
         | 
| 233 | 
            -
                  }
         | 
| 234 | 
            -
             | 
| 235 | 
            -
                  throw rollupError(error.message);
         | 
| 236 | 
            -
                }
         | 
| 237 | 
            -
              }
         | 
| 238 | 
            -
             | 
| 239 | 
            -
              for (const warning of warnings) {
         | 
| 240 | 
            -
                // In VizHub, we want to treat certain warnings as errors.
         | 
| 241 | 
            -
                if (warning.code === 'UNRESOLVED_IMPORT') {
         | 
| 242 | 
            -
                  // We don't treat it as an external dependency,
         | 
| 243 | 
            -
                  // so remove that message text as it may be confusing.
         | 
| 244 | 
            -
                  const cleanMessage = warning.message.replace(
         | 
| 245 | 
            -
                    ' – treating it as an external dependency',
         | 
| 246 | 
            -
                    '',
         | 
| 247 | 
            -
                  );
         | 
| 248 | 
            -
                  throw missingImportError(cleanMessage);
         | 
| 249 | 
            -
                }
         | 
| 250 | 
            -
              }
         | 
| 251 | 
            -
             | 
| 252 | 
            -
              if (debug) {
         | 
| 253 | 
            -
                console.log('  returning src in build.ts:');
         | 
| 254 | 
            -
                console.log('  src:');
         | 
| 255 | 
            -
                console.log(src?.slice(0, 200));
         | 
| 256 | 
            -
              }
         | 
| 257 | 
            -
             | 
| 258 | 
            -
              if (debug) {
         | 
| 259 | 
            -
                console.log('  warnings:');
         | 
| 260 | 
            -
                console.log(JSON.stringify(warnings, null, 2));
         | 
| 261 | 
            -
              }
         | 
| 262 | 
            -
             | 
| 263 | 
            -
              return {
         | 
| 264 | 
            -
                warnings,
         | 
| 265 | 
            -
                src,
         | 
| 266 | 
            -
                pkg,
         | 
| 267 | 
            -
                cssFiles: Array.from(cssFilesSet),
         | 
| 268 | 
            -
                time: Date.now() - startTime,
         | 
| 269 | 
            -
              };
         | 
| 270 | 
            -
            };
         | 
| @@ -1,15 +0,0 @@ | |
| 1 | 
            -
            // We want to remove the vizId from the error message
         | 
| 2 | 
            -
            // to make it more user-friendly.
         | 
| 3 | 
            -
            // Example error message before and after:
         | 
| 4 | 
            -
            // Before: "7f0b69fcb754479699172d1887817027/index.js (14:8): Expected ';', '}' or <eof>"
         | 
| 5 | 
            -
            // After: "./index.js (14:8): Expected ';', '}' or <eof>"
         | 
| 6 | 
            -
            export const cleanRollupErrorMessage = ({
         | 
| 7 | 
            -
              rawMessage,
         | 
| 8 | 
            -
              vizId,
         | 
| 9 | 
            -
            }: {
         | 
| 10 | 
            -
              rawMessage: string;
         | 
| 11 | 
            -
              vizId: string;
         | 
| 12 | 
            -
            }) => {
         | 
| 13 | 
            -
              const regex = new RegExp(vizId, 'g');
         | 
| 14 | 
            -
              return rawMessage?.replace(regex, '.');
         | 
| 15 | 
            -
            };
         | 
| @@ -1,151 +0,0 @@ | |
| 1 | 
            -
            import { getFileText } from 'entities';
         | 
| 2 | 
            -
            import { ResolvedVizFileId, V3BuildResult } from './types';
         | 
| 3 | 
            -
            import { VizCache } from './vizCache';
         | 
| 4 | 
            -
            import { parseId } from './parseId';
         | 
| 5 | 
            -
             | 
| 6 | 
            -
            const debug = false;
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            function randomDigits() {
         | 
| 9 | 
            -
              return Math.random().toString().slice(2, 7);
         | 
| 10 | 
            -
            }
         | 
| 11 | 
            -
             | 
| 12 | 
            -
            // Generates iframe srcdoc for first run.
         | 
| 13 | 
            -
            export const computeSrcDocV3 = async ({
         | 
| 14 | 
            -
              vizCache,
         | 
| 15 | 
            -
              buildResult,
         | 
| 16 | 
            -
            }: {
         | 
| 17 | 
            -
              vizCache: VizCache;
         | 
| 18 | 
            -
              buildResult: V3BuildResult;
         | 
| 19 | 
            -
            }) => {
         | 
| 20 | 
            -
              const { pkg, src, cssFiles } = buildResult;
         | 
| 21 | 
            -
             | 
| 22 | 
            -
              let cdn = '';
         | 
| 23 | 
            -
              let styles = '';
         | 
| 24 | 
            -
             | 
| 25 | 
            -
              if (debug) {
         | 
| 26 | 
            -
                console.log('computeSrcDocV3:');
         | 
| 27 | 
            -
                console.log('  src:');
         | 
| 28 | 
            -
                console.log(src?.slice(0, 200));
         | 
| 29 | 
            -
              }
         | 
| 30 | 
            -
             | 
| 31 | 
            -
              // Inject CDN scripts for dependencies.
         | 
| 32 | 
            -
              if (
         | 
| 33 | 
            -
                pkg &&
         | 
| 34 | 
            -
                pkg.dependencies &&
         | 
| 35 | 
            -
                pkg.vizhub &&
         | 
| 36 | 
            -
                pkg.vizhub.libraries
         | 
| 37 | 
            -
              ) {
         | 
| 38 | 
            -
                const {
         | 
| 39 | 
            -
                  dependencies,
         | 
| 40 | 
            -
                  vizhub: { libraries },
         | 
| 41 | 
            -
                } = pkg;
         | 
| 42 | 
            -
             | 
| 43 | 
            -
                cdn = Object.keys(dependencies)
         | 
| 44 | 
            -
                  .map((dependency, i) => {
         | 
| 45 | 
            -
                    const version = dependencies[dependency];
         | 
| 46 | 
            -
                    const path = libraries[dependency].path;
         | 
| 47 | 
            -
                    const src = `https://cdn.jsdelivr.net/npm/${dependency}@${version}${path}`;
         | 
| 48 | 
            -
                    const indent = i > 0 ? '    ' : '\n    ';
         | 
| 49 | 
            -
                    return `${indent}<script src="${src}"></script>`;
         | 
| 50 | 
            -
                  })
         | 
| 51 | 
            -
                  .join('\n');
         | 
| 52 | 
            -
              }
         | 
| 53 | 
            -
             | 
| 54 | 
            -
              // Inject CSS files.
         | 
| 55 | 
            -
              if (cssFiles.length > 0) {
         | 
| 56 | 
            -
                for (let i = 0; i < cssFiles.length; i++) {
         | 
| 57 | 
            -
                  const id: ResolvedVizFileId = cssFiles[i];
         | 
| 58 | 
            -
                  const indent = i > 0 ? '    ' : '\n    ';
         | 
| 59 | 
            -
                  const styleElementId = 'injected-style' + id;
         | 
| 60 | 
            -
                  const { vizId, fileName } = parseId(id);
         | 
| 61 | 
            -
                  const content = await vizCache.get(vizId);
         | 
| 62 | 
            -
                  const src = getFileText(content, fileName);
         | 
| 63 | 
            -
                  styles += `${indent}<style id="${styleElementId}">${src}</style>`;
         | 
| 64 | 
            -
                }
         | 
| 65 | 
            -
              }
         | 
| 66 | 
            -
             | 
| 67 | 
            -
              const containerSuffix = randomDigits();
         | 
| 68 | 
            -
             | 
| 69 | 
            -
              const vizContainerId = `viz-container-${containerSuffix}`;
         | 
| 70 | 
            -
             | 
| 71 | 
            -
              return `<!DOCTYPE html>
         | 
| 72 | 
            -
            <html>
         | 
| 73 | 
            -
              <head>
         | 
| 74 | 
            -
                <meta charset="utf-8">${cdn}${styles}
         | 
| 75 | 
            -
                <style>
         | 
| 76 | 
            -
                  body {
         | 
| 77 | 
            -
                    margin: 0;
         | 
| 78 | 
            -
                    overflow: hidden;
         | 
| 79 | 
            -
                  }
         | 
| 80 | 
            -
                  #${vizContainerId} {
         | 
| 81 | 
            -
                    height: 100vh;
         | 
| 82 | 
            -
                  }
         | 
| 83 | 
            -
                </style>
         | 
| 84 | 
            -
              </head>
         | 
| 85 | 
            -
              <body>
         | 
| 86 | 
            -
                <div id="${vizContainerId}"></div>
         | 
| 87 | 
            -
                <script id="injected-script">${src}</script>
         | 
| 88 | 
            -
                <script>
         | 
| 89 | 
            -
                  (() => {
         | 
| 90 | 
            -
                    let cleanup;
         | 
| 91 | 
            -
                    const render = () => {
         | 
| 92 | 
            -
                      const container = document.getElementById('${vizContainerId}');
         | 
| 93 | 
            -
                      typeof cleanup === 'function' && cleanup();
         | 
| 94 | 
            -
                      cleanup = Viz.main(container, { state: window.state, setState, writeFile });
         | 
| 95 | 
            -
                    };
         | 
| 96 | 
            -
                    const setState = (next) => {
         | 
| 97 | 
            -
                      window.state = next(window.state);
         | 
| 98 | 
            -
                      render();
         | 
| 99 | 
            -
                    };
         | 
| 100 | 
            -
                    const writeFile = (fileName, content) => {
         | 
| 101 | 
            -
                      parent.postMessage({ type: 'writeFile', fileName, content }, "*");
         | 
| 102 | 
            -
                    };
         | 
| 103 | 
            -
                    const run = () => {
         | 
| 104 | 
            -
                      try {
         | 
| 105 | 
            -
                        setState((state) => state || {});
         | 
| 106 | 
            -
                      } catch (error) {
         | 
| 107 | 
            -
                        console.error(error);
         | 
| 108 | 
            -
                        parent.postMessage({ type: 'runError', error }, "*");
         | 
| 109 | 
            -
                      }
         | 
| 110 | 
            -
                    }
         | 
| 111 | 
            -
                    run();
         | 
| 112 | 
            -
                    const runJS = (src) => {
         | 
| 113 | 
            -
                      document.getElementById('injected-script')?.remove();
         | 
| 114 | 
            -
                      const script = document.createElement('script');
         | 
| 115 | 
            -
                      script.textContent = src;
         | 
| 116 | 
            -
                      script.id = 'injected-script';
         | 
| 117 | 
            -
                      document.body.appendChild(script);
         | 
| 118 | 
            -
                      run();
         | 
| 119 | 
            -
                    };
         | 
| 120 | 
            -
                    const runCSS = (src, id) => {
         | 
| 121 | 
            -
                      const styleElementId = 'injected-style' + id;
         | 
| 122 | 
            -
                      let style = document.getElementById(styleElementId);
         | 
| 123 | 
            -
                      if (!style) {
         | 
| 124 | 
            -
                        style = document.createElement('style');
         | 
| 125 | 
            -
                        style.type = 'text/css';
         | 
| 126 | 
            -
                        style.id = styleElementId;
         | 
| 127 | 
            -
                        document.head.appendChild(style);
         | 
| 128 | 
            -
                      }
         | 
| 129 | 
            -
                      style.textContent = src;
         | 
| 130 | 
            -
                    };
         | 
| 131 | 
            -
                    onmessage = (message) => {
         | 
| 132 | 
            -
                      switch (message.data.type) {
         | 
| 133 | 
            -
                        case 'runJS':
         | 
| 134 | 
            -
                          runJS(message.data.src);
         | 
| 135 | 
            -
                          parent.postMessage({ type: 'runDone' }, "*");
         | 
| 136 | 
            -
                          break;
         | 
| 137 | 
            -
                        case 'runCSS':
         | 
| 138 | 
            -
                          runCSS(message.data.src, message.data.id);
         | 
| 139 | 
            -
                          break;
         | 
| 140 | 
            -
                        case 'ping':
         | 
| 141 | 
            -
                          parent.postMessage({ type: 'pong' }, "*");
         | 
| 142 | 
            -
                          break;
         | 
| 143 | 
            -
                        default:
         | 
| 144 | 
            -
                          break;
         | 
| 145 | 
            -
                      }
         | 
| 146 | 
            -
                    }
         | 
| 147 | 
            -
                  })();
         | 
| 148 | 
            -
                </script>
         | 
| 149 | 
            -
              </body>
         | 
| 150 | 
            -
            </html>`;
         | 
| 151 | 
            -
            };
         | 
| @@ -1,41 +0,0 @@ | |
| 1 | 
            -
            import { describe, it, expect } from 'vitest';
         | 
| 2 | 
            -
            import { extractVizImport } from './extractVizImport';
         | 
| 3 | 
            -
             | 
| 4 | 
            -
            describe('v3 extractVizImport', () => {
         | 
| 5 | 
            -
              it('should correctly extract username and viz id', () => {
         | 
| 6 | 
            -
                const result = extractVizImport(
         | 
| 7 | 
            -
                  '@curran/21f72bf74ef04ea0b9c9b82aaaec859a',
         | 
| 8 | 
            -
                );
         | 
| 9 | 
            -
                expect(result).toEqual({
         | 
| 10 | 
            -
                  userName: 'curran',
         | 
| 11 | 
            -
                  idOrSlug: '21f72bf74ef04ea0b9c9b82aaaec859a',
         | 
| 12 | 
            -
                });
         | 
| 13 | 
            -
              });
         | 
| 14 | 
            -
             | 
| 15 | 
            -
              it('should correctly extract username and alphanumeric id', () => {
         | 
| 16 | 
            -
                const result = extractVizImport('@curran/scatter-plot');
         | 
| 17 | 
            -
                expect(result).toEqual({
         | 
| 18 | 
            -
                  userName: 'curran',
         | 
| 19 | 
            -
                  idOrSlug: 'scatter-plot',
         | 
| 20 | 
            -
                });
         | 
| 21 | 
            -
              });
         | 
| 22 | 
            -
             | 
| 23 | 
            -
              it('should return null for non-matching strings', () => {
         | 
| 24 | 
            -
                const result = extractVizImport(
         | 
| 25 | 
            -
                  'not-a-matching-string',
         | 
| 26 | 
            -
                );
         | 
| 27 | 
            -
                expect(result).toBeNull();
         | 
| 28 | 
            -
              });
         | 
| 29 | 
            -
             | 
| 30 | 
            -
              it('should handle cases with invalid characters in username', () => {
         | 
| 31 | 
            -
                const result = extractVizImport('@c!urran/valid-id');
         | 
| 32 | 
            -
                expect(result).toBeNull();
         | 
| 33 | 
            -
              });
         | 
| 34 | 
            -
             | 
| 35 | 
            -
              it('should handle cases with invalid characters in id', () => {
         | 
| 36 | 
            -
                const result = extractVizImport('@curran/invalid id');
         | 
| 37 | 
            -
                expect(result).toBeNull();
         | 
| 38 | 
            -
              });
         | 
| 39 | 
            -
             | 
| 40 | 
            -
              // Add more test cases as needed for edge cases or specific behaviors
         | 
| 41 | 
            -
            });
         | 
| @@ -1,34 +0,0 @@ | |
| 1 | 
            -
            import { UserName, VizId } from 'entities';
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            export type VizImport = {
         | 
| 4 | 
            -
              userName: UserName;
         | 
| 5 | 
            -
              idOrSlug: VizId | string;
         | 
| 6 | 
            -
            };
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            export const extractVizImport = (
         | 
| 9 | 
            -
              str: string,
         | 
| 10 | 
            -
            ): VizImport | null => {
         | 
| 11 | 
            -
              // Updated regular expression pattern
         | 
| 12 | 
            -
              // Username: Alphanumeric characters, including underscores and hyphens
         | 
| 13 | 
            -
              // ID: Alphanumeric characters, including underscores, hyphens, and possibly other special characters
         | 
| 14 | 
            -
              const pattern = /^@([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)$/;
         | 
| 15 | 
            -
              const match = str.match(pattern);
         | 
| 16 | 
            -
             | 
| 17 | 
            -
              if (match) {
         | 
| 18 | 
            -
                // Extract the username and id
         | 
| 19 | 
            -
                return {
         | 
| 20 | 
            -
                  userName: match[1],
         | 
| 21 | 
            -
                  idOrSlug: match[2],
         | 
| 22 | 
            -
                };
         | 
| 23 | 
            -
              } else {
         | 
| 24 | 
            -
                // Return null if the string does not match
         | 
| 25 | 
            -
                return null;
         | 
| 26 | 
            -
              }
         | 
| 27 | 
            -
            };
         | 
| 28 | 
            -
             | 
| 29 | 
            -
            // // Example usages
         | 
| 30 | 
            -
            // const result1 = extractDetails("@curran/21f72bf74ef04ea0b9c9b82aaaec859a");
         | 
| 31 | 
            -
            // console.log(result1); // { username: "curran", id: "21f72bf74ef04ea0b9c9b82aaaec859a" }
         | 
| 32 | 
            -
             | 
| 33 | 
            -
            // const result2 = extractDetails("@curran/scatter-plot");
         | 
| 34 | 
            -
            // console.log(result2); // { username: "curran", id: "scatter-plot" }
         | 
| @@ -1,84 +0,0 @@ | |
| 1 | 
            -
            // Inspired by
         | 
| 2 | 
            -
            //  * https://github.com/rollup/rollup/blob/c5337ef28a71c796e768a9f0edb3d7259a93f1aa/cli/logging.ts#L4
         | 
| 3 | 
            -
            //  * https://github.com/vizhub-core/vizhub-legacy/blob/main/vizhub-v2/packages/neoFrontend/src/pages/VizPage/VizRunnerContext/generateRunErrorMessage.js#L2
         | 
| 4 | 
            -
            import { RollupError } from 'rollup';
         | 
| 5 | 
            -
            import { ResolvedVizFileId } from './types';
         | 
| 6 | 
            -
            import { parseId } from './parseId';
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            // Clean up the file id for human consumption.
         | 
| 9 | 
            -
            export default function relativeId(
         | 
| 10 | 
            -
              id: ResolvedVizFileId,
         | 
| 11 | 
            -
            ): string {
         | 
| 12 | 
            -
              // TODO consider exposing vizId or slug in the UI if the
         | 
| 13 | 
            -
              // error originates from a viz that is imported from.
         | 
| 14 | 
            -
              const { fileName } = parseId(id);
         | 
| 15 | 
            -
              return fileName;
         | 
| 16 | 
            -
            }
         | 
| 17 | 
            -
             | 
| 18 | 
            -
            export const generateRollupErrorMessage = (
         | 
| 19 | 
            -
              error: RollupError,
         | 
| 20 | 
            -
            ) => {
         | 
| 21 | 
            -
              const name = error.name || (error.cause as Error)?.name;
         | 
| 22 | 
            -
              const nameSection = name ? `${name}: ` : '';
         | 
| 23 | 
            -
             | 
| 24 | 
            -
              const message = `${nameSection}${error.message}`;
         | 
| 25 | 
            -
             | 
| 26 | 
            -
              const outputLines = [`[!] ${message.toString()}`];
         | 
| 27 | 
            -
             | 
| 28 | 
            -
              if (error.url) {
         | 
| 29 | 
            -
                outputLines.push(error.url);
         | 
| 30 | 
            -
              }
         | 
| 31 | 
            -
             | 
| 32 | 
            -
              if (error.loc) {
         | 
| 33 | 
            -
                outputLines.push(
         | 
| 34 | 
            -
                  `${relativeId((error.loc.file || error.id)!)} (${
         | 
| 35 | 
            -
                    error.loc.line
         | 
| 36 | 
            -
                  }:${error.loc.column})`,
         | 
| 37 | 
            -
                );
         | 
| 38 | 
            -
              } else if (error.id) {
         | 
| 39 | 
            -
                outputLines.push(relativeId(error.id));
         | 
| 40 | 
            -
              }
         | 
| 41 | 
            -
             | 
| 42 | 
            -
              if (error.frame) {
         | 
| 43 | 
            -
                outputLines.push(error.frame);
         | 
| 44 | 
            -
              }
         | 
| 45 | 
            -
             | 
| 46 | 
            -
              if (error.stack) {
         | 
| 47 | 
            -
                outputLines.push(
         | 
| 48 | 
            -
                  error.stack?.replace(
         | 
| 49 | 
            -
                    `${nameSection}${error.message}\n`,
         | 
| 50 | 
            -
                    '',
         | 
| 51 | 
            -
                  ),
         | 
| 52 | 
            -
                );
         | 
| 53 | 
            -
              }
         | 
| 54 | 
            -
             | 
| 55 | 
            -
              outputLines.push('', '');
         | 
| 56 | 
            -
             | 
| 57 | 
            -
              return outputLines.join('\n');
         | 
| 58 | 
            -
             | 
| 59 | 
            -
              //   const lines = [];
         | 
| 60 | 
            -
              //   let message = error.message;
         | 
| 61 | 
            -
              //   if (error.name) {
         | 
| 62 | 
            -
              //     message = `${error.name}: ${message}`;
         | 
| 63 | 
            -
              //   }
         | 
| 64 | 
            -
              //   lines.push(message);
         | 
| 65 | 
            -
              //   if (error.url) {
         | 
| 66 | 
            -
              //     lines.push(error.url);
         | 
| 67 | 
            -
              //   }
         | 
| 68 | 
            -
              //   if (error.loc) {
         | 
| 69 | 
            -
              //     lines.push(
         | 
| 70 | 
            -
              //       `${error.loc.file || error.id} (line ${
         | 
| 71 | 
            -
              //         error.loc.line
         | 
| 72 | 
            -
              //       })`,
         | 
| 73 | 
            -
              //     );
         | 
| 74 | 
            -
              //   } else if (error.id) {
         | 
| 75 | 
            -
              //     lines.push(error.id);
         | 
| 76 | 
            -
              //   }
         | 
| 77 | 
            -
              //   if (error.frame) {
         | 
| 78 | 
            -
              //     lines.push(error.frame);
         | 
| 79 | 
            -
              //   }
         | 
| 80 | 
            -
              //   // if (error.stack) {
         | 
| 81 | 
            -
              //   //   lines.push(error.stack);
         | 
| 82 | 
            -
              //   // }
         | 
| 83 | 
            -
              //   return lines.join('\n');
         | 
| 84 | 
            -
            };
         | 
| @@ -1,36 +0,0 @@ | |
| 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 { extractVizImport } from './extractVizImport';
         | 
| 7 | 
            -
            // import { Content, VizId, getFileText } from 'entities';
         | 
| 8 | 
            -
            // import { VizCache } from './vizCache';
         | 
| 9 | 
            -
             | 
| 10 | 
            -
            // export const importFromViz = (
         | 
| 11 | 
            -
            //   vizCache: VizCache,
         | 
| 12 | 
            -
            // ): InputPluginOption => ({
         | 
| 13 | 
            -
            //   name: 'importFromViz',
         | 
| 14 | 
            -
            //   // If we are import from another viz, like this:
         | 
| 15 | 
            -
            //   // import { message } from "@curran/21f72bf74ef04ea0b9c9b82aaaec859a";
         | 
| 16 | 
            -
            //   // import { message } from "@curran/scatter-plot";
         | 
| 17 | 
            -
            //   // then this import is the responsibility of this plugin.
         | 
| 18 | 
            -
            //   resolveId: async (id: string) => {
         | 
| 19 | 
            -
            //     console.log('importFromViz: resolveId() ' + id);
         | 
| 20 | 
            -
            //     const vizImport = extractVizImport(id);
         | 
| 21 | 
            -
            //     if (vizImport) {
         | 
| 22 | 
            -
            //       return vizImport.vizId;
         | 
| 23 | 
            -
            //     } else {
         | 
| 24 | 
            -
            //       return null;
         | 
| 25 | 
            -
            //     }
         | 
| 26 | 
            -
            //   },
         | 
| 27 | 
            -
            //   load: (id: string) => {
         | 
| 28 | 
            -
            //     const vizId: VizId = id as VizId;
         | 
| 29 | 
            -
            //     const content: Content = vizCache.get(vizId);
         | 
| 30 | 
            -
            //     const code = getFileText(content, 'index.js');
         | 
| 31 | 
            -
            //     // console.log('importFromViz: load() ' + id);
         | 
| 32 | 
            -
            //     // console.log('  content:');
         | 
| 33 | 
            -
            //     // console.log(content);
         | 
| 34 | 
            -
            //     return code;
         | 
| 35 | 
            -
            //   },
         | 
| 36 | 
            -
            // });
         | 
    
        package/src/v3Runtime/index.ts
    DELETED
    
    | @@ -1 +0,0 @@ | |
| 1 | 
            -
            export type { V3BuildResult } from './types';
         | 
    
        package/src/v3Runtime/parseId.ts
    DELETED
    
    | @@ -1,14 +0,0 @@ | |
| 1 | 
            -
            import { VizId } from 'entities';
         | 
| 2 | 
            -
            import { ResolvedVizFileId } from './types';
         | 
| 3 | 
            -
             | 
| 4 | 
            -
            export const parseId = (
         | 
| 5 | 
            -
              id: ResolvedVizFileId,
         | 
| 6 | 
            -
            ): {
         | 
| 7 | 
            -
              vizId: VizId;
         | 
| 8 | 
            -
              fileName: string;
         | 
| 9 | 
            -
            } => {
         | 
| 10 | 
            -
              const [vizId, fileName]: [VizId, string] = id.split(
         | 
| 11 | 
            -
                '/',
         | 
| 12 | 
            -
              ) as [VizId, string];
         | 
| 13 | 
            -
              return { vizId, fileName };
         | 
| 14 | 
            -
            };
         |