|
| 1 | +import pkg from '../../package.json'; |
| 2 | +import type { Tab } from '../../src'; |
| 3 | +import { compressToBase64 } from '@amoutonbrady/lz-string'; |
| 4 | +import dedent from 'dedent'; |
| 5 | + |
| 6 | +type IFiles = Record<string, { content: string | Record<string, any>; isBinary: boolean }>; |
| 7 | +const viteConfigFile = dedent` |
| 8 | +import { defineConfig } from "vite"; |
| 9 | +import solidPlugin from "vite-plugin-solid"; |
| 10 | +
|
| 11 | +export default defineConfig({ |
| 12 | + plugins: [solidPlugin()], |
| 13 | + build: { |
| 14 | + target: "esnext", |
| 15 | + polyfillDynamicImport: false, |
| 16 | + }, |
| 17 | +}); |
| 18 | +`; |
| 19 | +const tsConfig = JSON.stringify( |
| 20 | + { |
| 21 | + compilerOptions: { |
| 22 | + strict: false, |
| 23 | + module: 'ESNext', |
| 24 | + target: 'ESNext', |
| 25 | + jsx: 'preserve', |
| 26 | + esModuleInterop: true, |
| 27 | + sourceMap: true, |
| 28 | + allowJs: true, |
| 29 | + lib: ['es6', 'dom'], |
| 30 | + rootDir: 'src', |
| 31 | + moduleResolution: 'node', |
| 32 | + jsxImportSource: 'solid-js', |
| 33 | + types: ['solid-js', 'solid-js/dom'], |
| 34 | + }, |
| 35 | + }, |
| 36 | + null, |
| 37 | + 2, |
| 38 | +); |
| 39 | + |
| 40 | +const { dependencies: d, devDependencies: dd } = pkg; |
| 41 | + |
| 42 | +const packageJSON = JSON.stringify( |
| 43 | + { |
| 44 | + scripts: { |
| 45 | + start: 'vite', |
| 46 | + build: 'vite build', |
| 47 | + }, |
| 48 | + dependencies: { |
| 49 | + 'solid-js': d['solid-js'], |
| 50 | + }, |
| 51 | + devDependencies: { |
| 52 | + vite: dd['vite'], |
| 53 | + 'vite-plugin-solid': dd['vite-plugin-solid'], |
| 54 | + }, |
| 55 | + }, |
| 56 | + null, |
| 57 | + 2, |
| 58 | +); |
| 59 | + |
| 60 | +const indexHTML = (tabs: Tab[]) => dedent` |
| 61 | +<html> |
| 62 | + <head> |
| 63 | + <title>Vite Sandbox</title> |
| 64 | + <meta charset="UTF-8" /> |
| 65 | + </head> |
| 66 | +
|
| 67 | + <body> |
| 68 | + <div id="app"></div> |
| 69 | +
|
| 70 | + <script type="module" src="./src/${tabs[0].name}.tsx"></script> |
| 71 | + </body> |
| 72 | +</html> |
| 73 | +`; |
| 74 | + |
| 75 | +function getParameters(parameters: { files: IFiles }) { |
| 76 | + return compressToBase64(JSON.stringify(parameters)) |
| 77 | + .replace(/\+/g, '-') // Convert '+' to '-' |
| 78 | + .replace(/\//g, '_') // Convert '/' to '_' |
| 79 | + .replace(/=+$/, ''); // Remove ending '=' |
| 80 | +} |
| 81 | + |
| 82 | +export function exportToCsb(tabs: Tab[]): void { |
| 83 | + const params = tabs.reduce<IFiles>((p, tab) => { |
| 84 | + p[`src/${tab.name}.${tab.type}`] = { content: tab.source, isBinary: false }; |
| 85 | + return p; |
| 86 | + }, {}); |
| 87 | + |
| 88 | + const parameters = getParameters({ |
| 89 | + files: { |
| 90 | + ...params, |
| 91 | + 'vite.config.ts': { |
| 92 | + content: viteConfigFile, |
| 93 | + isBinary: false, |
| 94 | + }, |
| 95 | + 'tsconfig.json': { |
| 96 | + content: tsConfig, |
| 97 | + isBinary: false, |
| 98 | + }, |
| 99 | + 'index.html': { |
| 100 | + content: indexHTML(tabs), |
| 101 | + isBinary: false, |
| 102 | + }, |
| 103 | + 'package.json': { |
| 104 | + content: packageJSON, |
| 105 | + isBinary: false, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + const url = `https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}`; |
| 111 | + |
| 112 | + const a = document.createElement('a'); |
| 113 | + a.setAttribute('href', url); |
| 114 | + a.setAttribute('target', '_blank'); |
| 115 | + a.setAttribute('rel', 'noopener'); |
| 116 | + |
| 117 | + document.body.appendChild(a); |
| 118 | + a.click(); |
| 119 | + a.remove(); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * This function will convert the tabs of the playground |
| 124 | + * into a JSON formatted playground that can then be reimported later on |
| 125 | + * via the url `https://playground.solidjs.com/?data=my-file.json` or |
| 126 | + * vua the import button |
| 127 | + * |
| 128 | + * @param tabs {Tab[]} - The tabs to export |
| 129 | + */ |
| 130 | +export async function exportToZip(tabs: Tab[]): Promise<void> { |
| 131 | + const { default: JSZip } = await import('jszip'); |
| 132 | + const zip = new JSZip(); |
| 133 | + |
| 134 | + // basic structure |
| 135 | + zip.file('index.html', indexHTML(tabs)); |
| 136 | + zip.file('package.json', packageJSON); |
| 137 | + zip.file('vite.config.ts', viteConfigFile); |
| 138 | + zip.file('tsconfig.json', tsConfig); |
| 139 | + |
| 140 | + // project src |
| 141 | + const src = zip.folder('src')!; |
| 142 | + |
| 143 | + for (const tab of tabs) { |
| 144 | + src.file(`${tab.name}.${tab.type}`, tab.source); |
| 145 | + } |
| 146 | + |
| 147 | + const blob = await zip.generateAsync({ type: 'blob' }); |
| 148 | + const url = URL.createObjectURL(blob); |
| 149 | + |
| 150 | + const anchor = (<a href={url} target="_blank" rel="noopener" download />) as HTMLElement; |
| 151 | + document.body.prepend(anchor); |
| 152 | + anchor.click(); |
| 153 | + anchor.remove(); |
| 154 | +} |
0 commit comments