|
| 1 | +import { readdirSync, writeFileSync, mkdirSync, existsSync, rmSync, cpSync } from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +// Paths |
| 5 | +const distPath = path.resolve('dist/web-components'); |
| 6 | +const staticPath = path.resolve('storybook-static'); |
| 7 | +const iframePath = path.join(staticPath, 'iframe.html'); |
| 8 | + |
| 9 | +// 1️⃣ Clear previous static folder |
| 10 | +if (existsSync(staticPath)) rmSync(staticPath, { recursive: true }); |
| 11 | +mkdirSync(staticPath, { recursive: true }); |
| 12 | + |
| 13 | +// 2️⃣ Copy web-components folder |
| 14 | +cpSync(distPath, path.join(staticPath, 'web-components'), { recursive: true }); |
| 15 | + |
| 16 | +// 3️⃣ Get all JS/CSS files in dist/web-components |
| 17 | +const files = readdirSync(distPath); |
| 18 | +const jsFiles = files.filter(f => f.endsWith('.js')); |
| 19 | +const cssFiles = files.filter(f => f.endsWith('.css')); |
| 20 | + |
| 21 | +// 4️⃣ Generate iframe.html dynamically |
| 22 | +let iframeContent = `<!DOCTYPE html> |
| 23 | +<html lang="en"> |
| 24 | +<head> |
| 25 | +<meta charset="UTF-8"> |
| 26 | +<title>Stencil Storybook</title> |
| 27 | +`; |
| 28 | + |
| 29 | +// Include CSS files |
| 30 | +cssFiles.forEach(file => { |
| 31 | + iframeContent += `<link rel="stylesheet" href="/web-components/${file}" />\n`; |
| 32 | +}); |
| 33 | + |
| 34 | +// Include JS files |
| 35 | +jsFiles.forEach(file => { |
| 36 | + iframeContent += `<script type="module" src="/web-components/${file}"></script>\n`; |
| 37 | +}); |
| 38 | + |
| 39 | +iframeContent += `</head> |
| 40 | +<body> |
| 41 | +<storybook-root></storybook-root> |
| 42 | +</body> |
| 43 | +</html>`; |
| 44 | + |
| 45 | +// 5️⃣ Write iframe.html |
| 46 | +writeFileSync(iframePath, iframeContent, 'utf-8'); |
| 47 | + |
| 48 | +console.log('✅ Generated iframe.html with all JS/CSS files dynamically!'); |
0 commit comments