Skip to content

Commit

Permalink
fix: invalid html error by vite
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaAmaju committed Apr 16, 2024
1 parent de621e6 commit 669aea9
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/core/src/vite/plugin-build-html/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ function is_local_path(path: string) {
);
}

const VITE_HTML_PLACEHOLDER = "<div data-obfuscation-placeholder></div>";

const obfuscate: Plugin = {
name: "plugin-obfuscate",
/**
* we need to execute this before any vite html parser sees it and throws an error because of the
* svelte script at the beginning of the markup, to trick it into accepting the markup as valid HTML
*/
transformIndexHtml: {
order: "pre",
handler(html) {
return `${VITE_HTML_PLACEHOLDER}\n${html}`;
},
},
};

const deobfuscate: Plugin = {
enforce: "post",
name: "plugin-deobfuscate",
transformIndexHtml(html) {
return html.replace(`${VITE_HTML_PLACEHOLDER}\n`, "");
},
};

/**
* Builds svelte components as HTML files, processes images, stylesheets, script tags
* and other asset types
Expand Down Expand Up @@ -197,13 +221,13 @@ export function plugin_build_html({
logLevel: "silent",
mode: "production",
css: resolved_vite_config.css,
plugins: [...plugins, resolve],
json: resolved_vite_config.json,
base: resolved_vite_config.base,
define: resolved_vite_config.define,
esbuild: resolved_vite_config.esbuild,
optimizeDeps: resolved_vite_config.optimizeDeps,
envPrefix: resolved_vite_config.envPrefix as string[],
plugins: [...plugins, resolve, obfuscate, deobfuscate],
build: {
ssr: false,
outDir: build_dir,
Expand Down

0 comments on commit 669aea9

Please sign in to comment.