1+ import JSZip from "jszip" ;
2+
3+ export function generatePageContent ( { html, css, js} ) {
4+ return `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style type="text/css">${ css } </style></head><body>${ html } <script type="module">${ js } </` + `script></body></html>`
5+ }
6+
7+ function downloadFile ( blob , zip = false ) {
8+ const link = document . createElement ( "a" ) ;
9+ const hash = new Date ( ) . getTime ( ) . toString ( 16 )
10+ link . href = URL . createObjectURL ( blob ) ;
11+ link . download = `project-${ hash } .${ zip ? 'zip' : 'html' } ` ;
12+ link . click ( ) ;
13+ }
14+
15+ export function exportAsPage ( { html = '' , css = '' , js = '' } ) {
16+ const page = generatePageContent ( { html, css, js} )
17+ downloadFile ( new Blob ( [ page ] , { type : 'text/html' } ) , false )
18+ }
19+
20+ export function exportAsZip ( { html = '' , css = '' , js = '' } ) {
21+ const zip = new JSZip ( ) ;
22+
23+ zip . file ( "index.html" ,
24+ '<!DOCTYPE html>' +
25+ '<html lang="en">' +
26+ '<head>' +
27+ ' <meta charset="UTF-8">' +
28+ ' <meta name="viewport" content="width=device-width, initial-scale=1.0">' +
29+ ' <title>Document</title>' +
30+ ' <link rel="stylesheet" href="./style.css">' +
31+ '</head>' +
32+ '<body>' +
33+ html +
34+ ' <script src="./main.js"></script>' +
35+ // ' <script type="module" src="./main.js"></script>' +
36+ '</body>' +
37+ '</html>' ) ;
38+ zip . file ( "style.css" , css ) ;
39+ zip . file ( "main.js" , js ) ;
40+
41+ zip . generateAsync ( { type : "blob" } ) . then ( function ( content ) {
42+ downloadFile ( content , true )
43+ } ) ;
44+ }
0 commit comments