@vizhub/runtime 0.1.4 → 0.2.0
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/LICENSE +21 -0
- package/README.md +240 -194
- package/dist/common/packageJson.d.ts +39 -0
- package/dist/common/packageJson.js +71 -0
- package/dist/common/packageJson.test.d.ts +1 -0
- package/dist/common/packageJson.test.js +146 -0
- package/dist/common/sucrasePlugin.d.ts +2 -0
- package/dist/common/sucrasePlugin.js +24 -0
- package/dist/common/virtualFileSystem.d.ts +3 -0
- package/dist/common/virtualFileSystem.js +75 -0
- package/dist/determineRuntimeVersion.d.ts +2 -0
- package/dist/determineRuntimeVersion.js +22 -0
- package/dist/determineRuntimeVersion.test.d.ts +1 -0
- package/dist/determineRuntimeVersion.test.js +101 -0
- package/dist/fixtures/basicHTML.d.ts +2 -0
- package/dist/fixtures/basicHTML.js +22 -0
- package/dist/fixtures/fetchProxy.d.ts +2 -0
- package/dist/fixtures/fetchProxy.js +25 -0
- package/dist/fixtures/index.d.ts +3 -0
- package/dist/fixtures/index.js +9 -0
- package/dist/fixtures/jsScriptTag.d.ts +2 -0
- package/dist/fixtures/jsScriptTag.js +19 -0
- package/dist/index.d.ts +14 -8
- package/dist/index.js +59 -24882
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +50 -0
- package/dist/magicSandbox/fixProtocollessUrls.d.ts +4 -0
- package/dist/magicSandbox/fixProtocollessUrls.js +10 -0
- package/dist/magicSandbox/generateInterceptorScript.d.ts +4 -0
- package/dist/magicSandbox/generateInterceptorScript.js +178 -0
- package/dist/magicSandbox/index.d.ts +19 -0
- package/dist/magicSandbox/index.js +48 -0
- package/dist/test/fixtures/basicHTML.d.ts +2 -0
- package/dist/test/fixtures/basicHTML.js +19 -0
- package/dist/test/fixtures/fetchProxy.d.ts +2 -0
- package/dist/test/fixtures/fetchProxy.js +22 -0
- package/dist/test/fixtures/index.d.ts +6 -0
- package/dist/test/fixtures/index.js +6 -0
- package/dist/test/fixtures/jsScriptTag.d.ts +2 -0
- package/dist/test/fixtures/jsScriptTag.js +16 -0
- package/dist/test/fixtures/magicSandbox/basicHTML.d.ts +5 -0
- package/dist/test/fixtures/magicSandbox/basicHTML.js +17 -0
- package/dist/test/fixtures/magicSandbox/fetchProxy.d.ts +11 -0
- package/dist/test/fixtures/magicSandbox/fetchProxy.js +18 -0
- package/dist/test/fixtures/magicSandbox/index.d.ts +6 -0
- package/dist/test/fixtures/magicSandbox/index.js +6 -0
- package/dist/test/fixtures/magicSandbox/jsScriptTag.d.ts +8 -0
- package/dist/test/fixtures/magicSandbox/jsScriptTag.js +13 -0
- package/dist/test/fixtures/magicSandbox/protocolTest.d.ts +5 -0
- package/dist/test/fixtures/magicSandbox/protocolTest.js +17 -0
- package/dist/test/fixtures/magicSandbox/styleTest.d.ts +8 -0
- package/dist/test/fixtures/magicSandbox/styleTest.js +19 -0
- package/dist/test/fixtures/magicSandbox/xmlTest.d.ts +8 -0
- package/dist/test/fixtures/magicSandbox/xmlTest.js +23 -0
- package/dist/test/fixtures/protocolTest.d.ts +2 -0
- package/dist/test/fixtures/protocolTest.js +19 -0
- package/dist/test/fixtures/styleTest.d.ts +2 -0
- package/dist/test/fixtures/styleTest.js +22 -0
- package/dist/test/fixtures/v1/basicHTML.d.ts +3 -0
- package/dist/test/fixtures/v1/basicHTML.js +15 -0
- package/dist/test/fixtures/v1/fetchProxy.d.ts +5 -0
- package/dist/test/fixtures/v1/fetchProxy.js +12 -0
- package/dist/test/fixtures/v1/index.d.ts +6 -0
- package/dist/test/fixtures/v1/index.js +6 -0
- package/dist/test/fixtures/v1/jsScriptTag.d.ts +4 -0
- package/dist/test/fixtures/v1/jsScriptTag.js +9 -0
- package/dist/test/fixtures/v1/protocolTest.d.ts +3 -0
- package/dist/test/fixtures/v1/protocolTest.js +15 -0
- package/dist/test/fixtures/v1/styleTest.d.ts +4 -0
- package/dist/test/fixtures/v1/styleTest.js +15 -0
- package/dist/test/fixtures/v1/xmlTest.d.ts +4 -0
- package/dist/test/fixtures/v1/xmlTest.js +19 -0
- package/dist/test/fixtures/v2/basicBundle.d.ts +5 -0
- package/dist/test/fixtures/v2/basicBundle.js +5 -0
- package/dist/test/fixtures/v2/basicBundleNoExtension.d.ts +5 -0
- package/dist/test/fixtures/v2/basicBundleNoExtension.js +5 -0
- package/dist/test/fixtures/v2/d3Import.d.ts +4 -0
- package/dist/test/fixtures/v2/d3Import.js +7 -0
- package/dist/test/fixtures/v2/d3ImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/d3ImportPkg.js +19 -0
- package/dist/test/fixtures/v2/d3RosettaImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/d3RosettaImportPkg.js +19 -0
- package/dist/test/fixtures/v2/es6Preserve.d.ts +4 -0
- package/dist/test/fixtures/v2/es6Preserve.js +6 -0
- package/dist/test/fixtures/v2/generatorSupport.d.ts +4 -0
- package/dist/test/fixtures/v2/generatorSupport.js +6 -0
- package/dist/test/fixtures/v2/index.d.ts +14 -0
- package/dist/test/fixtures/v2/index.js +15 -0
- package/dist/test/fixtures/v2/jsxTranspile.d.ts +4 -0
- package/dist/test/fixtures/v2/jsxTranspile.js +6 -0
- package/dist/test/fixtures/v2/reactDomImport.d.ts +4 -0
- package/dist/test/fixtures/v2/reactDomImport.js +8 -0
- package/dist/test/fixtures/v2/reactDomImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/reactDomImportPkg.js +24 -0
- package/dist/test/fixtures/v2/reactImport.d.ts +4 -0
- package/dist/test/fixtures/v2/reactImport.js +7 -0
- package/dist/test/fixtures/v2/reactImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/reactImportPkg.js +17 -0
- package/dist/test/fixtures/v2/sourceMapErrorFixture.d.ts +5 -0
- package/dist/test/fixtures/v2/sourceMapErrorFixture.js +19 -0
- package/dist/test/fixtures/v2/unicodeSupport.d.ts +4 -0
- package/dist/test/fixtures/v2/unicodeSupport.js +6 -0
- package/dist/test/fixtures/v3/basicIndexJS.d.ts +3 -0
- package/dist/test/fixtures/v3/basicIndexJS.js +7 -0
- package/dist/test/fixtures/v3/cssImport.d.ts +4 -0
- package/dist/test/fixtures/v3/cssImport.js +9 -0
- package/dist/test/fixtures/v3/csvImport.d.ts +4 -0
- package/dist/test/fixtures/v3/csvImport.js +11 -0
- package/dist/test/fixtures/v3/csvStrangeChars.d.ts +4 -0
- package/dist/test/fixtures/v3/csvStrangeChars.js +12 -0
- package/dist/test/fixtures/v3/index.d.ts +9 -0
- package/dist/test/fixtures/v3/index.js +9 -0
- package/dist/test/fixtures/v3/jsExport.d.ts +4 -0
- package/dist/test/fixtures/v3/jsExport.js +10 -0
- package/dist/test/fixtures/v3/sourcemap.d.ts +3 -0
- package/dist/test/fixtures/v3/sourcemap.js +10 -0
- package/dist/test/fixtures/v3/svelte.d.ts +4 -0
- package/dist/test/fixtures/v3/svelte.js +18 -0
- package/dist/test/fixtures/v3/vizContent.d.ts +2 -0
- package/dist/test/fixtures/v3/vizContent.js +11 -0
- package/dist/test/fixtures/v3/vizImport.d.ts +6 -0
- package/dist/test/fixtures/v3/vizImport.js +85 -0
- package/dist/test/fixtures/v4/d3Usage.d.ts +5 -0
- package/dist/test/fixtures/v4/d3Usage.js +31 -0
- package/dist/test/fixtures/v4/esmBuild.d.ts +5 -0
- package/dist/test/fixtures/v4/esmBuild.js +18 -0
- package/dist/test/fixtures/v4/fetchInterception.d.ts +5 -0
- package/dist/test/fixtures/v4/fetchInterception.js +18 -0
- package/dist/test/fixtures/v4/index.d.ts +9 -0
- package/dist/test/fixtures/v4/index.js +9 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModule.d.ts +4 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModule.js +18 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModules.d.ts +5 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModules.js +12 -0
- package/dist/test/fixtures/v4/reactHooks.d.ts +6 -0
- package/dist/test/fixtures/v4/reactHooks.js +47 -0
- package/dist/test/fixtures/v4/reactJsx.d.ts +6 -0
- package/dist/test/fixtures/v4/reactJsx.js +34 -0
- package/dist/test/fixtures/v4/threeJsUsage.d.ts +4 -0
- package/dist/test/fixtures/v4/threeJsUsage.js +24 -0
- package/dist/test/fixtures/v4/typeScriptSupport.d.ts +5 -0
- package/dist/test/fixtures/v4/typeScriptSupport.js +29 -0
- package/dist/test/fixtures/xmlTest.d.ts +2 -0
- package/dist/test/fixtures/xmlTest.js +26 -0
- package/dist/test/index.test.d.ts +1 -0
- package/dist/test/index.test.js +40 -0
- package/dist/test/testInBrowser.d.ts +16 -0
- package/dist/test/testInBrowser.js +47 -0
- package/dist/test/testStackTrace.d.ts +9 -0
- package/dist/test/testStackTrace.js +55 -0
- package/dist/test/v1.test.d.ts +1 -0
- package/dist/test/v1.test.js +65 -0
- package/dist/test/v2.test.d.ts +1 -0
- package/dist/test/v2.test.js +121 -0
- package/dist/test/v3.test.d.ts +1 -0
- package/dist/test/v3.test.js +114 -0
- package/dist/test/v4.test.d.ts +1 -0
- package/dist/test/v4.test.js +100 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/dist/utils/getFileText.d.ts +2 -0
- package/dist/utils/getFileText.js +13 -0
- package/dist/utils/getFileText.test.d.ts +1 -0
- package/dist/utils/getFileText.test.js +72 -0
- package/dist/utils/vizContentToFileCollection.d.ts +8 -0
- package/dist/utils/vizContentToFileCollection.js +19 -0
- package/dist/utils/vizContentToFileCollection.test.d.ts +1 -0
- package/dist/utils/vizContentToFileCollection.test.js +75 -0
- package/dist/v2/computeBundleJSV2.d.ts +7 -0
- package/dist/v2/computeBundleJSV2.js +29 -0
- package/dist/v2/getComputedIndexHtml.d.ts +4 -0
- package/dist/v2/getComputedIndexHtml.js +81 -0
- package/dist/v2/getComputedIndexHtml.test.d.ts +1 -0
- package/dist/v2/getComputedIndexHtml.test.js +134 -0
- package/dist/v2/index.d.ts +1 -0
- package/dist/v2/index.js +1 -0
- package/dist/v2/v2Build.d.ts +7 -0
- package/dist/v2/v2Build.js +11 -0
- package/dist/v3/computeBundleJSV3.d.ts +18 -0
- package/dist/v3/computeBundleJSV3.js +48 -0
- package/dist/v3/createVizContent.d.ts +9 -0
- package/dist/v3/createVizContent.js +23 -0
- package/dist/{v3Runtime → v3}/extractVizImport.d.ts +2 -3
- package/dist/v3/extractVizImport.js +23 -0
- package/dist/v3/extractVizImport.test.d.ts +1 -0
- package/dist/v3/extractVizImport.test.js +30 -0
- package/dist/v3/htmlTemplate.d.ts +5 -0
- package/dist/v3/htmlTemplate.js +89 -0
- package/dist/v3/index.d.ts +1 -0
- package/dist/v3/index.js +1 -0
- package/dist/v3/parseId.d.ts +6 -0
- package/dist/v3/parseId.js +5 -0
- package/dist/v3/parseId.test.d.ts +1 -0
- package/dist/v3/parseId.test.js +25 -0
- package/dist/v3/slugCache.d.ts +11 -0
- package/dist/v3/slugCache.js +35 -0
- package/dist/v3/slugCache.test.d.ts +1 -0
- package/dist/v3/slugCache.test.js +105 -0
- package/dist/v3/transformDSV/dsvParseSrc.d.ts +1 -0
- package/dist/v3/transformDSV/dsvParseSrc.js +121 -0
- package/dist/v3/transformDSV/index.d.ts +2 -0
- package/dist/v3/transformDSV/index.js +40 -0
- package/dist/{v3Runtime → v3}/transformSvelte.d.ts +4 -2
- package/dist/v3/transformSvelte.js +77 -0
- package/dist/{v3Runtime → v3}/types.d.ts +18 -18
- package/dist/v3/types.js +1 -0
- package/dist/v3/v3Build.d.ts +15 -0
- package/dist/v3/v3Build.js +30 -0
- package/dist/v3/vizCache.d.ts +10 -0
- package/dist/v3/vizCache.js +36 -0
- package/dist/v3/vizCache.test.d.ts +1 -0
- package/dist/v3/vizCache.test.js +98 -0
- package/dist/{v3Runtime → v3}/vizLoad.d.ts +3 -4
- package/dist/v3/vizLoad.js +46 -0
- package/dist/v3/vizResolve.d.ts +7 -0
- package/dist/v3/vizResolve.js +69 -0
- package/dist/v4/index.d.ts +10 -0
- package/dist/v4/index.js +134 -0
- package/package.json +35 -38
- package/dist/assets/setupV3Runtime-BVr5lyKp.js +0 -240
- package/dist/computeSrcDoc.d.ts +0 -13
- package/dist/computeSrcDoc.d.ts.map +0 -1
- package/dist/computeSrcDoc.test.d.ts +0 -2
- package/dist/computeSrcDoc.test.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/useRuntime.d.ts +0 -13
- package/dist/useRuntime.d.ts.map +0 -1
- package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts +0 -5
- package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts.map +0 -1
- package/dist/v2Runtime/bundle/packageJson.d.ts +0 -10
- package/dist/v2Runtime/bundle/packageJson.d.ts.map +0 -1
- package/dist/v2Runtime/computeSrcDocV2.d.ts +0 -3
- package/dist/v2Runtime/computeSrcDocV2.d.ts.map +0 -1
- package/dist/v2Runtime/computeSrcDocV2.test.d.ts +0 -2
- package/dist/v2Runtime/computeSrcDocV2.test.d.ts.map +0 -1
- package/dist/v2Runtime/getComputedIndexHtml.d.ts +0 -4
- package/dist/v2Runtime/getComputedIndexHtml.d.ts.map +0 -1
- package/dist/v2Runtime/getComputedIndexHtml.test.d.ts +0 -2
- package/dist/v2Runtime/getComputedIndexHtml.test.d.ts.map +0 -1
- package/dist/v2Runtime/getText.d.ts +0 -3
- package/dist/v2Runtime/getText.d.ts.map +0 -1
- package/dist/v2Runtime/transformFiles.d.ts +0 -3
- package/dist/v2Runtime/transformFiles.d.ts.map +0 -1
- package/dist/v2Runtime/v3FilesToV2Files.d.ts +0 -3
- package/dist/v2Runtime/v3FilesToV2Files.d.ts.map +0 -1
- package/dist/v2Runtime/v3FilesToV2Files.test.d.ts +0 -2
- package/dist/v2Runtime/v3FilesToV2Files.test.d.ts.map +0 -1
- package/dist/v3Runtime/build.d.ts +0 -14
- package/dist/v3Runtime/build.d.ts.map +0 -1
- package/dist/v3Runtime/build.test.d.ts +0 -2
- package/dist/v3Runtime/build.test.d.ts.map +0 -1
- package/dist/v3Runtime/cleanRollupErrorMessage.d.ts +0 -5
- package/dist/v3Runtime/cleanRollupErrorMessage.d.ts.map +0 -1
- package/dist/v3Runtime/computeSrcDocV3.d.ts +0 -7
- package/dist/v3Runtime/computeSrcDocV3.d.ts.map +0 -1
- package/dist/v3Runtime/extractVizImport.d.ts.map +0 -1
- package/dist/v3Runtime/extractVizImport.test.d.ts +0 -2
- package/dist/v3Runtime/extractVizImport.test.d.ts.map +0 -1
- package/dist/v3Runtime/generateRollupErrorMessage.d.ts +0 -5
- package/dist/v3Runtime/generateRollupErrorMessage.d.ts.map +0 -1
- package/dist/v3Runtime/importFromViz.d.ts +0 -1
- package/dist/v3Runtime/importFromViz.d.ts.map +0 -1
- package/dist/v3Runtime/index.d.ts +0 -2
- package/dist/v3Runtime/index.d.ts.map +0 -1
- package/dist/v3Runtime/parseId.d.ts +0 -7
- package/dist/v3Runtime/parseId.d.ts.map +0 -1
- package/dist/v3Runtime/setupV3Runtime.d.ts +0 -15
- package/dist/v3Runtime/setupV3Runtime.d.ts.map +0 -1
- package/dist/v3Runtime/transformDSV/index.d.ts +0 -3
- package/dist/v3Runtime/transformDSV/index.d.ts.map +0 -1
- package/dist/v3Runtime/transformSvelte.d.ts.map +0 -1
- package/dist/v3Runtime/types.d.ts.map +0 -1
- package/dist/v3Runtime/urlLoad.d.ts +0 -1
- package/dist/v3Runtime/urlLoad.d.ts.map +0 -1
- package/dist/v3Runtime/virtual.d.ts +0 -1
- package/dist/v3Runtime/virtual.d.ts.map +0 -1
- package/dist/v3Runtime/vizCache.d.ts +0 -11
- package/dist/v3Runtime/vizCache.d.ts.map +0 -1
- package/dist/v3Runtime/vizCache.test.d.ts +0 -2
- package/dist/v3Runtime/vizCache.test.d.ts.map +0 -1
- package/dist/v3Runtime/vizLoad.d.ts.map +0 -1
- package/dist/v3Runtime/vizLoadSvelte.d.ts +0 -1
- package/dist/v3Runtime/vizLoadSvelte.d.ts.map +0 -1
- package/dist/v3Runtime/vizResolve.d.ts +0 -10
- package/dist/v3Runtime/vizResolve.d.ts.map +0 -1
- package/dist/v3Runtime/worker.d.ts +0 -2
- package/dist/v3Runtime/worker.d.ts.map +0 -1
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Datavis Tech INC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
@@ -1,257 +1,303 @@
|
|
1
|
-
# @vizhub
|
1
|
+
# @vizhub/runtime
|
2
2
|
|
3
|
-
|
3
|
+
[](https://www.npmjs.com/package/@vizhub/runtime)
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
4
5
|
|
5
|
-
|
6
|
+
A powerful, flexible runtime environment for executing code sandboxes in the browser. `@vizhub/runtime` powers [VizHub](https://vizhub.com/) and can be used to build similar interactive coding platforms.
|
6
7
|
|
7
|
-
|
8
|
-
- **File transformations**: Transforms various file types (e.g., JavaScript, CSS, CSV, Svelte) to be used in the runtime environment.
|
9
|
-
- **Hot reloading**: Automatically reloads visualizations when files are updated, providing a smooth development experience.
|
10
|
-
- **Dependency management**: Handles external dependencies via CDN and supports custom package configurations.
|
11
|
-
- **Virtual file system**: Implements a virtual file system for handling imports and file loading within the runtime.
|
12
|
-
- **Web Worker-based build system**: Uses Web Workers to offload the build process, ensuring a responsive UI during development.
|
13
|
-
- **Multi-version runtime support**: Supports both V2 and V3 runtime environments, allowing for backward compatibility and future-proofing.
|
8
|
+
## Overview
|
14
9
|
|
15
|
-
|
10
|
+
`@vizhub/runtime` intelligently detects the appropriate runtime version based on the provided files and generates executable HTML that can be used within an iframe's `srcdoc` attribute. It handles everything from simple HTML/JS/CSS combinations to complex module bundling, dependency resolution, and cross-viz imports.
|
11
|
+
|
12
|
+
## Key Features
|
13
|
+
|
14
|
+
- **Multi-Version Runtime Support**
|
15
|
+
|
16
|
+
- **v1**: Simple HTML execution with fetch proxying
|
17
|
+
- **v2**: JavaScript bundling with Rollup, JSX support, and CDN-based dependency resolution
|
18
|
+
- **v3**: Advanced module bundling with Svelte support and cross-viz imports
|
19
|
+
- **v4**: Modern ES Modules with import maps for direct browser execution
|
20
|
+
|
21
|
+
- **Comprehensive Tooling**
|
22
|
+
|
23
|
+
- **Bundling**: Seamless integration with Rollup for module bundling
|
24
|
+
- **Transpilation**: Support for JSX (v2) and Svelte components (v3)
|
25
|
+
- **Dependency Management**: Automatic resolution via CDNs (jsDelivr/unpkg)
|
26
|
+
- **Caching**: Efficient viz content and slug resolution caching
|
27
|
+
- **Debugging**: Sourcemap generation for improved debugging experience
|
16
28
|
|
17
|
-
|
29
|
+
- **Advanced Capabilities**
|
30
|
+
- **Cross-Viz Imports**: Import code from other viz instances using `@username/vizIdOrSlug` syntax
|
31
|
+
- **Fetch Interception**: Handle cross-origin requests and authentication
|
32
|
+
- **File Type Support**: Process JS, JSX, CSS, CSV, JSON, and more
|
33
|
+
|
34
|
+
## Installation
|
18
35
|
|
19
36
|
```bash
|
20
|
-
npm install @vizhub
|
37
|
+
npm install @vizhub/runtime
|
21
38
|
```
|
22
39
|
|
23
40
|
## Usage
|
24
41
|
|
25
|
-
|
26
|
-
|
27
|
-
### Example
|
28
|
-
|
29
|
-
Here's an example of how to use the runtime to compute the `srcdoc` for a visualization:
|
42
|
+
### Basic Usage
|
30
43
|
|
31
44
|
```javascript
|
32
|
-
import {
|
33
|
-
import { rollup } from
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
name: "index.js",
|
41
|
-
text: "export const main = (container) => container.innerHTML = 'Hello, world!';",
|
42
|
-
},
|
43
|
-
},
|
44
|
-
};
|
45
|
-
|
46
|
-
const vizCache: VizCache = createVizCache({
|
47
|
-
initialContents: [content],
|
48
|
-
handleCacheMiss: async () => {
|
49
|
-
return content;
|
50
|
-
},
|
51
|
-
});
|
52
|
-
|
53
|
-
const resolveSlug = async (slug: string) => {
|
54
|
-
throw new Error("Not implemented");
|
55
|
-
};
|
56
|
-
|
57
|
-
const getSvelteCompiler = async () => {
|
58
|
-
throw new Error("Not implemented");
|
59
|
-
};
|
60
|
-
|
61
|
-
const { initialSrcdoc, initialSrcdocError } = await computeSrcDoc({
|
62
|
-
rollup,
|
63
|
-
content,
|
64
|
-
vizCache,
|
65
|
-
resolveSlug,
|
66
|
-
getSvelteCompiler,
|
67
|
-
});
|
68
|
-
|
69
|
-
const randomID = initialSrcdoc.match(/viz-container-(\d+)/)?.[1];
|
70
|
-
|
71
|
-
const srcdocWithoutMapping = initialSrcdoc.replace(
|
72
|
-
/\/\/# sourceMappingURL=.*$/gm,
|
73
|
-
""
|
74
|
-
);
|
75
|
-
|
76
|
-
expect(srcdocWithoutMapping).toEqual(`<!DOCTYPE html>
|
77
|
-
<html>
|
78
|
-
<head>
|
79
|
-
<meta charset="utf-8">
|
80
|
-
<style>
|
81
|
-
body {
|
82
|
-
margin: 0;
|
83
|
-
overflow: hidden;
|
84
|
-
}
|
85
|
-
#viz-container-${randomID} {
|
86
|
-
height: 100vh;
|
87
|
-
}
|
88
|
-
</style>
|
89
|
-
</head>
|
90
|
-
<body>
|
91
|
-
<div id="viz-container-${randomID}"></div>
|
92
|
-
<script id="injected-script">(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.Viz={}));})(this,(function(exports){'use strict';const main = (container) => container.innerHTML = 'Hello, world!';exports.main=main;}));
|
93
|
-
|
94
|
-
|
95
|
-
<script>
|
96
|
-
(() => {
|
97
|
-
let cleanup;
|
98
|
-
const render = () => {
|
99
|
-
const container = document.getElementById('viz-container-${randomID}');
|
100
|
-
typeof cleanup === 'function' && cleanup();
|
101
|
-
cleanup = Viz.main(container, { state: window.state, setState, writeFile });
|
102
|
-
};
|
103
|
-
const setState = (next) => {
|
104
|
-
window.state = next(window.state);
|
105
|
-
render();
|
106
|
-
};
|
107
|
-
const writeFile = (fileName, content) => {
|
108
|
-
parent.postMessage({ type: 'writeFile', fileName, content }, "*");
|
109
|
-
};
|
110
|
-
const run = () => {
|
111
|
-
try {
|
112
|
-
setState((state) => state || {});
|
113
|
-
} catch (error) {
|
114
|
-
console.error(error);
|
115
|
-
parent.postMessage({ type: 'runError', error }, "*");
|
116
|
-
}
|
117
|
-
}
|
118
|
-
run();
|
119
|
-
const runJS = (src) => {
|
120
|
-
document.getElementById('injected-script')?.remove();
|
121
|
-
const script = document.createElement('script');
|
122
|
-
script.textContent = src;
|
123
|
-
script.id = 'injected-script';
|
124
|
-
document.body.appendChild(script);
|
125
|
-
run();
|
126
|
-
};
|
127
|
-
const runCSS = (src, id) => {
|
128
|
-
const styleElementId = 'injected-style' + id;
|
129
|
-
let style = document.getElementById(styleElementId);
|
130
|
-
if (!style) {
|
131
|
-
style = document.createElement('style');
|
132
|
-
style.type = 'text/css';
|
133
|
-
style.id = styleElementId;
|
134
|
-
document.head.appendChild(style);
|
135
|
-
}
|
136
|
-
style.textContent = src;
|
137
|
-
};
|
138
|
-
onmessage = (message) => {
|
139
|
-
switch (message.data.type) {
|
140
|
-
case 'runJS':
|
141
|
-
runJS(message.data.src);
|
142
|
-
parent.postMessage({ type: 'runDone' }, "*");
|
143
|
-
break;
|
144
|
-
case 'runCSS':
|
145
|
-
runCSS(message.data.src, message.data.id);
|
146
|
-
break;
|
147
|
-
case 'ping':
|
148
|
-
parent.postMessage({ type: 'pong' }, "*");
|
149
|
-
break;
|
150
|
-
default:
|
151
|
-
break;
|
152
|
-
}
|
153
|
-
}
|
154
|
-
})();
|
155
|
-
</script>
|
156
|
-
</body>
|
157
|
-
</html>`);
|
45
|
+
import { buildHTML } from '@vizhub/runtime';
|
46
|
+
import { rollup } from 'rollup';
|
47
|
+
|
48
|
+
// Simple v1 runtime (HTML only)
|
49
|
+
const html = await buildHTML({
|
50
|
+
files: {
|
51
|
+
'index.html': '<html><body><h1>Hello World</h1></body></html>'
|
52
|
+
}
|
158
53
|
});
|
54
|
+
|
55
|
+
// v2 runtime with bundling
|
56
|
+
const html = await buildHTML({
|
57
|
+
files: {
|
58
|
+
'index.html': '<html><body><div id="root"></div><script src="bundle.js"></script></body></html>',
|
59
|
+
'index.js': 'import { message } from "./message"; console.log(message);',
|
60
|
+
'message.js': 'export const message = "Hello, bundled world!";'
|
61
|
+
},
|
62
|
+
rollup
|
63
|
+
});
|
64
|
+
|
65
|
+
// Use the generated HTML in an iframe
|
66
|
+
const iframe = document.createElement('iframe');
|
67
|
+
iframe.srcdoc = html;
|
68
|
+
document.body.appendChild(iframe);
|
159
69
|
```
|
160
70
|
|
161
|
-
|
71
|
+
### Runtime Versions
|
162
72
|
|
163
|
-
|
73
|
+
The library automatically detects which runtime version to use based on the files provided:
|
164
74
|
|
165
|
-
|
75
|
+
- **v1**: When only `index.html` is present
|
76
|
+
- **v2**: When both `index.html` and `index.js` (or `index.jsx`) are present
|
77
|
+
- **v3**: When only `index.js` is present (no `index.html`)
|
78
|
+
- **v4**: When `index.html` contains ES module scripts with import maps
|
166
79
|
|
167
|
-
|
80
|
+
### Advanced Usage: v3 Runtime with Cross-Viz Imports
|
168
81
|
|
169
|
-
|
82
|
+
```javascript
|
83
|
+
import { buildHTML, createVizCache, createSlugCache } from '@vizhub/runtime';
|
84
|
+
import { rollup } from 'rollup';
|
85
|
+
import { compile } from 'svelte/compiler';
|
86
|
+
|
87
|
+
// Create caches for viz content and slug resolution
|
88
|
+
const vizCache = createVizCache({
|
89
|
+
initialContents: [
|
90
|
+
{
|
91
|
+
id: 'viz-123',
|
92
|
+
files: {
|
93
|
+
'file1': { name: 'index.js', text: 'export const value = 42;' }
|
94
|
+
}
|
95
|
+
}
|
96
|
+
],
|
97
|
+
handleCacheMiss: async (vizId) => {
|
98
|
+
// Fetch viz content from your backend
|
99
|
+
return await fetchVizContent(vizId);
|
100
|
+
}
|
101
|
+
});
|
170
102
|
|
171
|
-
|
172
|
-
|
173
|
-
-
|
174
|
-
|
175
|
-
|
103
|
+
const slugCache = createSlugCache({
|
104
|
+
initialMappings: {
|
105
|
+
'username/my-viz': 'viz-123'
|
106
|
+
},
|
107
|
+
handleCacheMiss: async (slug) => {
|
108
|
+
// Resolve slug to vizId from your backend
|
109
|
+
return await resolveSlug(slug);
|
110
|
+
}
|
111
|
+
});
|
176
112
|
|
177
|
-
|
113
|
+
// Build HTML with cross-viz imports
|
114
|
+
const html = await buildHTML({
|
115
|
+
files: {
|
116
|
+
'index.js': 'import { value } from "@username/my-viz"; console.log(value);'
|
117
|
+
},
|
118
|
+
rollup,
|
119
|
+
vizCache,
|
120
|
+
vizId: 'current-viz-id',
|
121
|
+
slugCache,
|
122
|
+
getSvelteCompiler: async () => compile
|
123
|
+
});
|
124
|
+
```
|
178
125
|
|
179
|
-
|
126
|
+
## API Reference
|
180
127
|
|
181
|
-
|
182
|
-
- **`initialSrcdocError`**: Any error that occurred during the computation.
|
128
|
+
### buildHTML(options)
|
183
129
|
|
184
|
-
|
130
|
+
Builds HTML that can be used as the `srcdoc` of an iframe.
|
185
131
|
|
186
|
-
|
132
|
+
#### Options
|
187
133
|
|
188
|
-
|
134
|
+
- **files**: `FileCollection` - A map of filenames to their contents
|
135
|
+
- **rollup**: `(options: RollupOptions) => Promise<RollupBuild>` - Rollup function (required for v2, v3, v4)
|
136
|
+
- **enableSourcemap**: `boolean` - Whether to include sourcemaps (default: true)
|
137
|
+
- **vizCache**: `VizCache` - Cache for viz content (required for v3 with cross-viz imports)
|
138
|
+
- **vizId**: `string` - ID of the current viz (required for v3 with cross-viz imports)
|
139
|
+
- **slugCache**: `SlugCache` - Cache for slug resolution (optional for v3)
|
140
|
+
- **getSvelteCompiler**: `() => Promise<SvelteCompiler>` - Function that returns Svelte compiler (optional for v3)
|
189
141
|
|
190
|
-
|
191
|
-
- **`handleCacheMiss`**: A function called when a visualization is not found in the cache.
|
142
|
+
### createVizCache(options)
|
192
143
|
|
193
|
-
|
144
|
+
Creates a cache for viz content.
|
194
145
|
|
195
|
-
|
146
|
+
#### Options
|
196
147
|
|
197
|
-
-
|
198
|
-
-
|
199
|
-
- **`invalidate(vizId)`**: Invalidates the cache for a specific visualization.
|
148
|
+
- **initialContents**: `VizContent[]` - Initial viz contents to populate the cache
|
149
|
+
- **handleCacheMiss**: `(vizId: string) => Promise<VizContent>` - Function to handle cache misses
|
200
150
|
|
201
|
-
###
|
151
|
+
### createSlugCache(options)
|
202
152
|
|
203
|
-
|
153
|
+
Creates a cache for slug resolution.
|
204
154
|
|
205
|
-
####
|
155
|
+
#### Options
|
206
156
|
|
207
|
-
-
|
157
|
+
- **initialMappings**: `Record<string, string>` - Initial slug to vizId mappings
|
158
|
+
- **handleCacheMiss**: `(slug: string) => Promise<string>` - Function to handle cache misses
|
208
159
|
|
209
|
-
|
160
|
+
## Examples
|
210
161
|
|
211
|
-
|
162
|
+
### v1: Simple HTML
|
163
|
+
|
164
|
+
```javascript
|
165
|
+
const html = await buildHTML({
|
166
|
+
files: {
|
167
|
+
'index.html': `
|
168
|
+
<!DOCTYPE html>
|
169
|
+
<html>
|
170
|
+
<head>
|
171
|
+
<style>
|
172
|
+
body { font-family: sans-serif; }
|
173
|
+
</style>
|
174
|
+
</head>
|
175
|
+
<body>
|
176
|
+
<h1>Hello World</h1>
|
177
|
+
<script>
|
178
|
+
console.log('Hello from v1 runtime!');
|
179
|
+
</script>
|
180
|
+
</body>
|
181
|
+
</html>
|
182
|
+
`
|
183
|
+
}
|
184
|
+
});
|
185
|
+
```
|
212
186
|
|
213
|
-
|
187
|
+
### v2: Bundled JavaScript
|
214
188
|
|
215
|
-
|
216
|
-
|
189
|
+
```javascript
|
190
|
+
const html = await buildHTML({
|
191
|
+
files: {
|
192
|
+
'index.html': `
|
193
|
+
<!DOCTYPE html>
|
194
|
+
<html>
|
195
|
+
<body>
|
196
|
+
<div id="root"></div>
|
197
|
+
<script src="bundle.js"></script>
|
198
|
+
</body>
|
199
|
+
</html>
|
200
|
+
`,
|
201
|
+
'index.js': `
|
202
|
+
import { render } from './render';
|
203
|
+
render(document.getElementById('root'));
|
204
|
+
`,
|
205
|
+
'render.js': `
|
206
|
+
export function render(element) {
|
207
|
+
element.innerHTML = '<h1>Hello from v2 runtime!</h1>';
|
208
|
+
}
|
209
|
+
`,
|
210
|
+
'package.json': JSON.stringify({
|
211
|
+
dependencies: {
|
212
|
+
'd3': '7.8.5'
|
213
|
+
}
|
214
|
+
})
|
215
|
+
},
|
216
|
+
rollup
|
217
|
+
});
|
218
|
+
```
|
217
219
|
|
218
|
-
|
220
|
+
### v4: ES Modules with Import Maps
|
219
221
|
|
220
|
-
|
222
|
+
```javascript
|
223
|
+
const html = await buildHTML({
|
224
|
+
files: {
|
225
|
+
'index.html': `
|
226
|
+
<!DOCTYPE html>
|
227
|
+
<html>
|
228
|
+
<head>
|
229
|
+
<script type="importmap">
|
230
|
+
{
|
231
|
+
"imports": {
|
232
|
+
"utils": "./utils.js"
|
233
|
+
}
|
234
|
+
}
|
235
|
+
</script>
|
236
|
+
</head>
|
237
|
+
<body>
|
238
|
+
<div id="app"></div>
|
239
|
+
<script type="module" src="index.js"></script>
|
240
|
+
</body>
|
241
|
+
</html>
|
242
|
+
`,
|
243
|
+
'index.js': `
|
244
|
+
import { createApp } from 'utils';
|
245
|
+
createApp(document.getElementById('app'));
|
246
|
+
`,
|
247
|
+
'utils.js': `
|
248
|
+
export function createApp(element) {
|
249
|
+
element.innerHTML = '<h1>Hello from v4 runtime!</h1>';
|
250
|
+
}
|
251
|
+
`
|
252
|
+
},
|
253
|
+
rollup
|
254
|
+
});
|
255
|
+
```
|
221
256
|
|
222
257
|
## Development
|
223
258
|
|
224
|
-
|
225
|
-
|
226
|
-
### Clone the Repository
|
259
|
+
### Setup
|
227
260
|
|
228
261
|
```bash
|
229
|
-
git clone https://github.com/vizhub-core/
|
230
|
-
cd
|
262
|
+
git clone https://github.com/vizhub-core/vizhub-runtime.git
|
263
|
+
cd vizhub-runtime
|
264
|
+
npm install
|
231
265
|
```
|
232
266
|
|
233
|
-
###
|
267
|
+
### Build
|
234
268
|
|
235
269
|
```bash
|
236
|
-
npm
|
270
|
+
npm run build
|
237
271
|
```
|
238
272
|
|
239
|
-
###
|
240
|
-
|
241
|
-
The package uses Vitest for testing. Run the tests with:
|
273
|
+
### Test
|
242
274
|
|
243
275
|
```bash
|
244
|
-
npm
|
276
|
+
npm test
|
245
277
|
```
|
246
278
|
|
247
|
-
|
279
|
+
Run specific tests:
|
248
280
|
|
249
|
-
|
281
|
+
```bash
|
282
|
+
npx vitest run -t "should handle CSS imports"
|
283
|
+
```
|
284
|
+
|
285
|
+
### Type Checking
|
250
286
|
|
251
287
|
```bash
|
252
|
-
npm run
|
288
|
+
npm run typecheck
|
253
289
|
```
|
254
290
|
|
291
|
+
## Contributing
|
292
|
+
|
293
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
294
|
+
|
295
|
+
1. Fork the repository
|
296
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
297
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
298
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
299
|
+
5. Open a Pull Request
|
300
|
+
|
255
301
|
## License
|
256
302
|
|
257
|
-
This project is licensed under the MIT License
|
303
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { FileCollection } from "../types";
|
2
|
+
export type Licence = string;
|
3
|
+
export interface PackageJson {
|
4
|
+
dependencies?: {
|
5
|
+
[key: string]: string;
|
6
|
+
};
|
7
|
+
vizhub?: {
|
8
|
+
libraries?: {
|
9
|
+
[key: string]: VizHubLibraryConfig;
|
10
|
+
};
|
11
|
+
};
|
12
|
+
license?: Licence | {
|
13
|
+
type: string;
|
14
|
+
};
|
15
|
+
}
|
16
|
+
export interface Dependency {
|
17
|
+
name: string;
|
18
|
+
version: string;
|
19
|
+
}
|
20
|
+
export interface VizHubLibraryConfig {
|
21
|
+
path?: string;
|
22
|
+
global?: string;
|
23
|
+
}
|
24
|
+
export interface VizHubLibraryConfigs {
|
25
|
+
[key: string]: VizHubLibraryConfig;
|
26
|
+
}
|
27
|
+
export declare const packageJSON: (files: FileCollection) => PackageJson;
|
28
|
+
export declare const dependencies: (files: FileCollection) => {
|
29
|
+
[key: string]: string;
|
30
|
+
};
|
31
|
+
export declare const getConfiguredLibraries: (files: FileCollection) => {
|
32
|
+
[key: string]: VizHubLibraryConfig;
|
33
|
+
};
|
34
|
+
export declare const dependencySource: ({ name, version }: Dependency, libraries: VizHubLibraryConfigs, cdn?: string) => string;
|
35
|
+
export declare const getLicense: (files: FileCollection) => string | {
|
36
|
+
type: string;
|
37
|
+
} | undefined;
|
38
|
+
export declare const getConfiguredGlobals: (pkg: PackageJson) => Record<string, string>;
|
39
|
+
export declare const getGlobals: (pkg: PackageJson) => Record<string, string>;
|
@@ -0,0 +1,71 @@
|
|
1
|
+
const EMPTY_PKG_JSON = {
|
2
|
+
dependencies: {},
|
3
|
+
vizhub: {},
|
4
|
+
license: "MIT",
|
5
|
+
};
|
6
|
+
const DEBUG = false;
|
7
|
+
export const packageJSON = (files) => {
|
8
|
+
const packageJsonText = files["package.json"];
|
9
|
+
DEBUG &&
|
10
|
+
console.log("[packageJSON] packageJsonText:", packageJsonText);
|
11
|
+
try {
|
12
|
+
const pkg = packageJsonText
|
13
|
+
? JSON.parse(packageJsonText)
|
14
|
+
: EMPTY_PKG_JSON;
|
15
|
+
DEBUG &&
|
16
|
+
console.log("[packageJSON] pkg:", JSON.stringify(pkg, null, 2));
|
17
|
+
return pkg;
|
18
|
+
}
|
19
|
+
catch {
|
20
|
+
DEBUG &&
|
21
|
+
console.log("[packageJSON] Error parsing package.json");
|
22
|
+
return EMPTY_PKG_JSON;
|
23
|
+
}
|
24
|
+
};
|
25
|
+
export const dependencies = (files) => packageJSON(files).dependencies || {};
|
26
|
+
export const getConfiguredLibraries = (files) => {
|
27
|
+
const vizhubConfig = packageJSON(files).vizhub || {};
|
28
|
+
return vizhubConfig.libraries || {};
|
29
|
+
};
|
30
|
+
export const dependencySource = ({ name, version }, libraries, cdn = "jsdelivr") => {
|
31
|
+
const path = libraries[name]
|
32
|
+
? libraries[name].path || ""
|
33
|
+
: "";
|
34
|
+
const src = cdn === "jsdelivr"
|
35
|
+
? `https://cdn.jsdelivr.net/npm/${name}@${version}${path}`
|
36
|
+
: `https://unpkg.com/${name}@${version}${path}`;
|
37
|
+
return src;
|
38
|
+
};
|
39
|
+
export const getLicense = (files) => {
|
40
|
+
const license = packageJSON(files).license;
|
41
|
+
if (typeof license === "object" &&
|
42
|
+
license !== null &&
|
43
|
+
"type" in license) {
|
44
|
+
return license.type;
|
45
|
+
}
|
46
|
+
return license || EMPTY_PKG_JSON.license;
|
47
|
+
};
|
48
|
+
export const getConfiguredGlobals = (pkg) => {
|
49
|
+
const libraries = pkg?.vizhub?.libraries;
|
50
|
+
if (libraries) {
|
51
|
+
return Object.entries(libraries).reduce((accumulator, [packageName, config]) => {
|
52
|
+
if (config.global) {
|
53
|
+
accumulator[packageName] = config.global;
|
54
|
+
}
|
55
|
+
return accumulator;
|
56
|
+
}, {});
|
57
|
+
}
|
58
|
+
return {};
|
59
|
+
};
|
60
|
+
const defaultGlobals = {
|
61
|
+
d3: "d3",
|
62
|
+
react: "React",
|
63
|
+
"react-dom": "ReactDOM",
|
64
|
+
};
|
65
|
+
export const getGlobals = (pkg) => {
|
66
|
+
const configuredGlobals = getConfiguredGlobals(pkg);
|
67
|
+
return {
|
68
|
+
...defaultGlobals,
|
69
|
+
...configuredGlobals,
|
70
|
+
};
|
71
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|