From e2cafb7efa13d2a2cf75038c075d33d613e74cf9 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 9 Jan 2020 18:22:32 -0300 Subject: [PATCH] fix(tauri.js) do not inline assets on the embedded-server mode (#304) --- cli/tauri.js/src/runner.ts | 78 ++++++++++++++++----------- examples/svelte/svelte-app/.gitignore | 1 + 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/cli/tauri.js/src/runner.ts b/cli/tauri.js/src/runner.ts index 0dbd8bdda..c7412d391 100644 --- a/cli/tauri.js/src/runner.ts +++ b/cli/tauri.js/src/runner.ts @@ -159,40 +159,54 @@ class Runner { ) reject(new Error('Could not find index.html in dist dir.')) } - new Inliner(indexPath, (err: Error, html: string) => { - if (err) { - reject(err) - } else { - const dom = new JSDOM(html) - const document = dom.window.document - document.querySelectorAll('link').forEach(link => { - link.removeAttribute('rel') - link.removeAttribute('as') - }) - const tauriScript = document.createElement('script') - // @ts-ignore - tauriScript.text = readFileSync(path.join(tauriDir, 'tauri.js')) - document.body.insertBefore(tauriScript, document.body.firstChild) - - const csp = cfg.tauri.security.csp - if (csp) { - const cspTag = document.createElement('meta') - cspTag.setAttribute('http-equiv', 'Content-Security-Policy') - cspTag.setAttribute('content', csp) - document.head.appendChild(cspTag) - } - - writeFileSync( - path.join(indexDir, 'index.tauri.html'), - dom.serialize() - ) - resolve(inlinedAssets) + const rewriteHtml = (html: string, interceptor?: (dom: JSDOM) => void) => { + const dom = new JSDOM(html) + const document = dom.window.document + if (interceptor !== undefined) { + interceptor(dom) } - }).on('progress', (event: string) => { - const match = event.match(/([\S\d]+)\.([\S\d]+)/g) - match && inlinedAssets.push(match[0]) - }) + + const tauriScript = document.createElement('script') + // @ts-ignore + tauriScript.text = readFileSync(path.join(tauriDir, 'tauri.js')) + document.body.insertBefore(tauriScript, document.body.firstChild) + + const csp = cfg.tauri.security.csp + if (csp) { + const cspTag = document.createElement('meta') + cspTag.setAttribute('http-equiv', 'Content-Security-Policy') + cspTag.setAttribute('content', csp) + document.head.appendChild(cspTag) + } + writeFileSync( + path.join(indexDir, 'index.tauri.html'), + dom.serialize() + ) + } + + if (cfg.tauri.embeddedServer.active) { + rewriteHtml(readFileSync(indexPath).toString()) + resolve(inlinedAssets) + } else { + new Inliner(indexPath, (err: Error, html: string) => { + if (err) { + reject(err) + } else { + rewriteHtml(html, dom => { + const document = dom.window.document + document.querySelectorAll('link').forEach(link => { + link.removeAttribute('rel') + link.removeAttribute('as') + }) + }) + resolve(inlinedAssets) + } + }).on('progress', (event: string) => { + const match = event.match(/([\S\d]+)\.([\S\d]+)/g) + match && inlinedAssets.push(match[0]) + }) + } }) } diff --git a/examples/svelte/svelte-app/.gitignore b/examples/svelte/svelte-app/.gitignore index da93220bc..59e2fe5ad 100644 --- a/examples/svelte/svelte-app/.gitignore +++ b/examples/svelte/svelte-app/.gitignore @@ -2,3 +2,4 @@ /public/build/ .DS_Store +/public/index.tauri.html \ No newline at end of file